zoukankan      html  css  js  c++  java
  • 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("
    ");
        }
    
        return 0;
    }
    
  • 相关阅读:
    斐波那契数列 的两种实现方式(Java)
    单链表反转
    单链表合并
    两个有序list合并
    list去重 转载
    RemoveAll 要重写equals方法
    Java for LeetCode 138 Copy List with Random Pointer
    Java for LeetCode 137 Single Number II
    Java for LeetCode 136 Single Number
    Java for LeetCode 135 Candy
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5723363.html
Copyright © 2011-2022 走看看