HDU 2037:今年暑假不AC

来源:互联网 发布:勇者之塔数据大全 编辑:程序博客网 时间:2024/06/05 18:41

贪心的题目

按照持续时间去排序

时间相等则开始时间靠前的放在前面

放一个将对应的时间区间全部置1

注意判断的时候并不需要去判断区间端点的情况


看能不能再A一道,不行就回去开撸了 =  =、

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>using namespace std;struct show{int st;int et;}h[1111];bool cmp(show a,show b){    if(a.et-a.st==b.et-b.st)        return  a.st<b.st;    return a.et-a.st<b.et-b.st;}int main(){   int t,i,j;   int time[1111];   while(~scanf("%d",&t))   {    if(t==0)break;      for(i=0;i<t;i++)        scanf("%d%d",&h[i].st,&h[i].et);    sort(h,h+t,cmp);    int ans=0;    memset(time,0,sizeof(time));    int flag;    for(i=0;i<t;i++)    {//        if(h[i].et-h[i].st==1)//           if(time[h[i].st]==1&&time[h[i].et]==1)&&time[h[i].st-1]==1||time[h[i].et+1]==1)//                flag=1;break;            flag=0;            for(j=h[i].st;j<=h[i].et;j++)                {                if(j==h[i].st||j==h[i].et)                    continue;                else if(time[j]==1)                    {flag=1;break;}                else                    continue;                }            if(flag==0)                {                    for(j=h[i].st;j<=h[i].et;j++)                        time[j]=1;                    ans++;                }    }//    for(i=0;i<t;i++)//        printf("%d",time[i]);//    printf("\n");    printf("%d\n",ans);   }    return 0;}


0 0
原创粉丝点击