zoukankan      html  css  js  c++  java
  • CF799B T-shirt buying

    题目链接

    题解 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;
    } 
    自己选择的路,跪着也要走完
  • 相关阅读:
    Ubuntu安装deepin wine版QQ
    解决anaconda安装cvxpy失败的方法
    1006
    一种不用参数交换两变量值的方法
    输入三位数,翻转输出
    一个简单的问题
    1006ac(转)
    1007(转)
    杭电oj1004 自写成功代码
    1004
  • 原文地址:https://www.cnblogs.com/tonyshen/p/11734877.html
Copyright © 2011-2022 走看看