首页 > 代码库 > hdu 3507 Print Article

hdu 3507 Print Article

这个题的式子比较好推,

然而还是要吐槽,要把除法换成乘法,而且这个题可能出现斜率相等的情况,而且,不能直接用double判断,精度有问题,本蒟蒻WAWAWAWAWA,,,,得出的惨痛教训。。

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define lowbit(x) x&(-x)
 4 #define inf 0x3f3f3f3f
 5 #define eps 1e-5
 6 #define N 500005
 7 using namespace std;
 8 inline int ra()
 9 {
10     int x=0,f=1; char ch=getchar();
11     while (ch<0 || ch>9) {if (ch==-) f=-1; ch=getchar();}
12     while (ch>=0 && ch<=9) {x=x*10+ch-0; ch=getchar();}
13     return x*f;
14 }
15 int n,M,sum[N],f[N],q[N];
16 int squ(int a)
17 {
18     return a*a;
19 }
20 int up(int a, int b)
21 {
22     return (f[b]+squ(sum[b])-f[a]-squ(sum[a]));
23 }
24 int down(int a, int b)
25 {
26     return (2*(sum[b]-sum[a]));
27 }
28 int main(int argc, char const *argv[])
29 {
30     while (scanf("%d%d",&n,&M)!=EOF)
31     {
32         for (int i=1; i<=n; i++) sum[i]=sum[i-1]+ra();
33         int l=0,r=0;
34         for (int i=1; i<=n; i++)
35         {
36             while (l<r && up(q[l],q[l+1])<=sum[i]*down(q[l],q[l+1])) l++;
37             f[i]=f[q[l]]+squ(sum[i]-sum[q[l]])+M;
38             while (l<r && up(q[r-1],q[r])*down(q[r],i)>=up(q[r],i)*down(q[r-1],q[r])) r--;
39             q[++r]=i;
40         }
41         cout<<f[n]<<endl;
42     }
43     return 0;
44 }

 

hdu 3507 Print Article