T-shirt buying CodeForces

题目链接

思路:

由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆,

我是根据pair<int,int> 创建了一个以价格小的优先的优先队列。

pair中的另外一个int,用来存这个衣服的id,即用来标记这个衣服有没有已经被卖了。

详细看代码哦

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct cmp
{
    bool operator()(const pii p1, const pii p2)
    {
        return p1.second > p2.second; //second的小值优先
    }
};
// fi -> id se -> prices
priority_queue<pii,vector<pii>,cmp> h[5];
int n;
int m;
int num[maxn];
bool bj[maxn];
int main()
{
    gg(n);
    repd(i,1,n)
    {
        gg(num[i]);
    }
    int c;
    repd(i,1,n)
    {
        gg(c);
        h[c].push(mp(i,num[i]));
    }
    repd(i,1,n)
    {
        gg(c);
        h[c].push(mp(i,num[i]));
    }
    gg(m);
    repd(i,1,m)
    {
        gg(c);
        if(h[c].empty())
        {
            printf("-1 ");
        }else
        {
            int flag=1;
            while(flag&&!h[c].empty())
            {
                pii tem=h[c].top();
                h[c].pop();
                if(bj[tem.fi]==0)
                {
                    printf("%d ",tem.second);
                    flag=0;
                    bj[tem.fi]=1;
                }else
                {
                    continue;
                }
            }
            if(flag)
            {

                printf("-1 ");
            }
        }

    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '
');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/10235706.html