Codeforces Round #413 B. T-shirt buying

来源:互联网 发布:战国红异端网络 编辑:程序博客网 时间:2024/06/01 09:06

A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.

m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.

A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won’t buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.

You are to compute the prices each buyer will pay for t-shirts.

题意

有 n 件T恤,每件正面与反面的颜色均为 1-3 中一种(可能相同),每件的价格为 pi ,且均不同。m 个顾客依次前来买一件衣服,对于第 j 个顾客,他所选择的衣服一定存在至少一面是他喜欢的颜色 cj ,且将选择所有符合条件的衣服中最便宜的一件。输出 m 个顾客所消费的金额?若没有满足条件的衣服,则不买,输出 -1 。

解题思路

对颜色 1, 2, 3 分别建立优先队列,价格小的靠近队首。若每件衣服只属于一个队列,则对每个顾客抛出队首元素即可。但衣服具有正反两面,即每件衣服可能分属两个不同的优先队列,故为解决这种情况,应设置标记数组,记录该件衣服是否已经出售。

代码

#include<bits/stdc++.h>using namespace std;const int N = 200000 + 10;struct T_SHIRT {    int p, a, b, id;} t[N], tmp;bool operator<(T_SHIRT a, T_SHIRT b) {    return a.p > b.p;}bool mrk[N];priority_queue<T_SHIRT> que[4];int main(){    int n, m;    scanf("%d", &n);    for(int i=1;i<=n;i++)        scanf("%d",&t[i].p);    for(int i=1;i<=n;i++)        scanf("%d",&t[i].a);    for(int i=1;i<=n;i++)        scanf("%d",&t[i].b);    for(int i=1;i<=n;i++) {        t[i].id = i;        que[t[i].a].push(t[i]);        if(t[i].a != t[i].b)    que[t[i].b].push(t[i]);    }    scanf("%d",&m);    for(int i=1, c;i<=m;i++)    {        scanf("%d",&c);        if(que[c].empty())  printf("-1 ");        while(!que[c].empty())        {            tmp = que[c].top();            que[c].pop();            if(mrk[tmp.id]) {                if(que[c].empty())  printf("-1 ");                continue;            }            mrk[tmp.id] = 1;            printf("%d ", tmp.p);            break;        }    }}
0 0