[BZOJ1721][Usaco2006 Mar]Ski Lift 纜車支柱
Description
Farmer Ron in Colorado is building a ski resort for his cows (though budget constraints dictate construction of just one ski lift). The lift will be constructed as a monorail and will connect a concrete support at the starting location to the support at the ending location via some number of intermediate supports, each of height 0 above its land. A straight-line segment of steel connects every pair of adjacent supports. For obvious reasons, each section of straight steel must lie above the ground at all points. Always frugal, FR wants to minimize the number of supports that he must build. He has surveyed the N (2 <= N <= 5,000) equal-sized plots of land the lift will traverse and recorded the integral height H (0 <= H <= 1,000,000,000) of each plot. Safety regulations require FR to build adjacent supports no more than K (1 <= K <= N - 1) units apart. The steel between each pair of supports is rigid and forms a straight line from one support to the next. Help FR compute the smallest number of supports required such that: each segment of steel lies entirely above (or just tangent to) each piece of ground, no two consecutive supports are more than K units apart horizontally, and a support resides both on the first plot of land and on the last plot of land.
科羅拉州的羅恩打算為他的奶牛們建造一個滑雪場,雖然需要的設施僅僅是一部纜車.建造一部纜車,需要從山腳到山頂立若幹根柱子,並用鋼絲連結它們.你可以認為相對於地面,柱子的高度可以忽略不計.每相鄰兩根柱子間都有鋼絲直接相連.顯然,所有鋼絲的任何一段都不能在地面之下. 為了節省建造的費用,羅恩希望在工程中修建盡可能少的柱子.他在準備修建纜車的山坡上叠定了N(2≤N≤5000)個兩兩之間水平距離相等的點,並且測量了每個點的高度H(O≤日≤10^9).並且,按照國家安全標準,相鄰兩根柱子間的距離不能超過K(1≤K≤N-1)個單位長度.柱子間的鋼絲都是筆直的. 羅恩希望你幫他計算一下,在滿足下列條件的情況下,他至少要修建多少根柱子:首先,所有的柱子都必須修建在他所選定的點上,且每一段鋼絲都必須高於地面或者正好跟地面相切.相鄰兩根柱子的距離不大於K個單位長度.當然,在第一個點與最後一個點上一定都要修建柱子.
Input
* Line 1: Two space-separate integers, N and K
* Lines 2..N+1: Line i+1 contains a single integer that is the height of plot i.
第1行:兩個整數N和K,用空格隔開.
第2到N+1行:每行包括一個正整數,第i+l行的數描述了第i個點的高度.
Output
* Line 1: A single integer equal to the fewest number of lift towers FR needs to build subject to the above constraints
輸出一個整數,即羅恩最少需要修建的柱子的數目.
Sample Input
13 40
1
0
2
4
6
8
6
8
8
9
11
12
Sample Output
5 開心做dp系列。。也沒啥好說的,然而double搞錯了讓我多交了兩發 代碼:1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define inf 1e9 5 using namespace std; 6 int n,k; 7 int h[5001],f[5001]; 8 int main() 9 { 10 cin>>n>>k; 11 for(int i=1;i<=n;i++) cin>>h[i]; 12 memset(f,0x3f,sizeof(f)); 13 f[1]=1; 14 for(int i=1;i<=n;i++) 15 { 16 double maxn=-inf; 17 for(int j=i+1;j<=min(i+k,n);j++) 18 { 19 double now=(double)(h[j]-h[i])/(j-i); 20 if(now>=maxn) 21 { 22 f[j]=min(f[j],f[i]+1); 23 maxn=now; 24 } 25 } 26 } 27 cout<<f[n]; 28 return 0; 29 }
[BZOJ1721][Usaco2006 Mar]Ski Lift 纜車支柱