• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 5437 Alisha’s Party (优先队列模拟)

题意:邀请k个朋友,每个朋友带有礼物价值不一,m次开门,每次开门让一定人数p(如果门外人数少于p,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每次都是礼物价值高的人先进。最后给出q个数,表示要输出第ni个进来的人的名字。

析:其实这就是一个模拟题,很容易知道是优先队列模拟,不能set,会超时,反正我是超时了,然后就一步步的模拟就好了,注意它给的时间可能不是按顺序,要排序,在比赛时我就没排序,一直WA。。。。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 150000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
struct node{
    int id, val;
    char name[205];
    bool operator < (const node &p) const{
        return val < p.val || (val == p.val && id > p.id);
    }
};
node a[maxn];
P pt[maxn];
int id[maxn];
int ans[maxn];
priority_queue<node> pq;

int main(){
   // ios::sync_with_stdio(false);
    int T, q;  cin >> T;
    while(T--){
        scanf("%d %d %d", &n, &m, &q);
        for(int i = 0; i < n; ++i){
            scanf("%s %d", a[i].name, &a[i].val);
            a[i].id = i;
        }
        for(int i = 0; i < m; ++i)
            scanf("%d %d", &pt[i].first, &pt[i].second);
        sort(pt, pt+m);
        pt[m].first = pt[m].second = 0;
        for(int i = 0; i < q; ++i)  scanf("%d", &id[i]);
        int cnt = 0;
        for(int i = 0, j = 0; i < n; ++i){
            pq.push(a[i]);
            if(i + 1 == pt[j].first){
                int x = 0;
                while(!pq.empty() && x < pt[j].second){
                    ans[cnt++] = pq.top().id;
                    pq.pop();
                    ++x;
                }
                ++j;
            }
        }

        while(!pq.empty()){
            ans[cnt++] = pq.top().id;
            pq.pop();
        }

        for(int i = 0; i < q; ++i){
            if(i)  putchar(' ');
            printf("%s", a[ans[id[i]-1]].name);
        }
        printf("\n");
    }

    return 0;
}

 

posted on 2016-07-31 16:47  dwtfukgv  阅读(205)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3