码迷,mamicode.com
首页 > 其他好文 > 详细

BNUOJ 26224 Covered Walkway

时间:2014-08-15 14:14:28      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   strong   

Covered Walkway

Time Limit: 10000ms
Memory Limit: 131072KB
This problem will be judged on HDU. Original ID: 4258
64-bit integer IO format: %I64d      Java class name: Main
 
Your university wants to build a new walkway, and they want at least part of it to be covered. There are certain points which must be covered. It doesn’t matter if other points along the walkway are covered or not. 
The building contractor has an interesting pricing scheme. To cover the walkway from a point at x to a point at y, they will charge c+(x-y)2, where c is a constant. Note that it is possible for x=y. If so, then the contractor would simply charge c
Given the points along the walkway and the constant c, what is the minimum cost to cover the walkway?
 

Input

There will be several test cases in the input. Each test case will begin with a line with two integers, n (1≤n≤1,000,000) and c (1≤c≤109), where n is the number of points which must be covered, and c is the contractor’s constant. Each of the following n lines will contain a single integer, representing a point along the walkway that must be covered. The points will be in order, from smallest to largest. All of the points will be in the range from 1 to 109, inclusive. The input will end with a line with two 0s.
 

Output

For each test case, output a single integer, representing the minimum cost to cover all of the specified points. Output each integer on its own line, with no spaces, and do not print any blank lines between answers. All possible inputs yield answers which will fit in a signed 64-bit integer.
 

Sample Input

10 5000
1
23
45
67
101
124
560
789
990
1019
0 0

Sample Output

30726

Source

 
解题:晚上再写一下思路,详细地写一下思路
 
 
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxn = 1000100;
17 LL n,c,d[maxn],dp[maxn];
18 int q[maxn],head,tail;
19 LL G(int k,int j){
20     return dp[j] + d[j+1]*d[j+1] - (dp[k]+d[k+1]*d[k+1]);
21 }
22 LL S(int k,int j){
23     return (d[j+1]-d[k+1]);
24 }
25 int main(){
26     int i,j;
27     while(scanf("%I64d %I64d",&n,&c),(n||c)){
28         for(i = 1; i <= n; i++)
29             scanf("%I64d",d+i);
30         dp[0] = q[0] = 0;
31         head = tail = 0;
32         for(i = 1; i <= n; i++){
33             while(head < tail && G(q[head],q[head+1]) <= 2*d[i]*S(q[head],q[head+1])) head++;
34             dp[i] = dp[q[head]] + (d[i]-d[q[head]+1])*(d[i]-d[q[head]+1])+c;
35             while(head < tail && G(q[tail-1],q[tail])*S(q[tail],i) >= G(q[tail],i)*S(q[tail-1],q[tail])) tail--;
36             q[++tail] = i;
37         }
38         printf("%I64d\n",dp[n]);
39     }
40     return 0;
41 }
View Code

 

 

BNUOJ 26224 Covered Walkway,布布扣,bubuko.com

BNUOJ 26224 Covered Walkway

标签:style   blog   http   color   java   os   io   strong   

原文地址:http://www.cnblogs.com/crackpotisback/p/3914592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!