卧槽我感觉写的是对的,但是就是样例都过不了。。。留坑
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define LL long long using namespace std; const int maxx = 5005; struct node{ int h,w; }p[maxx]; LL dp[maxx][2005]; LL sums[maxx]; LL sumw[maxx]; LL que[maxx]; bool cmp(node a,node b){ return a.h<b.h; } LL Y(int k,int j){ return dp[k][j-1]-sums[k]+(LL)sums[k]*p[k+1].h; } LL KY(int j,int k1,int k2){ return Y(k1,j)-Y(k2,j); } LL X(int k){ return p[k+1].h; } LL KX(int i,int j){ return X(i)-X(j); } int main(){ int n,K; while(~scanf("%d%d",&n,&K)){ sums[0]=0; sumw[0]=0; for (int i=1;i<=n;i++){ scanf("%d%d",&p[i].w,&p[i].h); } memset(dp,0,sizeof(dp)); sort(p+1,p+1+n,cmp); for (int i=1;i<=n;i++){ sums[i]=(LL)sums[i-1]+p[i].w*p[i].h; sumw[i]=(LL)sumw[i-1]+p[i].w; dp[i][1]=sums[i]-(LL)sumw[i]*p[1].h; } int l,r; l=r=1; for (int j=2;j<=K;j++){ l=r=1; que[1]=0; dp[j][j]=0; for (int i=1;i<=n;i++){ while(l<r && KY(j,que[l+1],que[l])<=sumw[i]*KX(que[l+1],que[l]))l++; dp[i][j]=dp[que[l]][j-1]+sums[i]-sums[que[l]]-(sumw[i]-sumw[que[l]])*p[que[l]+1].h; while(l<r && KY(j,que[r],que[r-1])*KX(i,que[r])>=KY(j,i,que[r])*KX(que[r],que[r-1]))r--; que[++r]=i; } } // for (int i=1;i<=n;i++){ // for (int j=1;j<=K;j++){ // cout<<dp[i][j]<<" "; // } // } printf("%lld ",dp[n][K]); } return 0; }