POJ3612——Telephone Wire

Telephone Wire

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2662   Accepted: 970

Description

Farmer John‘s cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize
N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some
heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost
C × the two poles‘ height difference for each section of wire where the poles are of different heights (1 ≤
C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.

Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer
X number of meters to a pole at a cost of X2.

Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains a single integer: heighti

Output

* Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.

Sample Input

5 2
2
3
5
1
4

Sample Output

15

Source

USACO 2007 November Gold

设dp[i][j] 表示 第i根长度为j时的最优值,直接O(n)转移是会超时的,具体方法参见代码

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;
const int inf = 0x3f3f3f3f;
int dp[N][105];
int low[200], high[200];
int h[N];

int main()
{
	int n, c;
	while (~scanf("%d%d", &n, &c))
	{
		memset (dp, inf, sizeof(dp));
		memset (low, inf, sizeof(inf));
		memset (high, inf, sizeof(high));
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &h[i]);
		}
		for (int i = h[1]; i <= 100; ++i)
		{
			dp[1][i] = (i - h[1]) * (i - h[1]);
		}
		for (int i = 1; i <= 100; ++i)
		{
			low[i] = min(low[i - 1], dp[1][i] - c * i);
		}
		for (int i = 100; i >= 1; --i)
		{
			high[i] = min(high[i + 1], dp[1][i] + c * i);
		}
		for (int i = 2; i <= n; ++i)
		{
			for (int j = h[i]; j <= 100; ++j)
			{
				dp[i][j] = min(low[j] + j * c, high[j] - j * c) + (j - h[i]) * (j - h[i]);
			}
			memset (low, inf, sizeof(inf));
			memset (high, inf, sizeof(high));
			for (int j = 1; j <= 100; ++j)
			{
				low[j] = min(low[j - 1], dp[i][j] - c * j);
			}
			for (int j = 100; j >= 1; --j)
			{
				high[j] = min(high[j + 1], dp[i][j] + c * j);
			}
		}
		int ans = inf;
		for (int i = h[n]; i <= 100; ++i)
		{
			ans = min(ans, dp[n][i]);
		}
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-08-24 09:13:48

POJ3612——Telephone Wire的相关文章

P2885 [USACO07NOV]电话线Telephone Wire

题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each wit

【POJ3612】【USACO 2007 Nov Gold 】1.Telephone Wire 动规

题意: 给出若干棵树的高度,你可以进行一种操作:把某棵树增高h,花费为h*h. 操作完成后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3很像. 暴力动规是O(1*10^9)会T 所以单调队列一下,每颗树扫两遍结束. 完事,看水代码吧. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N

【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节

意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O(1*10^9)会T 所以单调队列一下,每颗树扫两遍结束. 完事,看水代码吧. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101

[bzoj1705] [Usaco2007 Nov]Telephone Wire 架设电话线

正常DP.. f[i][j]表示前i个电线杆,把第i个电线杆高度改为j的最少总费用.设原来电线杆高度为h[] f[i][j]=min{ f[i-1][k]+C*|j-k|+(j-h[i])^2,(k>=h[i-1],j>=h[i]) } 直接上的话复杂度是O(n*100*100)= = 可以用两个数组存一下j不同取值时的k(两个数组:一个是k>=j的,另一个是k<=j的,j值改变的时候把这两个辅助数组也调一下就好了)..具体见代码吧..这样复杂度就是O(n*100)了 1 #inc

bzoj 1705;poj 3612:[Usaco2007 Nov]Telephone Wire 架设电话线

Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N <= 100,000)根电话线杆上, 第i根电话线杆的高度为height_i米(1 <= height_i <= 100). 电话线总是从一根电话线杆的顶端被引到相邻的那根的顶端 如果这两根电话线杆的高度不同,那么FJ就必须为此支付 C*电话线杆高度差(1 <= C <=

【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线

题目描述 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N <= 100,000)根电话线杆上, 第i根电话线杆的高度为height_i米(1 <= height_i <= 100). 电话线总是从一根电话线杆的顶端被引到相邻的那根的顶端 如果这两根电话线杆的高度不同,那么FJ就必须为此支付 C*电话线杆高度差(1 <= C <= 100)的费用

bzoj 1705: [Usaco2007 Nov]Telephone Wire 架设电话线——dp

Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N <= 100,000)根电话线杆上, 第i根电话线杆的高度为height_i米(1 <= height_i <= 100). 电话线总是从一根电话线杆的顶端被引到相邻的那根的顶端 如果这两根电话线杆的高度不同,那么FJ就必须为此支付 C*电话线杆高度差(1 <= C <=

[luoguP2885] [USACO07NOV]电话线Telephone Wire(DP + 贪心)

传送门 真是诡异. 首先 O(n * 100 * 100) 三重循环 f[i][j] 表示到第 i 个柱子,高度是 j 的最小花费 f[i][j] = min(f[i - 1][k] + abs(k - j) * c + (j - a[j]) * (j - a[j]) (1 <= k <= 100) 然而肯定超时 对于f[i][j]的值,既可以从f[i-1][j+]更新,又可以从f[i-1][j]更新,还可以从f[i-1][j-]更新. 所以可以从后往前扫,从前往后扫,都记录一个前缀最小值,然

tapping of data 词义来源

tapping of data 在数据交互的过程 数据被 窃听到. 例如, 网站的账户被泄露, 在用户登陆过程中, 其账号被第三方偷到. tapping 含义 看看 youdao 词典解释: n. [冶] 出钢:开孔:出渣:轻敲声 v. 轻敲(tap的ing形式):轻拍 n. (Tapping)人名:(英)塔平 貌似, 开孔.放液体 和 分支 比较接近. Telephone tapping (电话窃听) https://en.wikipedia.org/wiki/Telephone_tappin