【codeforces 799B】Tshirt buying

【题目链接】:http://codeforces.com/contest/799/problem/B

【题意】

告诉你每个人喜欢的衣服的颜色;
然后告诉你每件衣服的正面和背面的颜色以及它的价格;
只要某件衣服的正面或背面是某个人喜欢的颜色;
则那个人就会喜欢它;
然后每个人会挑自己最喜欢的且最便宜的衣服买;
给你每个人来的先后顺序;
让你求出每个人买的衣服的价格;

【题解】

定义3个set;
每次取出对应的set的头节点;
然后把那件衣服另外一面的颜色,在另外一个set中删去;
(或者,先不删去,记录某件衣服有没有被买去,然后每次取头结点,知道遇到一件没有被买去的衣服为止)

【Number Of WA

0

【完整代码】
code 1

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100;

struct rec{
    LL p,id;
    friend bool operator < (rec a,rec b)
    {
        return a.p<b.p||(a.p==b.p && a.id<b.id);
    }
};

set <rec> myset[4];
int n,p[N],a[N],b[N];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    rep1(i,1,n)
        cin>>p[i];
    rep1(i,1,n)
        cin >>a[i];
    rep1(i,1,n)
        cin >>b[i];
    rep1(i,1,n)
    {
        myset[a[i]].insert(rec{p[i],i});
        if (a[i]!=b[i])
            myset[b[i]].insert(rec{p[i],i});
    }
    cin >> n;
    rep1(i,1,n)
    {
        int x;
        cin >>x;
        if (myset[x].empty())
            cout<<-1<<endl;
        else
        {
            __typeof(myset[x].begin()) c=myset[x].begin();
            rec tou = (*c);
            cout << tou.p;
            myset[x].erase(c);
            //cout <<x<<' '<<(x^a[tou.id]^b[tou.id])<<endl;
            //cout <<tou.p<<endl;
            if (a[tou.id]!=b[tou.id])
            {
                int t = (x^a[tou.id]^b[tou.id]);
                c = myset[t].lower_bound(rec{p[tou.id],tou.id});
                myset[t].erase(c);
            }
        }
        if (i==n) cout << endl;
        else
            cout<<' ';
    }
    //init??????
    return 0;
}

code 2

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100;

struct rec{
    LL p,id;
    friend bool operator < (rec a,rec b)
    {
        return a.p<b.p||(a.p==b.p && a.id<b.id);
    }
};

set <rec> myset[4];
int n,p[N],a[N],b[N];
bool vis[N];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    rep1(i,1,n)
        cin>>p[i];
    rep1(i,1,n)
        cin >>a[i];
    rep1(i,1,n)
        cin >>b[i];
    rep1(i,1,n)
    {
        myset[a[i]].insert(rec{p[i],i});
        myset[b[i]].insert(rec{p[i],i});
    }
    cin >> n;
    rep1(i,1,n)
    {
        int x;
        cin >>x;
        bool fi = false;
        while (!myset[x].empty())
        {
            auto y = myset[x].begin();
            rec tou = *y;
            myset[x].erase(y);
            if (vis[tou.id]) continue;
            vis[tou.id] = true;
            fi = true;
            cout << tou.p <<' ';
            break;
        }
        if (!fi)
            cout <<-1<<' ';
    }
    //init??????
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626325.html