首页 > 代码库 > BNUOJ 26224 Covered Walkway
BNUOJ 26224 Covered Walkway
Covered Walkway
Time Limit: 10000ms
Memory Limit: 131072KB
This problem will be judged on HDU. Original ID: 425864-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?
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 5000123456710112456078999010190 0
Sample Output
30726
Source
The University of Chicago Invitational Programming Contest 2012
解题:晚上再写一下思路,详细地写一下思路
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 long14 #define INF 0x3f3f3f3f15 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。