今天做hdu 1556題的時候,覺得與此題一個大牛這題思想一樣就找出這題的代碼
//大牛的代碼
[cpp]
#include <cstdio>
#include <cstring>
int s[1445];
int main()
{
int n;
while (scanf("%d",&n) != EOF)
{
memset(s,0,sizeof(s));
int a,b,c,d;
while (n--)
{
scanf("%d:%d %d:%d",&a,&b,&c,&d);
s[a*60+b] += 1;
s[c*60+d] -= 1;
}
int ans = 0;
int ss = 0;
for (int i = 0;i < 1440;i++)
{
ss += s[i];
if (!ss)
ans++;
}
printf("%d\n",ans);
}
}
#include <cstdio>
#include <cstring>
int s[1445];
int main()
{
int n;
while (scanf("%d",&n) != EOF)
{
memset(s,0,sizeof(s));
int a,b,c,d;
while (n--)
{
scanf("%d:%d %d:%d",&a,&b,&c,&d);
s[a*60+b] += 1;
s[c*60+d] -= 1;
}
int ans = 0;
int ss = 0;
for (int i = 0;i < 1440;i++)
{
ss += s[i];
if (!ss)
ans++;
}
printf("%d\n",ans);
}
}
//自己的代碼
[cpp]
#include<stdio.h>
#include<stdlib.h>
#define N 500005
struct op
{
int b,e;
}p[N],cur;
int cmp(const void *a,const void *b)
{
struct op *c,*d;
c=(struct op *)a;
d=(struct op *)b;
if(c->b==d->b)
return c->e-d->e;
return c->b-d->b;
}
int main()
{
int i,n,sum,j,a,b,c,d;
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<n;i++)
{
scanf("%d:%d %d:%d",&a,&b,&c,&d);
a=a*60+b;c=c*60+d;
p[i].b=a;p[i].e=c;
}
p[n].b=24*60;p[n].e=24*60;
qsort(p,n,sizeof(p[0]),cmp);
sum=p[0].b;
cur=p[0];
for(i=1;i<=n;i++)
{
if(p[i].b>=cur.e)
{
sum=sum+p[i].b-cur.e;
}
if(cur.e<p[i].e)
{cur.e=p[i].e;}
}
printf("%d\n",sum);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define N 500005
struct op
{
int b,e;
}p[N],cur;
int cmp(const void *a,const void *b)
{
struct op *c,*d;
c=(struct op *)a;
d=(struct op *)b;
if(c->b==d->b)
return c->e-d->e;
return c->b-d->b;
}
int main()
{
int i,n,sum,j,a,b,c,d;
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<n;i++)
{
scanf("%d:%d %d:%d",&a,&b,&c,&d);
a=a*60+b;c=c*60+d;
p[i].b=a;p[i].e=c;
}
p[n].b=24*60;p[n].e=24*60;
qsort(p,n,sizeof(p[0]),cmp);
sum=p[0].b;
cur=p[0];
for(i=1;i<=n;i++)
{
if(p[i].b>=cur.e)
{
sum=sum+p[i].b-cur.e;
}
if(cur.e<p[i].e)
{cur.e=p[i].e;}
}
printf("%d\n",sum);
}
return 0;
}