HRBUST1315 火影忍者之~大战之后 2017-03-06 16:14 54人阅读 评论(0) 收藏

时间:2023-12-15 16:13:38

火影忍者之~大战之后

经历了大战的木叶村现在急需重建,人手又少,所以需要尽可能多的接受外来的任务,以赚取报酬,重建村庄,假设你现在是木叶的一名高级忍者,有一大堆的任务等着你来做,但毕竟个人时间有限,所以没办法将所有的任务都做了,而只能尽可能的多。

HRBUST1315 火影忍者之~大战之后                                                                                            2017-03-06 16:14             54人阅读              评论(0)              收藏

Input
每组数据包括一个整数n,表示分配给你的任务总数,然后n行,每行两个整数,分别表示任务开始以及结束时间,输入到0结束。n不超过100,每个时间值不超过1000
Output
对每组数据,输出能够执行的最多任务数。
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

—————————————————————————————————————

贪心 按结束时间排序,每次如果开始时间在当前结束时间之后的就做

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
using namespace std; struct node
{
int st,ed;
} s[105]; bool cmp(node a,node b)
{
if(a.ed!=b.ed)
return a.ed<b.ed;
return a.st>a.st;
} int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&s[i].st,&s[i].ed);
}
sort(s,s+n,cmp);
int cnt=0;
int e=0;
for(int i=0;i<n;i++)
{
if(s[i].st>=e)
{
cnt++;
e=s[i].ed;
}
}
printf("%d\n",cnt);
} return 0;
}