| 火影忍者之~大战之后 | ||||||
| ||||||
| Description | ||||||
|
经历了大战的木叶村现在急需重建,人手又少,所以需要尽可能多的接受外来的任务,以赚取报酬,重建村庄,假设你现在是木叶的一名高级忍者,有一大堆的任务等着你来做,但毕竟个人时间有限,所以没办法将所有的任务都做了,而只能尽可能的多。
| ||||||
| 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 | ||||||
| Source | ||||||
| 2012 Spring Contest 3 - STL | ||||||
| Author | ||||||
| 拂晓 |
解题思路:对所有任务重新排序,越早结束的越在前面
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
struct node
{
int s,e;
}x[200];
bool cmp(node x,node y)
{
return x.e<y.e;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
scanf("%d %d",&x[i].s,&x[i].e);
sort(x+1,x+1+n,cmp);
int sum=1;
int k=x[1].e;
for(int i=2;i<=n;i++)
{
if(x[i].s>=k)
{
sum++;
k=x[i].e;
}
}
printf("%d\n",sum);
}
return 0;
}

本文介绍了一个基于《火影忍者》背景的任务调度问题,通过合理安排任务的执行顺序来最大化完成的任务数量。采用结束时间优先的策略进行任务排序,并提供了一段C++代码实现。



474

被折叠的 条评论
为什么被折叠?



