其他分享
首页 > 其他分享> > Fence Painting

Fence Painting

作者:互联网

http://codeforces.com/contest/1481/problem/C

题意

\(给n个数,要变成另外n个数,问依次做出m次变换能否成功.\)

思路

\(对于这m个数\)

\(如果最后能够成功,那么必然所有a[i]!=b[i]的位置都被用掉了.\)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N], b[N], c[N];
int ans[N];
int n, m, f;

void solve() {
    unordered_map<int, int> cnt, mp1;
    unordered_map<int, vector<int>> v;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) {
        cin >> b[i];
        mp1[b[i]] = i;
    }
    for (int i = 1; i <= m; ++i) cin >> c[i];
    int all = 0; 
    for (int i = 1; i <= n; ++i) 
        if (a[i] != b[i]) {
            cnt[b[i]]++;
            v[b[i]].pb(i);
            all++;
        }
    if (all > m) {
        f = 0;
        return;
    }
    
    for (int i = m; i; --i) {
        if (!mp1[c[i]]) {
            if (i == m) {
                f = 0;
                return;
            } else ans[i] = ans[i + 1];
        }
        else {
            if (!cnt[c[i]]) ans[i] = mp1[c[i]];
            else {
                cnt[c[i]]--;
                ans[i] = v[c[i]][cnt[c[i]]];
                --all;
            }
        }
    }
    if (all) f = 0;
}

int main() {
    IO;
    int _;
    cin >> _;
    while (_--) {
        f = 1;
        solve();
        if (!f) cout << "NO\n";
        else {
            cout << "YES\n";
            for (int i = 1; i <= m; ++i) cout << ans[i] << " ";
            cout << '\n';
        }
    }
    return 0;
} 

标签:cnt,return,Fence,int,mp1,ans,Painting,define
来源: https://www.cnblogs.com/phr2000/p/14383439.html