會場安排問題 nyoj
阿新 • • 發佈:2018-11-20
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,ans; struct node{ int B,E; }a[10001]; /*超時 int book[1001]; void dfs(int t,int count,int end); int main(){ scanf("%d",&n); while(n--){ scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d%d",&a[i].B,&a[i].E); } memset(book,0,sizeof(0)); dfs(0,0,0); cout<<ans<<endl; } return 0; } void dfs(int t,int count,int end){ if(ans<count){ ans=count; } if(t>=m){ return ; } for(int i=t;i<m;i++){ if(a[i].B>=(end+1)&&(!book[i])){ book[i]=1; dfs(i+1,count+1,a[i].E); book[i]=0; } } return ; } */ int com(struct node a,struct node b){ return a.E<b.E; } int main(){ scanf("%d",&n); while(n--){ scanf("%d",&m); ans=0; for(int i=0;i<m;i++){ scanf("%d%d",&a[i].B,&a[i].E); } sort(a,a+m,com); int time=0; for(int i=0;i<m;i++){ if(time<a[i].B){ ans++; time=a[i].E; } } cout<<ans<<endl; } return 0; } //解決問題的關鍵:優先選擇結束時間早的活動。 //開始時間早,活動時長段...安排活動的重點時結束時間早!!!