hdu 2993

来源:互联网 发布:mac下的数据库软件 编辑:程序博客网 时间:2024/05/01 20:42

斜率优化dp..

听着吓人其实是傻逼题。。转化转化维护一个下凸的东西是o(n)的?

反正有人证明了。。

还有这种大量输入要开读入挂。。。吧

贴代码。

#include<stdio.h>  #define LL long long  typedef struct  {      int x,y;  }Node;  Node pp[100010];  Node tmp,now; double res; int sum[100010]; int head,tail;  LL multi(Node a,Node b,Node c)  {      return (LL)(b.x-a.x)*(c.y-a.y) - (LL)(b.y-a.y)*(c.x-a.x) ;  }  void read(int &r) {      char c;      while (c = getchar(), c < '0' || c > '9');      r = c - '0';      while (c = getchar(), c >= '0' && c <= '9') r = r * 10 + c - '0';  }  int main()  {      int n,k,i,j; double tt;      while(~scanf("%d%d",&n,&k))      {          sum[0]=0; head=tail=0; tt=res=0.0;          for(i=1;i<=n;i++)          {              read(sum[i]);            sum[i]+=sum[i-1];          }          for(i=k;i<=n;i++)          {              tmp.x=i-k;              tmp.y=sum[i-k];              while( tail-head>=2 && multi(pp[tail-2],pp[tail-1],tmp)<=0 )                tail--;              pp[tail]=tmp; tail++;            now.x=i; now.y=sum[i];              while(tail-head>=2 && multi(pp[head],pp[head+1],now)>=0 )                head++;              tt=(double)(now.y-pp[head].y)/(now.x-pp[head].x);              res= tt>res?tt:res;          }          printf("%.2lf\n",res);      }      return 0;  }  


0 0
原创粉丝点击