Codeforces Round #413 B. T-shirt buying

来源:互联网 发布:mac u盘 没有退出选项 编辑:程序博客网 时间:2024/06/01 10:37

传送门

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
5300 200 400 500 9111 2 1 2 32 1 3 2 162 3 1 2 1 1
output
200 400 300 500 911 -1 
input
21000000000 11 11 222 1
output
1 1000000000 


题目大意是:p为衣服价值,a为衣服前面的颜色,b为衣服后面的颜色,m是买的人有多少个。c是这些人喜欢的颜色。

如果这些人能在剩下的衣服中找到自己想要的颜色的衣服就买下来 当然是买当前最便宜的嘛。


#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0);#define rand() srand(time(0));typedef long long LL;typedef pair<int, int> pii;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;//const int dx[]={-1,0,1,0,-1,-1,1,1};//const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=2e5+100;const double EPS=1e-9;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt);    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1);    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}inline int Scan(){    int res=0,ch,flag=0;    if((ch=getchar())=='-')flag=1;    else if(ch>='0' && ch<='9')res=ch-'0';    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';    return flag ? -res : res;}struct node{    int p,a,b;}Q[maxx];map<int,int >vis;stack<int >s1,s2,s3;bool cmp(node A,node B){    if(A.p!=B.p)        return A.p>B.p;//价格排序 因为用到stack 所以最大值放最前面}int c[maxx];int main(){    int n,m,x;    scanf("%d",&n);    for(int i=1;i<=n;i++)        Q[i].p=Scan();    for(int i=1;i<=n;i++)        Q[i].a=Scan();    for(int i=1;i<=n;i++)        Q[i].b=Scan();    sort(Q+1,Q+n+1,cmp);    scanf("%d",&m);    for(int i=1;i<=n;i++)    {        vis[Q[i].p]=0;        if(Q[i].a==1||Q[i].b==1)            s1.push(Q[i].p);//分三个stack分别在1-2-3颜色里找衣服的价格        if(Q[i].a==2||Q[i].b==2)            s2.push(Q[i].p);        if(Q[i].a==3||Q[i].b==3)            s3.push(Q[i].p);    }    for(int i=1;i<=m;i++)    {        int flag=0;        scanf("%d",&x);        if(x==1)        {            while(!s1.empty())            {                int h=s1.top();                s1.pop();                if(vis[h]==0)                {                    printf("%d ",h);                    vis[h]=1;                    flag=1;                    break;                }            }            if(!flag)                printf("-1 ");        }        if(x==2)        {            while(!s2.empty())            {                int h=s2.top();                s2.pop();                if(vis[h]==0)                {                    printf("%d ",h);                    vis[h]=1;                    flag=1;                    break;                }            }            if(!flag)                printf("-1 ");        }        if(x==3)        {            while(!s3.empty())            {                int h=s3.top();                s3.pop();                if(vis[h]==0)                {                    printf("%d ",h);                    vis[h]=1;                    flag=1;                    break;                }            }            if(!flag)                printf("-1 ");        }    }}


0 0