CF 799B T-shirt buying

传送门:https://www.luogu.org/problemnew/show/CF799B

 

题中说每一个人只要一种颜色,无论正反面,所以我们只要贪心的选包含这种颜色中且没有被别的人选过的价格最小的衣服就行了。

实现只要每一种颜色开一个优先队列,然后给每一件衣服标号,然后对于每一个人,查看他要的颜色的优先队列,如果没被选走,就拿这件衣服。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cctype>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 #define enter printf("\n")
12 #define space printf(" ")
13 #define Mem(a) memset(a, 0, sizeof(a))
14 typedef long long ll;
15 typedef double db;
16 const int INF = 0x3f3f3f3f;
17 const db eps = 1e-8;
18 const int maxn = 2e5 + 5;
19 inline ll read()
20 {
21     ll ans = 0;
22     char ch = getchar(), last = ' ';
23     while(!isdigit(ch)) {last = ch; ch = getchar();}
24     while(isdigit(ch))
25     {
26         ans = ans * 10 + ch - '0'; ch = getchar();    
27     }
28     if(last == '-') ans = -ans;
29     return ans;
30 }
31 inline void write(ll x)
32 {
33     if(x < 0) x = -x, putchar('-');
34     if(x >= 10) write(x / 10);
35     putchar('0' + x % 10);
36 }
37 
38 int n, m;
39 struct Node
40 {
41     int id;
42     ll p;
43     bool operator < (const Node& other)const
44     {
45         return p > other.p ||(p == other.p && id > other.id);
46     }
47 }t[maxn];
48 priority_queue<Node> sh[5];
49 bool vis[maxn];
50 
51 int main()
52 {
53     n = read();
54     for(int i = 1; i <= n; ++i) {t[i].p = read(); t[i].id = i;}
55     for(int i = 1; i <= n; ++i) {int a = read(); sh[a].push(t[i]);}        //没必要记录没每一件衣服的颜色 
56     for(int i = 1; i <= n; ++i) {int b = read(); sh[b].push(t[i]);}
57     m = read();
58     for(int i = 1; i <= m; ++i)
59     {
60         bool flag = 0;
61         int c = read();
62         while(!sh[c].empty())
63         {
64             Node x = sh[c].top(); sh[c].pop();
65             if(!vis[x.id]) {vis[x.id] = 1; flag = 1; write(x.p); enter; break;}
66         } 
67         if(!flag) {printf("-1\n"); continue;}
68     }
69     return 0;
70 }

 

posted @ 2018-08-03 16:02  mrclr  阅读(910)  评论(0编辑  收藏  举报