Telephone Wire
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 2662 |
|
Accepted: 970 |
Description
Farmer John ' s cows is getting restless about their poor telephone service; They want FJ to replace the old telephone wire with new, more efficient wire. The new wiring would utilizen (2≤ n ≤100,000) already-installed telephone poles, each with some Heig Hti meters (1≤ heighti ≤100). The new wire would connect the tops of each pair of adjacent poles and would incur a penalty costC xthe both poles ' Height difference for each of the sections of the wire where the poles is of different heights (1≤C ≤100). The Poles, of course, is in a certain sequence and can is not 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 t Heir 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 the It would cost Farmer John to attach the new telephone wire.
Sample Input
5 223514
Sample Output
15
Source
Usaco November Gold
Set DP[I][J] indicates that the length of the root I is the best value of J, the direct O (n) transfer will be timed out, the specific method see code
#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, SI Zeof (DP)); memset (Low, INF, sizeof (INF)), memset (High, INF, sizeof); for (int i = 1; I <= n; ++i) {scanf ("%d", & ; h[i]);} for (int i = h[1]; I <=, ++i) {dp[1][i] = (i-h[1]) * (i-h[1]);} for (int i = 1; i <=; ++i) {Low[i] = min (low[i-1], dp[1][i]-c * i);} for (int i = +; I >= 1; i) {high[i] = min (high[i + 1], dp[1][i] + c * i);} for (int i = 2; l <= N; ++i) {for (int j = h[i]; J <=, ++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); for (int j = 1; J <=; ++j) {Low[j] = min (low[j-1], dp[i][j]-c * j);} for (int j = +, J >= 1;--j) {High[j] = min (high[j + 1], dp[i][j] + c * j);}} int ans = inf;for (int i = h[n]; I <= ++i) {ans = min (ans, dp[n][i]);} printf ("%d\n", ans);} return 0;}
Poj3612--telephone Wire