您的位置:首页 > 其它

[HDOJ5360]Hiking

2015-09-04 16:44 417 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360

优先队列+贪心。(感觉这个贪心在《挑战程序设计竞赛》一书中有个类似的,有兴趣的可以找一下。)

写优先队列优先级判断的时候把maxx写成minn也是醉了,调了2个小时才发现。看来应该再仔细一些啊。

初始化将下限小于当前人数的人全部压入队列中,再判断要求上限和当前人数是否符合,如果符合,则当前去的人数+1,此人出队。同时记下顺序。接着将下限符合条件的压入队列。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cctype>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <vector>

using namespace std;

typedef struct Node {
int maxx;
int minn;
int id;
friend bool operator <(Node a, Node b) {
return a.maxx > b.maxx;
}
}Node;

bool cmp(Node a, Node b) {
return a.minn < b.minn;
}

const int maxn = 1000010;
Node peo[maxn];
priority_queue<Node> pq;
int ord[maxn];
int n;

int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
int cnt = 0;
while(!pq.empty()) {
pq.pop();
}
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &peo[i].minn);
peo[i].id = i;
}
for(int i = 1; i <= n; i++) {
scanf("%d", &peo[i].maxx);
}
sort(peo+1, peo+n+1, cmp);
int cur = 0;    //当前有多少人去
int k = 1;
while(peo[k].minn <= cur) {   //init
pq.push(peo[k++]);
}
while(!pq.empty()) {
if(pq.top().maxx >= cur) {
cur++;
}
ord[cnt++] = pq.top().id;
pq.pop();
while(peo[k].minn <= cur && k <= n) {
pq.push(peo[k++]);
}
}
printf("%d\n", cur);
for(int i = 0; i < cnt; i++) {
printf("%d ", ord[i]);
}
for(int i = k; i <= n; i++) {
printf("%d ", peo[i].id);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: