Fence Painting

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

题意

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

思路

\(对于这m个数\)

  • \(如果c[i]不在b中:\)

    • \(如果是最后一个,一定不能成功.\)
    • \(不是最后一个,要考虑它被后面的数覆盖.\)
  • \(如果c[i]在b中:\)

    • \(cnt[c[i]]==0,随意放在一个最后是c[i]的位置即可.\)
    • \(cnt[c[i]]!=0,必须把它放在指定位置.\)

\(如果最后能够成功,那么必然所有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;
} 
上一篇:Spring的JdbcTemplate


下一篇:dubbo+zookeeper 记录