Codeforces 799B - T-shirt buying(set优先队列)

xiaoxiao2021-02-28  104

题目链接:http://codeforces.com/problemset/problem/799/B点击打开链接

B. T-shirt buying time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output

A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers piai 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.

Input

The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.

The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.

The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.

The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.

The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.

The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.

Output

Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.

Examples input 5 300 200 400 500 911 1 2 1 2 3 2 1 3 2 1 6 2 3 1 2 1 1 output 200 400 300 500 911 -1 input 2 1000000000 1 1 1 1 2 2 2 1 output 1 1000000000

题意:按所给顺序和要求买衣服 衣服正反有3种颜色123 找价格最低的 没有输出-1 第一反应是优先队列 写完之后学习set 使用更加方便

#include <stdio.h> #include <stdlib.h> #include <iostream> #include<algorithm> #include <math.h> #include <string.h> #include <limits.h> #include <string> #include <queue> #include <stack> using namespace std; struct xjy { int sp; int cl1; int cl2; int num; friend bool operator< (xjy n1, xjy n2) { return n1.sp>n2.sp; } }xjy1[200000]; int book[200000]; int a[200000]; int cmp1(struct xjy a,struct xjy b) { return a.sp<b.sp; } priority_queue<struct xjy> q[3]; int main() { int n=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].sp); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].cl1); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].cl2); for(int i=1;i<=n;i++) xjy1[i].num=i; for(int i=1;i<=n;i++) { if(xjy1[i].cl1==1||xjy1[i].cl2==1) { q[0].push(xjy1[i]); } else ; if(xjy1[i].cl1==2||xjy1[i].cl2==2) { q[1].push(xjy1[i]); } else ; if(xjy1[i].cl1==3||xjy1[i].cl2==3) { q[2].push(xjy1[i]); } else ; } int nn=0; scanf("%d",&nn); for(int i=0;i<nn;i++) scanf("%d",&a[i]); for(int i=0;i<nn;i++) { int flag=0; while(!q[a[i]-1].empty()) { struct xjy mid=q[a[i]-1].top(); q[a[i]-1].pop(); if(book[mid.num]) continue; else { flag=1; book[mid.num]=1; printf("%d ",mid.sp); break; } } if(!flag) printf("-1 "); } return 0; } #include <bits/stdc++.h> using namespace std; struct xjy { int sp; int cl1; int cl2; int num; bool operator < (const xjy &r) const { return sp<r.sp; } }; set <struct xjy> q[3]; int main() { int n=0; scanf("%d",&n); vector <int> book(n+1); vector <xjy> xjy1(n+1); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].sp); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].cl1); for(int i=1;i<=n;i++) scanf("%d",&xjy1[i].cl2); for(int i=1;i<=n;i++) xjy1[i].num=i; for(int i=1;i<=n;i++) { if(xjy1[i].cl1==1||xjy1[i].cl2==1) { q[0].insert(xjy1[i]); } else ; if(xjy1[i].cl1==2||xjy1[i].cl2==2) { q[1].insert(xjy1[i]); } else ; if(xjy1[i].cl1==3||xjy1[i].cl2==3) { q[2].insert(xjy1[i]); } else ; } int nn=0; scanf("%d",&nn); vector <int> a(nn+1); for(int i=0;i<nn;i++) scanf("%d",&a[i]); for(int i=0;i<nn;i++) { int flag=0; while(!q[a[i]-1].empty()) { struct xjy mid=*q[a[i]-1].begin(); q[a[i]-1].erase(q[a[i]-1].begin()); if(book[mid.num]) continue; else { flag=1; book[mid.num]=1; printf("%d ",mid.sp); break; } } if(!flag) printf("-1 "); } }

转载请注明原文地址: https://www.6miu.com/read-70782.html

最新回复(0)