主要学习sort的用法和贪心法
测试例:
11
1 4
3 5
5 7
0 6
3 8
5 9
8 11
6 10
8 12
2 13
12 14
#include <iostream>
#include <algorithm> //sort函数的头文件
using namespace std;
struct action{
int s; //起始时间
int f; //终止时间
int index; //活动编号
};
action a[1010];
int n;
//把const去掉也不报错
bool comp(const action &a,const action &b)
{
return a.f <= b.f ? true: false;
}
int forjob()
{
int num = 1; //第一个活动是必选
sort(a, a + n, comp); //根据终止时间排序
int temp = 0;
cout<<a[0].index;
for(int i = 1; i < n; i++){
//如果当前活动的开始时间在上一个被选活动结束之后开始
if(a[i].s >= a[temp].f){
num++;
temp = i;
cout<<" "<<a[temp].index;
}
}
cout<<endl;
return num;
}
int main()
{
//freopen("in.txt", "r", stdin);
int i = 0;
cin>>n;
for(i = 0; i < n; i++){
cin>>a[i].s>>a[i].f;
a[i].index = i;
}
forjob();
return 0;
}