博文持续更新中,做题过程中进行总结。
(1)hdu 2037今年暑假不AC http://acm.hdu.edu.cn/showproblem.php?pid=2037
思路:贪心策略就是尽量的留下更多的时间给下一个节目,所以把节目结束时间升序排一遍,然后遍历,合适的就留下,不合适冲突的就舍弃掉!
代码:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
};
bool cmp(node a,node b)
{
return a.y<b.y;
}
int main()
{
int n;while(cin>>n){
if(n==0) break;
node *a=new node[n];
for(int i=0;i<n;i++) cin>>a[i].x>>a[i].y;
sort(a,a+n,cmp);
int ans=0;
int temp=0;
for(int i=0;i<n;i++)
if(a[i].x>=temp){
ans++;
temp=a[i].y;
}
cout<<ans<<endl;
}
}