POJ3612——Telephone Wire
来源:互联网 发布:企业网站结构优化 编辑:程序博客网 时间:2024/05/22 06:51
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 utilizeN (2 ≤ N ≤ 100,000) already-installed telephone poles, each with someheighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty costC × 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 integerX 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 223514
Sample Output
15
Source
设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;}
- POJ3612——Telephone Wire
- [POJ3612]telephone wire
- 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动规
- poj 3612 Telephone Wire
- openjudge telephone wire
- poj 3612 Telephone Wire
- BZOJ1705 Telephone Wire 架设电话线
- P2885 [USACO07NOV]电话线Telephone Wire
- poj3612
- [BZOJ1705] [Usaco2007 Nov]Telephone Wire 架设电话线
- 1705: [Usaco2007 Nov]Telephone Wire 架设电话线
- 译文及题解 电话线Telephone Wire(动态规划)
- 单总线协议(1—wire)
- Telephone
- 深夜切题——Keyboard of a Mobile Telephone
- telephone conference
- telephone interview
- Android telephone
- C 语言程序设计实践 4.8 三角形和圆
- HDU1717循环小数化成分数
- Ajax加载外部页面的一个弹出层效果
- T02Scanner
- 我爱你们
- POJ3612——Telephone Wire
- 自动生成已有部门的编号-RowNumber+Partition
- C/C++中字符串与数字相互转换
- HiLo
- 在BeagleBone Black上构建Linux 0.1
- poj2485 Kruskal算法+并查集 922MS险过
- average
- Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly Detection&Recommende
- Autocomplete in ASP.NET MVC3自动检索并填充输入框