HDU 2037 今年暑假不AC

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=2037

思路:

貪心思想,將節目結束時間從小到大排序,依次選取可以觀看的節目;

代碼:

#include<bits/stdc++.h>

using namespace std;

struct Pro {
	int s, e;
	bool operator < ( Pro & p) const {
		return e < p.e;	
	}
}arr[105];

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	int n;
	while(scanf("%d", &n) && n) {
		for(int i = 0; i < n; i++) {
			scanf("%d %d", &arr[i].s, &arr[i].e);	
		}
		sort(arr, arr + n);
		int cnt = 0, end = 0;
		for(int i = 0; i < n; i++) {
			if(arr[i].s < end) continue;
			++cnt;
			end = arr[i].e;
		}
		printf("%d\n", cnt);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章