CF799B T-shirt buying

题目大意

  有一些衣服,它们有价格、正面的颜色和反面的颜色。现有一群顾客按顺序来买存在某颜色且价格最低的衣服(不存在则不会买),求每个顾客花了多少钱。

思路

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int MAX_OBJ = 200010, MAX_COLOR = 5;
int TotObj;

struct Obj
{
    int Price, A, B;
    bool Vis;
}_objs[MAX_OBJ];

struct Cmp
{
    bool operator () (Obj *a, Obj *b)
    {
        return a->Price > b->Price;
    }
};

priority_queue<Obj*, vector<Obj*>, Cmp> q[3][MAX_COLOR];

int main()
{
    scanf("%d", &TotObj);
    for (int i = 1; i <= TotObj; i++)
        scanf("%d", &_objs[i].Price);
    for (int i = 1; i <= TotObj; i++)
    {
        scanf("%d", &_objs[i].A);
        q[1][_objs[i].A].push(_objs + i);
    }
    for (int i = 1; i <= TotObj; i++)
    {
        scanf("%d", &_objs[i].B);
        q[2][_objs[i].B].push(_objs + i);
    }
    int opCnt;
    scanf("%d", &opCnt);
    while(opCnt--)
    {
        int color;
        scanf("%d", &color);
        
        while(!q[1][color].empty() && q[1][color].top()->Vis)
            q[1][color].pop();
        Obj *obj1 = q[1][color].empty() ? NULL : q[1][color].top();
        while(!q[2][color].empty() && q[2][color].top()->Vis)
            q[2][color].pop();
        Obj *obj2 = q[2][color].empty() ? NULL : q[2][color].top();
        
        Obj *ans = obj1 && obj2 ? (obj1->Price < obj2->Price ? obj1 : obj2) : obj1 ? obj1 : obj2;
        bool Is1 = obj1 && obj2 ? (obj1->Price < obj2->Price) : (obj1 != NULL); 
        if (!ans)
            printf("-1 ");
        else
        {
            printf("%d ", ans->Price);
            ans->Vis = true;
            q[Is1 ? 1 : 2][Is1 ? ans->A : ans->B].pop();
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/headboy2002/p/9427004.html