P2885 [usaco 07nov] Telephone Wire, p2885usaco 07nov
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.
Given the height of several trees, you can perform one operation: increase the height of a tree by h, and the cost is h * h.
After the operation is complete, the cost between the two regions is the height difference * fixed value c.
Calculate the sum of two consumption values and the minimum value.
Input/Output Format
Input Format:
Output Format:
- Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.
Input and Output sample input sample #1:
5 223514
Output sample #
I wrote a DP myself at the beginning, so I can use this example.
Very excited,
But only 70 points are handed in.
Found that time complexity is a bit high
The idea is simple:
We can easily see that this question is ineffective,
Use dp [I] [j] to represent the first I tree. The height of the I tree is the minimum cost of j.
Preprocessing dp [1] [j], then for each tree, we enumerate the height of the previous tree and the height of the tree,
Just calculate it.
Time Complexity n * h
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <cstring> 6 # include <algorithm> 7 # include <queue> 8 # include <cstdlib> 9 using namespace std; 10 const int MAXN = 100001; 11 const int INF = 0x7f7f7f; 12 inline void read (int & n) 13 {14 char c = '+'; bool flag = 0; n = 0; 15 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1;} 16 while (c> = '0' & c <= '9') n = n * 10 + c-48, c = getchar (); 17} 18 int dp [MAXN] [201]; // the I-th tree. The minimum cost for the height of j is 19 int n, C; 20 int a [MAXN]; 21 int maxheight; 22 int main () 23 {24 read (n); read (C); 25 memset (dp, INF, sizeof (dp )); 26 for (int I = 1; I <= n; I ++) 27 read (a [I]), maxheight = max (maxheight, a [I]); 28 for (int I = a [1]; I <= maxheight; I ++) 29 dp [1] [I] = (I-a [1]) * (I-a [1]); 30 31 for (int I = 2; I <= n; I ++) // enumerate all trees 32 for (int j = a [I]; j <= maxheight; j ++) // enumerate the height of this tree 33 for (int k = a [I-1]; k <= maxheight; k ++) // enumerate the height of the previous tree 34 dp [I] [j] = min (dp [I] [j], 35 (j-a [I]) * (j-a [I]) + (dp [I-1] [k]) + abs (j-k) * C); 36 37 38 int ans = 0x7fffff; 39 for (int I = a [n]; I <= maxheight; I ++) 40 ans = min (ans, dp [n] [I]); 41 printf ("% d", ans); 42 return 0; 43}View Code
Then, simplify the formula.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 using namespace std; 6 const int MAXN = 300005; 7 const int INF = 0x7fffff; 8 const int maxheight = 100; 9 int dp [301]; // the I-th tree, the minimum cost of the height is 10 int f [301]; 11 int n, C; 12 int a [MAXN]; 13 int bgsum [MAXN]; 14 int edsum [MAXN]; 15 int main () {16 scanf ("% d", & n, & C); 17 for (int I = 0; I <n; I ++) 18 scanf ("% d", & a [I]); 19 memset (dp, 0x3f, sizeof (dp); 20 for (int I = a [0]; I <= maxheight; I ++) 21 dp [I] = (I-a [0]) * (I-a [0]); 22 23 for (int I = 1; I <n; I ++) {// enumerate all trees 24 memcpy (f, dp, sizeof (dp )); 25 for (int j = 0; j <= maxheight; j ++) dp [j] = bgsum [j] = edsum [j] = INF; 26 27 bgsum [0] = f [0]; 28 for (int j = 1; j <= maxheight; j ++) 29 bgsum [j] = min (bgsum [J-1], f [j]-C * j); 30 31 edsum [maxheight] = f [maxheight] + maxheight * C; 32 for (int j = maxheight-1; j> = 0; j --) 33 edsum [j] = min (edsum [j + 1], f [j] + C * j); 34 35 for (int j = a [I]; j <= maxheight; j ++) // enumerate the height of this tree 36 dp [j] = min (edsum [j]-C * j, bgsum [j] + C * j) + (j-a [I]) * (j-a [I]); 37} 38 int ans = 0x7fffff; 39 for (int I = a [n-1]; I <= maxheight; I ++) 40 ans = min (ans, dp [I]); 41 printf ("% d", ans); 42 return 0; 43}View Code