codeforcesPlayrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)B题B. T-shirt buying

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 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.

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

#include<bits/stdc++.h>
using namespace std;
int n,m;
#define N 210001
#define PER(i,a,b) for(int i=a;i<=b;i++)
int a[N],b[N];int p[N];int c[N];bool pg[4][4];int t[4][4][2*N];int size[4][4];
void up(int x,int y,int k)
{
    while(k>1&&t[x][y][k]<t[x][y][k/2]) 
    {
        swap(t[x][y][k],t[x][y][k/2]);
        k/=2;
    }
}
void down(int x,int y,int k)
{
    int j;
    int le=k*2,ri=le+1;
    while(le<=size[x][y])
    {
        if(le==size[x][y]||t[x][y][le]<t[x][y][ri]) j=le;else j=ri;
        if(t[x][y][j]<t[x][y][k]) swap(t[x][y][j],t[x][y][k]);
        else return;
        k=j;le=k*2,ri=le+1;
    }
    return;
}
void add(int x,int y,int p)
{
    int num=++size[x][y];
    t[x][y][num]=p;up(x,y,num);
}
int co;
int back,on[4];
int QwQ;
int bb,cc;
int main()
{
    scanf("%d",&n); PER(i,1,n) scanf("%d",&p[i]);
    PER(i,1,3)PER(j,1,3)PER(xx,1,n+1) t[i][j][xx]=2e9;
    PER(i,1,n)scanf("%d",&a[i]);PER(i,1,n)scanf("%d",&b[i]);
    PER(i,1,n) if(a[i]>b[i]) swap(a[i],b[i]);
    PER(i,1,n) add(a[i],b[i],p[i]); scanf("%d",&m);
    PER(i,1,m)
    {
        scanf("%d",&co);
        PER(j,1,3)
        {
            bb=co;cc=j;
            if(bb>cc) swap(bb,cc);
            on[j]=t[bb][cc][1];
        }
        QwQ=2e9;
        PER(j,1,3) {if(QwQ>on[j]) {back=j;QwQ=on[j];}}if(back==0||QwQ>1e9+4001) QwQ=-1;
        printf("%d ",QwQ);
        bb=co;cc=back;if(bb>cc) swap(bb,cc);t[bb][cc][1]=2e9+5;down(bb,cc,1);   
    }
}

> 这里的算法非常暴力,建衣服前后两面为集合(1,2),(1,3),(2,3),(1,1),(2,2),(3,3)共6个堆,然后就可以暴力乱弹,对每个询问比一下大小弹个堆即可。其实是可以用三个优先队列,维护一下有没有被取过来解决的,但复杂度没我这个优。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值