码迷,mamicode.com
首页 > 其他好文 > 详细

CF799B T-shirt buying

时间:2019-10-24 21:16:27      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:style   card   ola   for   clu   ons   ++   如何   article   

题目链接

题解 CF799B 【T-shirt buying】

STL大法好!!!

用三个优先级队列记录每件衣服的价钱,堆里存放价钱

因为是按照顺序买衣服

所以每次取堆里最小的就好了

但是一个问题浮现了出来

如何处理别人已经拿过的衣服???

于是就将优先级队里存放的改为一个结构体,记录价钱和编号

用vis数组记录是否已经被取走

如果堆空就输出-1

细节见代码:

#include<bits/stdc++.h>
using namespace std;
struct SYM{
    int p,id;
    bool operator<(const SYM &a)const{
        return a.p<p;
    }
}clo[200010];
int vis[200010],c,n,m;
priority_queue<SYM> q[4];
int main(){
    //freopen("c.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&clo[i].p);
        clo[i].id=i;
    }
    for(int i=1;i<=2*n;i++){
        scanf("%d",&c);
        if(i>n) q[c].push(clo[i-n]);   //怼到堆里
        else q[c].push(clo[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        int hh=1;
        scanf("%d",&c);
        if(q[c].empty()){
              printf("-1 ");
              continue ;
        }
        while(vis[q[c].top().id]){ 
              q[c].pop();          //如果已经拿过就弹出拿下一个
              if(q[c].empty()){         
                 printf("-1 ");
                 hh=0;
                 break;
              }
        }
        if(hh) printf("%d ",q[c].top().p),vis[q[c].top().id]=1,q[c].pop();    //买最便宜衣服,记录衣服已经买过,弹出这件衣服
    }
    return 0;
} 

 

CF799B T-shirt buying

标签:style   card   ola   for   clu   ons   ++   如何   article   

原文地址:https://www.cnblogs.com/tonyshen/p/11734877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!