zoukankan      html  css  js  c++  java
  • UValive4195 Heroes of Money and Magic

    斜率优化

    想骂人了,马格吉最后调了半小时

    TMD造数据的人是SB吧?

    我写  while(scanf("%d%d",&n,&m)!=EOF&&n)

    然后就TMD无限WA...WA...WA...

    尼玛 改成while(scanf("%d%d",&n,&m),n)

    就过了,就过了!!!

    沃日,浪费我时间是吧,坑爹是吧

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<iostream>
     4 #include<string>
     5 #include<cstring>
     6 #define Clear(x,i) memset(x,i,sizeof(x))
     7 #define re(i,l,r) for(int i=(l);i<=(r);i++)
     8 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
     9 using namespace std;
    10 template <typename Q>
    11 void inin(Q &ret)
    12 {
    13     ret=0;int f=0;char ch=getchar();
    14     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
    15     while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
    16     ret=f?-ret:ret;
    17 }
    18 int n,m,a[5010],sum[5010],cost[5010],x[5010][2],y[5010][2],num[2];
    19 int f[5010][2],c;
    20 int cross(int a,int b,int xx,int yy)
    21 {
    22     return (x[b][c]-x[a][c])*(yy-y[a][c])-
    23            (xx-x[a][c])*(y[b][c]-y[a][c]);
    24 }
    25 int main()
    26 {
    27     while(scanf("%d%d",&n,&m),n)
    28     {
    29         m++;
    30         for(int i=1;i<=n;i++)
    31         {
    32             scanf("%d",&a[i]);
    33             sum[i]=sum[i-1]+a[i];
    34             cost[i]=cost[i-1]+sum[i-1]*a[i];
    35         }
    36         num[1]=0;
    37         c=1;
    38         x[1][1]=y[1][1]=0;
    39         for(int j=1;j<=m;j++)
    40         {
    41             c^=1;
    42             num[c]=0;
    43             for(int i=1,k=1;i<=n;i++)
    44             {
    45                 while(k<num[!c]&&(x[k+1][!c]-x[k][!c])*sum[i]>y[k+1][!c]-y[k][!c])k++;
    46                 f[i][c]=-sum[i]*x[k][!c]+y[k][!c]+cost[i];
    47                 int xx=sum[i],yy=f[i][c]+xx*xx-cost[i];
    48                 while(1<num[c]&&cross(num[c]-1,num[c],xx,yy)<=0)num[c]--;
    49                 x[++num[c]][c]=xx;
    50                 y[num[c]][c]=yy;
    51             }
    52         }
    53         printf("%d
    ",f[n][c]);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    一个好的时间函数
    Codeforces 785E. Anton and Permutation
    Codeforces 785 D. Anton and School
    Codeforces 510 E. Fox And Dinner
    Codeforces 242 E. XOR on Segment
    Codeforces 629 E. Famil Door and Roads
    Codeforces 600E. Lomsat gelral(Dsu on tree学习)
    Codeforces 438D The Child and Sequence
    Codeforces 729E Subordinates
    【ATcoder】D
  • 原文地址:https://www.cnblogs.com/HugeGun/p/5343167.html
Copyright © 2011-2022 走看看