Contents
  1. 1. 题目:
  2. 2. 输入:
  3. 3. 输出:
  4. 4. sample input
  5. 5. sample output
  6. 6. 思路:

题目:

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

输入:

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

输出:

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

sample input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

sample output

5

思路:

贪心+快排

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

//hdu2037
按结束时间从小到大排序,然后用结束时间和下一个的开始时间对比,找到结束时间比
开始时间小的节目
#include<stdio.h>
#include<stdlib.h> //用qsort()函数
struct jiemu
{
int x,y;
}p[1000];

int cmp(const void *a, const void *b)
{

return (*(struct jiemu*)a).y-(*(struct jiemu*)b).y;//比较其大小
}
//也可以这样reuturn ((struct jiemu *)a)->y - ((struct jiemu *)b)->y

int main()
{

int n,i,j,count;
while(scanf("%d",&n)!=EOF&&n!=0)
{
count=1;
for(i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
qsort(p,n,sizeof(struct jiemu),cmp);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].y<=p[j].x)
{
count++;
i=j-1; //break后i++,所以i=j-1,后i会自己加一
break;
}
}
}
printf("%d\n",count);
}
return 0;
}
Contents
  1. 1. 题目:
  2. 2. 输入:
  3. 3. 输出:
  4. 4. sample input
  5. 5. sample output
  6. 6. 思路: