1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3006 Solved: 1360
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
很容易写出斜率式什么的就不说了。。
不开long long见祖宗。。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstdio> 5 6 #define maxn 1000001 7 8 using namespace std; 9 10 long long sum[maxn],f[maxn]; 11 12 int a,b,c,n,que[maxn],head=1,tail=1; 13 14 double K(int x,int y){return (double)(sum[x]+sum[y])/2.0+(double)(f[y]-f[x])/(2.0*a*(sum[y]-sum[x]))-(double)b/(2.0*a);} 15 16 long long F(int x){return a*x*x+b*x+c;} 17 18 void DP() 19 { 20 que[tail]=0; 21 for(int i=1;i<=n;i++) 22 { 23 while(head<tail && sum[i] >= K(que[head],que[head+1]))head++; 24 int sd=que[head]; 25 f[i]=f[sd]+F(sum[i]-sum[sd]); 26 while(head<tail && K(que[tail],i) <= K(que[tail-1],que[tail]))tail--; 27 que[++tail]=i; 28 } 29 printf("%lld",f[n]); 30 } 31 32 int main() 33 { 34 scanf("%d%d%d%d",&n,&a,&b,&c); 35 for(int i=1;i<=n;i++)scanf("%lld",&sum[i]),sum[i]+=sum[i-1]; 36 DP(); 37 return 0; 38 }