各种蕴含算法思想的DP - 3

 

https://www.luogu.org/problemnew/show/P3628

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <time.h>
 6 #include <string>
 7 #include <set>
 8 #include <map>
 9 #include <list>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <bitset>
14 #include <ext/rope>
15 #include <algorithm>
16 #include <iostream>
17 using namespace std;
18 #define ll long long
19 #define minv 1e-6
20 #define inf 1e9
21 #define pi 3.1415926536
22 #define nl 2.7182818284
23 const ll mod=1e9+7;//998244353
24 const int maxn=1e6+10;
25 
26 ll x[maxn],a,b,c;
27 ll sum[maxn],f[maxn],q[maxn];
28 
29 double slope(int j,int k)
30 {
31     return 1.0*(f[j]-f[k]-a*(sum[k]-sum[j])*(sum[k]+sum[j])+b*(sum[k]-sum[j]))
32         /(sum[j]-sum[k]);
33 }
34 
35 int main()
36 {
37     int n,i,j,l,r;
38     scanf("%d",&n);
39     scanf("%lld%lld%lld",&a,&b,&c);
40     for (i=1;i<=n;i++)
41         scanf("%lld",&x[i]);
42     sum[0]=0;
43     f[1]=0;
44     q[1]=0;
45     l=1,r=1;
46     for (i=1;i<=n;i++)
47     {
48         sum[i]=sum[i-1]+x[i];
49         while (l<r && slope(q[l],q[l+1])>=2*a*sum[i])
50             l++;
51         j=q[l];
52         f[i]=f[j]+a*(sum[i]-sum[j])*(sum[i]-sum[j])+b*(sum[i]-sum[j])+c;
53         while (l<r && slope(q[r-1],q[r])<slope(q[r],i))
54             r--;
55         q[++r]=i;
56     }
57     printf("%lld",f[n]);
58     return 0;
59 }

 

斜率dp+二分

https://blog.csdn.net/litble/article/details/76917097

待写

posted @ 2018-08-31 16:52  congmingyige  阅读(254)  评论(0编辑  收藏  举报