HDU 4258(Covered Walkway-斜率优化)

Covered Walkway

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1273    Accepted Submission(s): 491


Problem Description
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 atx to a point at y, they will charge c+(x-y)2, wherec is a constant. Note that it is possible for x=y. If so, then the contractor would simply chargec.
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), wheren is the number of points which must be covered, and c is the contractor’s constant. Each of the followingn 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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  4257 4260 4261 4262  
 


设f[i]表示仅仅考虑前n个的最小费用

显然f[i]=f[j-1]+c+sqr(a[i]-a[j])

若选i比选j优(i<j)

f[i-1]+sqr(a[i]-a[y])+c<f[j-1]+sqr(a[j]-a[y])+c

f[i-1]+sqr(a[i])-f[j-1]-sqr(a[j])

-----------------------------------  > 2*a[y]

 a[i]-a[j]

斜率优化。






#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
#define eps 1e-13
#define Read(x) { 
				  while (!isdigit(c=getchar()));  
				  x=c-48; 
				  while (isdigit(c=getchar())) x=x*10+c-48; 
				}
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
ll cost; 
char c;
ll a[MAXN],f[MAXN];
struct P
{
	int i;
	long double x,y;
	P(int _i,ll _x,ll _y):i(_i),x(_x),y(_y){}
	P(ll _x,ll _y):x(_x),y(_y){}
	P(){}
	friend long double kk(P a,P b){if (abs(a.x-b.x)<eps) return (b.y-a.y)*INF;return (b.y-a.y)/(b.x-a.x);	}
}st[MAXN];
struct V
{
	long double x,y;
	V(ll _x,ll _y):x(_x),y(_y){}
	V(){}
	V(P a,P b):x(b.x-a.x),y(b.y-a.y){}
	friend long double operator*(V a,V b){return a.x*b.y-a.y*b.x;	}
};
int main()
{
	freopen("B.in","r",stdin);
	
	while (1)
	{
		scanf("%d%lld",&n,&cost);
		if (!n) break;
		For(i,n)
			Read(a[i])
		
		f[0]=0;
		int head=1,tail=1;
		st[1]=P(1,a[1],a[1]*a[1]);
		Fork(i,1,n)
		{
			P A=P(i,a[i],f[i-1]+a[i]*a[i]);
			while (head^tail&&V(st[tail-1],st[tail])*V(st[tail],A)<=0) tail--;
			st[++tail]=A;
			
			while (head^tail&&kk(st[head],st[head+1])<2*a[i]) head++;
			int k=st[head].i;
			f[i]=f[k-1]+cost+(a[k]-a[i])*(a[k]-a[i]);
		}
		cout<<f[n]<<endl;
	

	}
	return 0;
}




原文地址:https://www.cnblogs.com/cynchanpin/p/7120836.html