hdu5714

来源:互联网 发布:英语书籍阅读软件 编辑:程序博客网 时间:2024/05/05 03:48

先开始我是思考在每行的情况忽略了小明这个关键因素,这题的关键就是注意一个技巧将分散的变量集中在一个点上这是一个常用技巧。。

注意在离散化话一下。

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int dir[2][40000],all[40000];struct pos{    int kind,addkind,p;};int t,n,tot;pos q[80050];bool com(pos a,pos b){  return a.p<b.p;}int main(){    scanf("%d",&t);    int k=1;    while(t--)    {        tot=0;       scanf("%d",&n);       while(n--)       {           int x,y,z,d;           scanf("%d%d%d%d",&x,&y,&z,&d);           if(z*2<(y-x))continue;           if(d==1)           {               q[tot].p=y-z;               q[tot].kind=1;               q[tot++].addkind=1;               q[tot].p=x+z+1;               q[tot].kind=1;               q[tot++].addkind=-1;           }           else           {               q[tot].p=x+z;               q[tot].kind=0;               q[tot++].addkind=1;               q[tot].p=y-z-1;               q[tot].kind=0;               q[tot++].addkind=-1;           }       }           sort(q,q+tot,com);           int endpos=0;           for(int i=0;i<tot;i++)           {               if(i!=0&&q[i].p>q[i-1].p)                endpos++;               dir[q[i].kind][endpos]+=q[i].addkind;           }           int ans=0;           for(int i=0;i<=endpos;i++)           {               ans+=dir[1][i];               all[i]=ans;           }           int maxx=-1;           ans=0;           for(int i=endpos;i>=0;i--)           {               ans+=dir[0][i];               maxx=max(ans,maxx);               all[i]+=maxx;           }           maxx=-1;           for(int i=0;i<=endpos;i++)            maxx=max(all[i],maxx);            printf("Case #%d:\n%d\n",k++,maxx);           for(int i=0;i<=endpos;i++)            {                all[i]=0;                dir[0][i]=0;                dir[1][i]=0;            }       }         return 0;    }

原创粉丝点击