Codeforces Round #699 (Div. 2) C. Fence Painting
翻译:
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it.
You have a fence consisting of 𝑛n planks, where the 𝑖i-th plank has the color 𝑎𝑖ai. You want to repaint the fence in such a way that the 𝑖i-th plank has the color 𝑏𝑖bi.
You've invited 𝑚m painters for this purpose. The 𝑗j-th painter will arrive at the moment 𝑗j and will recolor exactly one plank to color 𝑐𝑗cj. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.
Can you get the coloring 𝑏b you want? If it's possible, print for each painter which plank he must paint.
Input
The first line contains one integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases. Then 𝑡t test cases follow.
The first line of each test case contains two integers 𝑛n and 𝑚m (1≤𝑛,𝑚≤1051≤n,m≤105) — the number of planks in the fence and the number of painters.
The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤𝑛1≤ai≤n) — the initial colors of the fence.
The third line of each test case contains 𝑛n integers 𝑏1,𝑏2,…,𝑏𝑛b1,b2,…,bn (1≤𝑏𝑖≤𝑛1≤bi≤n) — the desired colors of the fence.
The fourth line of each test case contains 𝑚m integers 𝑐1,𝑐2,…,𝑐𝑚c1,c2,…,cm (1≤𝑐𝑗≤𝑛1≤cj≤n) — the colors painters have.
It's guaranteed that the sum of 𝑛n doesn't exceed 105105 and the sum of 𝑚m doesn't exceed 105105 over all test cases.
Output
For each test case, output "NO" if it is impossible to achieve the coloring 𝑏b.
Otherwise, print "YES" and 𝑚m integers 𝑥1,𝑥2,…,𝑥𝑚x1,x2,…,xm, where 𝑥𝑗xj is the index of plank the 𝑗j-th painter should paint.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
Example
input
Copy
6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4
output
Copy
YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO
思路:按顺序来画,后面的可以覆盖前边的。
所以判断可不可以满足,首先我们可以直接看最后一个,存不存在于需要满足的的地方,如果不存在,那么直接就是NO,因为最后一个,无法被覆盖。如果存在,那么给它找个位置,因为多余不需要的画师,都可以画在那个位置,因为最后会被覆盖。所以我们接下来只需要考虑,怎么画,能不能画完这个问题。
我们给每个需要改变的存进去,之后如果有我们有利用vector的back和pop_back来存入和弹出(本来用的map+queue,T到死都过不去的,复杂度太高之后想到了vector的操作),然后每次存,然后如果没有需要画师画的,那就直接画在最后一个画师预定的位置上就好了。到最后,如果还有需要画的地方没有画,那自然就是NO了,之后就是输出即可AC。
代码:
/*Looking! The blitz loop this planet to search way
Only my RAILGUN can shoot it 今すぐ
身体中を 光の速さで
駆け巡った確かな予感
掴め! 望むものなら残さず
輝ける自分らしさで
信じてるよ あの日の誓いを
この瞳に光る涙それさえも 強さになるから
*/
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdio.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<numeric>
#include<stack>
using namespace::std;
typedef long long ll;
int n,t;
inline __int128 read(){
__int128 x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void print(__int128 x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
int m;
int a[100005];
int b[100005];
int c[100005];
pair<int,int>ff;
void wanyurukong(){
cin>>n>>m;
// map<int,int>q;
// map<int,int>w;
// map<int,int>e;
int flag=0;
vector<int>na;
vector<int>cc[100005];
for (int i =1; i<=n; i++) {
cin>>a[i];
}
for (int i =1; i<=n; i++) {
cin>>b[i];
// q[b[i]]=1;
if (a[i]!=b[i]) {
cc[b[i]].push_back(i);
flag=1;
// w[b[i]]++;
}
}
for (int i =1; i<=m; i++) {
cin>>c[i];
// e[c[i]]++;
}
// if (!q.count(c[m])) {
// printf("NO\n");return;
// }
int kl=-23;
// for(auto [x,y]:w){
// if (w[x]>e[x]) {
// printf("NO\n");return;
// }
// }
if (cc[c[m]].size()) {
kl=cc[c[m]].back();
cc[c[m]].pop_back();
}
else{
for (int i =1; i<=n; i++) {
if (b[i]==c[m]) {
kl=i;break;
}
}
}
if (kl==-23) {
printf("NO\n");return;
}
if (!flag) {
printf("YES\n");
for (int i =1; i<=m; i++) {
printf("%d ",kl);
}printf("\n");return;
}
for (int i =1; i<m; i++) {
if (cc[c[i]].size()==0) {
// printf("%d ",kl);
na.push_back(kl);
continue;
}
na.push_back(cc[c[i]].back());
// printf("%d ",cc[c[i]].back());
cc[c[i]].pop_back();
// while (!cc.empty()) {
// ff=cc.front();cc.pop();
// if (ff.first==c[i]) {
// w[c[i]]--;
// printf("%d ",ff.second);
// break;
// }
// cc.push(ff);
// }
}
// printf("%d\n",kl);
na.push_back(kl);
for (int i =1; i<=n; i++) {
if (cc[b[i]].size()>0) {
printf("NO\n");return;
}
}
printf("YES\n");
for (int i =0; i<na.size(); i++) {
printf("%d ",na[i]);
}printf("\n");
}
int main(){
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
cin>>t;
while (t--) {
wanyurukong();
}
//wanyurukong
return 0;
}