【codeforces 782D】 Innokenty and a Football League

【题目链接】:http://codeforces.com/contest/782

【题意】

每个队名有两种选择,
然后第一个选择队名相同的那些队只能选第二种;
让你安排队名

【题解】

首先全都选成第一种队名;
然后第一种队名相同的队,它们只能全都变成选第二种队名的了;
这个时候如果第一种队名相同的那些队里面,变成第二种队名后有重复的,直接输出无解;
这个时候先不管第一种队名
之后再处理第一种队名;
看看第一种队名有没有和第二种队名重的;
如果有重的话就让他变到第二种队名;
以此法贪心就好

【完整代码】

#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 rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&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 = 1e3 + 100;

int n, ans = 1,now;
string s1, s2;
string ts1[N], ts2[N];
int zt[N];
map <string, vector<int> > dic1,dic2;
map <string, int > di1, di2;
map <string, bool> haved;

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    rei(n);
    rep1(i, 1, n)
    {
        cin >> s1 >> s2;
        ts1[i] = "",ts1[i] += s1.substr(0, 3);
        ts2[i] = "", ts2[i] += s1.substr(0, 2);
        ts2[i] += s2[0];
        dic1[ts1[i]].push_back(i);
        di1[ts1[i]]++;
    }
    rep1(i,1,n)
        if (!haved[ts1[i]])
        {
            int len = dic1[ts1[i]].size();
            if (len > 1)
            {
                rep1(j, 0, len - 1)
                {
                    di1[ts1[i]]--;
                    int idx = dic1[ts1[i]][j];
                    zt[idx] = 1;
                    di2[ts2[idx]]++;
                    if (di2[ts2[idx]] > 1)
                        return puts("NO"), 0;
                }
            }
            haved[ts1[i]] = true;
        }
    while (1)
    {
        bool ok = true;
        rep1(j,1,n)
            if (zt[j] == 0)
            {
                if (di2[ts1[j]])
                {
                    ok = false;
                    if (di2[ts2[j]])
                        return puts("NO"), 0;
                    else
                    {
                        zt[j] = 1;
                        di2[ts2[j]] = 1;
                    }
                }
            }
        if (ok) break;
    }
    puts("YES");
    rep1(i, 1, n)
    {
        if (zt[i] == 0)
            cout << ts1[i] << endl;
        else
            cout << ts2[i] << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626576.html