题意很清晰,在给定的时间段中选出 最多的不交叉的时间段,很裸的贪心策略;
思路:直接按时间段右端点排序,然后从第一个开始选不交叉的区间即可。
下面是代码:
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 |
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<cmath> #include<ctype.h> using namespace std; typedef long long LL; //typedef pair<int ,int> P; //const int maxn=100000; struct rec { int s; int e; }ans[105]; bool cmp(rec a,rec b) { return a.e<b.e; } int main() { int n; while(~scanf("%d",&n)&&n) { int sum=1; for(int i=0;i<n;i++) scanf("%d%d",&ans[i].s,&ans[i].e); sort(ans,ans+n,cmp); for(int i=1,k=0;i<n;i++) { if(ans[k].e<=ans[i].s) {sum++;k=i;} } printf("%d\n",sum); } return 0; } |