1. 程式人生 > 實用技巧 >1298活動選擇(貪心)

1298活動選擇(貪心)

Description

學校的大學生藝術中心週日將面向全校各個學院的學生社團開放,但活動中心同時只能供一個社團活動使用,並且每一個社團活動開始後都不能中斷。現在各個社團都提交了他們使用該中心的活動計劃(即活動的開始時刻和截止時刻)。請設計一個演算法來找到一個最佳的分配序列,以能夠在大學生藝術中心安排不衝突的儘可能多的社團活動。
比如有5個活動,開始與截止時刻分別為:



最佳安排序列為:1,4,5。

Input

第一行輸入活動數目n(0<n<100);
以後輸入n行,分別輸入序號為1到n的活動使用中心的開始時刻a與截止時刻b(a,b為整數且0<=a,b<24,a,b輸入以空格分隔)。

Output

輸出最佳安排序列所包含的各個活動(按照活動被安排的次序,兩個活動之間用逗號分隔),如果有多個活動安排序列符合要求輸出字典序最小的序列。

Sample

Input

6
8 10
9 16
11 16
14 15
10 14
7 11

Output

1,5,4
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <vector>
 5 #include <queue>
 6 
 7 #define inf 0x3f3f3f3f
 8
9 using namespace std; 10 11 struct node 12 { 13 int left, right, id; 14 }huo[105]; 15 16 bool cmp(struct node a, struct node b) 17 { 18 return a.right<b.right || (a.right==b.right&&a.id<b.id); 19 } 20 21 int main() 22 { 23 int n, i, top, cur; 24 int re[105]; 25 cin >> n;
26 for(i=0;i<n;i++) 27 { 28 cin >> huo[i].left >> huo[i].right; 29 huo[i].id = i+1; 30 } 31 sort(huo, huo+n, cmp); 32 top = 0; 33 cur = 0; 34 for(i=0;i<n;i++) 35 { 36 if(huo[i].left >= cur) 37 { 38 re[top++] = huo[i].id; 39 cur = huo[i].right; 40 } 41 } 42 for(i=0;i<top;i++) 43 { 44 if(i==top-1) cout << re[i] << endl; 45 else cout << re[i] << ","; 46 } 47 return 0; 48 }