1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=20005; int n,K,Q[maxn]; LL x[maxn],y[maxn],f[maxn][105],ans=0; double Slope(int j,int k,int con) { if(y[j+1]-x[j+1]==y[k+1]-x[k+1])return -1e18; return (double)(f[j][con]-j*y[j+1]+j*x[j+1]-j-(f[k][con]-k*y[k+1]+k*x[k+1]-k))/(y[j+1]-x[j+1]-(y[k+1]-x[k+1])); } int main() { n=Get_Int(); K=Get_Int(); for(int i=1; i<=n; i++) { x[i]=Get_Int(); y[i]=Get_Int(); } reverse(x+1,x+n+1); reverse(y+1,y+n+1); for(int k=1; k<=K; k++) { int Left=1,Right=1; Q[1]=0; for(int i=1; i<=n; i++) { while(Left<Right&&Slope(Q[Left],Q[Left+1],k-1)>=-i)Left++; int j=Q[Left]; f[i][k]=f[j][k-1]+(y[j+1]-x[j+1]+1)*(i-j); ans=max(ans,f[i][k]); while(Left<Right&&Slope(Q[Right-1],Q[Right],k-1)<=Slope(Q[Right],i,k-1))Right--; Q[++Right]=i; } } printf("%lld\n",ans); return 0; }
|