1. 程式人生 > >南陽OJ-14-會場安排問題---區間不相交

南陽OJ-14-會場安排問題---區間不相交

需要 main -- 競賽 一個 const nod clas 會場安排問題

題目鏈接:

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=14

題目描述:

學校的小禮堂每天都會有許多活動,有時間這些活動的計劃時間會發生沖突,需要選擇出一些活動進行舉辦。小劉的工作就是安排學校小禮堂的活動,每個時間最多安排一個活動。現在小劉有一些活動計劃的時間表,他想盡可能的安排更多的活動,請問他該如何安排。

思路:

盡可能多的選取區間使得兩兩不相交。按照右端點排序,然後依次選取第一個右端點,然後除去與之相交的區間,選下一個右端點,重復如此。證明過程見算法競賽入門經典P232

 1 #include<iostream>
 2 #include<cstdio>
 3
#include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 const int maxn = 1e4 + 10; 8 int T, n; 9 struct node 10 { 11 int x, y; 12 bool operator < (const node a)const 13 { 14 return y < a.y; 15 } 16 node(){} 17 node(double
x, double y):x(x), y(y){} 18 }; 19 node a[maxn]; 20 int main() 21 { 22 cin >> T; 23 while(T--) 24 { 25 cin >> n; 26 for(int i = 0; i < n; i++) 27 { 28 cin >> a[i].x >> a[i].y; 29 } 30 sort(a, a + n); 31 int
time = 0, ans = 0; 32 for(int i = 0; i < n; i++) 33 { 34 if(a[i].x > time) 35 { 36 time = a[i].y; 37 ans++; 38 } 39 } 40 cout<<ans<<endl; 41 } 42 return 0; 43 }

南陽OJ-14-會場安排問題---區間不相交