标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360
优先队列+贪心。(感觉这个贪心在《挑战程序设计竞赛》一书中有个类似的,有兴趣的可以找一下。)
写优先队列优先级判断的时候把maxx写成minn也是醉了,调了2个小时才发现。看来应该再仔细一些啊。
初始化将下限小于当前人数的人全部压入队列中,再判断要求上限和当前人数是否符合,如果符合,则当前去的人数+1,此人出队。同时记下顺序。接着将下限符合条件的压入队列。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <cctype> 8 #include <queue> 9 #include <map> 10 #include <set> 11 #include <stack> 12 #include <list> 13 #include <vector> 14 15 using namespace std; 16 17 18 typedef struct Node { 19 int maxx; 20 int minn; 21 int id; 22 friend bool operator <(Node a, Node b) { 23 return a.maxx > b.maxx; 24 } 25 }Node; 26 27 bool cmp(Node a, Node b) { 28 return a.minn < b.minn; 29 } 30 31 const int maxn = 1000010; 32 Node peo[maxn]; 33 priority_queue<Node> pq; 34 int ord[maxn]; 35 int n; 36 37 int main() { 38 // freopen("in", "r", stdin); 39 int T; 40 scanf("%d", &T); 41 while(T--) { 42 int cnt = 0; 43 while(!pq.empty()) { 44 pq.pop(); 45 } 46 scanf("%d", &n); 47 for(int i = 1; i <= n; i++) { 48 scanf("%d", &peo[i].minn); 49 peo[i].id = i; 50 } 51 for(int i = 1; i <= n; i++) { 52 scanf("%d", &peo[i].maxx); 53 } 54 sort(peo+1, peo+n+1, cmp); 55 int cur = 0; //当前有多少人去 56 int k = 1; 57 while(peo[k].minn <= cur) { //init 58 pq.push(peo[k++]); 59 } 60 while(!pq.empty()) { 61 if(pq.top().maxx >= cur) { 62 cur++; 63 } 64 ord[cnt++] = pq.top().id; 65 pq.pop(); 66 while(peo[k].minn <= cur && k <= n) { 67 pq.push(peo[k++]); 68 } 69 } 70 printf("%d\n", cur); 71 for(int i = 0; i < cnt; i++) { 72 printf("%d ", ord[i]); 73 } 74 for(int i = k; i <= n; i++) { 75 printf("%d ", peo[i].id); 76 } 77 printf("\n"); 78 } 79 return 0; 80 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4781944.html