exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 0bf1cbed7e89503acfcda8b314fa0e71 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
/*
HashMap<> map=new HashMap<>();
TreeMap<> map=new TreeMap<>();
map.put(p,map.getOrDefault(p,0)+1);
for(Map.Entry<> mx:map.entrySet()){
int v=mx.getValue(),k=mx.getKey();
}
ArrayList<Pair<Character,Integer>> l=new ArrayList<>();
ArrayList<> l=new ArrayList<>();
HashSet<> has=new HashSet<>();*/
PrintWriter out;
FastReader sc;
int mod=(int)(1e9+7);
public void sol(){
int t=ni();
while(t-->0){
int n=ni(),m=ni();
int[] a=new int[n],b=new int[n],c=new int[n+1],gg=new int[m];
HashMap<Integer,Integer> map=new HashMap<>();
HashSet<Integer>[] h=new HashSet[n+1];
int last=0;
StringBuilder sb=new StringBuilder();
for(int i=1;i<=n;i++)h[i]=new HashSet<>();
for(int i=0;i<n;i++)a[i]=ni();
for(int i=0;i<n;i++){
int p=ni();
b[i]=p;
map.put(p,i+1);
}for(int i=0;i<m;i++){
int p=ni();
if(i==m-1)last=p;
c[p]++;
h[p].add(i);
}boolean f=false,g=false;
int ans=0;
for(int i=0;i<n;i++){
if(a[i]==b[i])continue;
else{
int p=b[i];
if(c[p]<=0){
f=true;
break;
}else{
c[p]--;
int v=0;
for(int j:h[p]){
v=j;
break;
}h[p].remove(v);
gg[v]=i+1;
if(v==m-1){
ans=i+1;
g=true;
}
}
}
}if(f){
no();
continue;
}for(int i=0;i<m;i++){
if(gg[i]==0){
f=true;
break;
}
}if(f&&map.get(last)==null){
no();
continue;
}int jj=map.get(last);
if(g){
jj=ans;
}
yes();
for(int i=0;i<m;i++){
if(gg[i]==0)sb.append(jj+" ");
else sb.append(gg[i]+" ");
}pl(sb);
}
}
public static void main(String[] args)
{
Main g=new Main();
g.out=new PrintWriter(System.out);
g.sc=new FastReader();
g.sol();
g.out.flush();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} public int ni(){
return sc.nextInt();
}public long nl(){
return sc.nextLong();
}public double nd(){
return sc.nextDouble();
}public char[] rl(){
return sc.nextLine().toCharArray();
}public String rl1(){
return sc.nextLine();
}
public void pl(Object s){
out.println(s);
}public void ex(){
out.println();
}
public void pr(Object s){
out.print(s);
}public String next(){
return sc.next();
}public long abs(long x){
return Math.abs(x);
}
public int abs(int x){
return Math.abs(x);
}
public double abs(double x){
return Math.abs(x);
}
public long pow(long x,long y){
return (long)Math.pow(x,y);
}
public int pow(int x,int y){
return (int)Math.pow(x,y);
}
public double pow(double x,double y){
return Math.pow(x,y);
}public long min(long x,long y){
return (long)Math.min(x,y);
}
public int min(int x,int y){
return (int)Math.min(x,y);
}
public double min(double x,double y){
return Math.min(x,y);
}public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) {
l.add(i);
}
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) {
l.add(i);
}
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}void sort(double[] a) {
ArrayList<Double> l = new ArrayList<>();
for (double i : a) {
l.add(i);
}
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}int swap(int a,int b){
return a;
}long swap(long a,long b){
return a;
}double swap(double a,double b){
return a;
}
boolean isPowerOfTwo (int x)
{
return x!=0 && ((x&(x-1)) == 0);
}boolean isPowerOfTwo (long x)
{
return x!=0 && ((x&(x-1)) == 0);
}public long max(long x,long y){
return (long)Math.max(x,y);
}
public int max(int x,int y){
return (int)Math.max(x,y);
}
public double max(double x,double y){
return Math.max(x,y);
}long sqrt(long x){
return (long)Math.sqrt(x);
}int sqrt(int x){
return (int)Math.sqrt(x);
}void input(int[] ar,int n){
for(int i=0;i<n;i++)ar[i]=ni();
}void input(long[] ar,int n){
for(int i=0;i<n;i++)ar[i]=nl();
}int maxint(){
return Integer.MAX_VALUE;
}int minint(){
return Integer.MIN_VALUE;
}long maxlong(){
return Long.MAX_VALUE;
}long minlong(){
return Long.MIN_VALUE;
}void fill(int[] ar,int k){
Arrays.fill(ar,k);
}void yes(){
pl("YES");
}void no(){
pl("NO");
}
int[] sieve(int n)
{
boolean prime[] = new boolean[n+1];
int[] k=new int[n+1];
for(int i=0;i<=n;i++) {
prime[i] = true;
k[i]=i;
}
for(int p = 2; p <=n; p++)
{
if(prime[p] == true)
{
// sieve[p]=p;
for(int i = p*2; i <= n; i += p) {
prime[i] = false;
// sieve[i]=p;
while(k[i]%(p*p)==0){
k[i]/=(p*p);
}
}
}
}return k;
}int strSmall(int[] arr, int target)
{
int start = 0, end = arr.length-1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
} int strSmall(ArrayList<Integer> arr, int target)
{
int start = 0, end = arr.size()-1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid) > target) {
start = mid + 1;
ans=start;
}
else {
end = mid - 1;
}
}
return ans;
}
public static class pair implements Comparable<pair> {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + "," + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Double(x).hashCode() * 31 + new Double(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | be75c7ea0560e93ca16bf13a95bc8525 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.Scanner;
import java.util.LinkedList;
public class D21195 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
l: for(int t=sc.nextInt();t-->0;) {
StringBuilder sb=new StringBuilder();
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m];
LinkedList<Integer> d[]=new LinkedList[n+1];
d[n]=new LinkedList<Integer>();
for(int i=0;i<n;i++) {
d[i]=new LinkedList<Integer>();
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i);
for(int i=0;i<n;i++) if(b[i]!=a[i]) d[b[i]].add(i);
for(int i=0;i<m;i++) c[i]=sc.nextInt();
if(d[c[m-1]].isEmpty()) {System.out.println("NO");continue;}
for(int i=m-1;i>-1;i--)
if(d[c[i]].isEmpty()) ans[i]=ans[i+1];
else ans[i]=d[c[i]].pollLast();
for(int i=0;i<m;i++) {
a[ans[i]]=c[i];
sb.append(ans[i]+1+" ");
}
for(int i=0;i<n;i++) if(a[i]!=b[i]) {System.out.println("NO"); continue l;}
System.out.println("YES\n"+sb);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 7aee0541ccee77bc500959bc5aee4bbc | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;
public class CF_699_C {
static FastScanner fs = new FastScanner();
public static void main(String[] args) {
int T=fs.nextInt();
StringBuilder ss=new StringBuilder();
for(int tt=0;tt<T;++tt) {
int n=fs.nextInt(), m=fs.nextInt();
int []a=fs.readArray(n), b=fs.readArray(n), c=fs.readArray(m);
ArrayList<Integer> arr=new ArrayList<>();
ArrayList<ArrayList<Integer>> v=new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> v1=new ArrayList<ArrayList<Integer>>();
boolean cond=false;
for(int i=0;i<=n;++i) {
v.add(new ArrayList<>());
v1.add(new ArrayList<>());
}
for(int i=0;i<n;++i) {
if(a[i]!=b[i]) {
arr.add(b[i]);
v.get(b[i]).add(i+1);
}
if(b[i]==c[m-1]) cond=true;
v1.get(b[i]).add(i+1);
}
int []pos=new int[n+1];
Arrays.fill(pos, 0);
HashMap<Integer, Integer> mp=new HashMap<>();
for(int i:arr) {
if(mp.containsKey(i)) {
mp.replace(i, mp.get(i)+1);
}
else {
mp.put(i, 1);
}
}
ArrayList<Integer> g=new ArrayList<>();
for(int i:c) {
if(mp.containsKey(i) && mp.get(i)!=0) {
g.add(i);
mp.replace(i, mp.get(i)-1);
}
}
//Collections.reverse(g);
ArrayList<Integer> col=new ArrayList<>();
int idx=0;
for(int i:c) {
if(idx==g.size()) break;
else {
col.add(g.get(idx));
if(i==g.get(idx)) {
++idx;
}
}
}
ArrayList<Integer> gg=new ArrayList<Integer>();
int yu=0;
for(int i:col) {
int r=v.get(i).get(pos[i]);
gg.add(r);
a[r-1]=i;
if(i==c[yu]) ++pos[i];
++yu;
}
for(int i=0;i<n;++i) {
if(a[i]!=b[i]) cond=false;
}
if(cond==false) {
ss.append("No\n");
continue;
}
if(gg.size()==m) {
ss.append("Yes\n");
for(int i:gg) ss.append(i).append(" ");
ss.append("\n");
continue;
}
int kr=0;
int loc=0;
if(gg.size()!=m) {
for(int i:b) {
++loc;
if(i==c[m-1]) {
break;
}
}
}
while(gg.size()!=m) gg.add(loc);
ss.append("Yes\n");
for(int i:gg) ss.append(i).append(" ");
ss.append("\n");
}
System.out.println(ss);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | b66f1fce094d08222c39b685037d702c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;
public class CF_699_C {
static FastScanner fs = new FastScanner();
public static void main(String[] args) {
int T=fs.nextInt();
for(int tt=0;tt<T;++tt) {
int n=fs.nextInt(), m=fs.nextInt();
int []a=fs.readArray(n), b=fs.readArray(n), c=fs.readArray(m);
ArrayList<Integer> arr=new ArrayList<>();
ArrayList<ArrayList<Integer>> v=new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> v1=new ArrayList<ArrayList<Integer>>();
boolean cond=false;
for(int i=0;i<=n;++i) {
v.add(new ArrayList<>());
v1.add(new ArrayList<>());
}
for(int i=0;i<n;++i) {
if(a[i]!=b[i]) {
arr.add(b[i]);
v.get(b[i]).add(i+1);
}
if(b[i]==c[m-1]) cond=true;
v1.get(b[i]).add(i+1);
}
int []pos=new int[n+1];
Arrays.fill(pos, 0);
HashMap<Integer, Integer> mp=new HashMap<>();
for(int i:arr) {
if(mp.containsKey(i)) {
mp.replace(i, mp.get(i)+1);
}
else {
mp.put(i, 1);
}
}
ArrayList<Integer> g=new ArrayList<>();
for(int i:c) {
if(mp.containsKey(i) && mp.get(i)!=0) {
g.add(i);
mp.replace(i, mp.get(i)-1);
}
}
//Collections.reverse(g);
ArrayList<Integer> col=new ArrayList<>();
int idx=0;
for(int i:c) {
if(idx==g.size()) break;
else {
col.add(g.get(idx));
if(i==g.get(idx)) {
++idx;
}
}
}
ArrayList<Integer> gg=new ArrayList<Integer>();
int yu=0;
for(int i:col) {
int r=v.get(i).get(pos[i]);
gg.add(r);
a[r-1]=i;
if(i==c[yu]) ++pos[i];
++yu;
}
for(int i=0;i<n;++i) {
if(a[i]!=b[i]) cond=false;
}
if(cond==false) {
System.out.println("No");
continue;
}
if(gg.size()==m) {
System.out.println("Yes");
for(int i:gg) System.out.print(i+" ");
System.out.println();
continue;
}
int kr=0;
int loc=0;
if(gg.size()!=m) {
for(int i:b) {
++loc;
if(i==c[m-1]) {
break;
}
}
}
while(gg.size()!=m) gg.add(loc);
System.out.println("Yes");
for(int i:gg) System.out.print(i+" ");
System.out.println();
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 0f472329bbc684da183e1b034c2e599c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.Scanner;
import java.util.LinkedList;
public class p1418C{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
l: for(int t=sc.nextInt();t-->0;) {
StringBuilder sb=new StringBuilder();
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m];
LinkedList<Integer> d[]=new LinkedList[n+1];
d[n]=new LinkedList<Integer>();
for(int i=0;i<n;i++) {
d[i]=new LinkedList<Integer>();
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i);
for(int i=0;i<n;i++) if(b[i]!=a[i]) d[b[i]].add(i);
for(int i=0;i<m;i++) c[i]=sc.nextInt();
if(d[c[m-1]].isEmpty()) {System.out.println("NO");continue;}
for(int i=m-1;i>-1;i--)
if(d[c[i]].isEmpty()) ans[i]=ans[i+1];
else ans[i]=d[c[i]].pollLast();
for(int i=0;i<m;i++) {
a[ans[i]]=c[i];
sb.append(ans[i]+1+" ");
}
for(int i=0;i<n;i++) if(a[i]!=b[i]) {System.out.println("NO"); continue l;}
System.out.println("YES\n"+sb);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 42b4dbbec84711b11ca6b97489cec78a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.LinkedList;
public class p1418C{
public static void main(String[] args) {
scanner sc=new scanner(System.in);
l: for(int t=sc.nextInt();t-->0;) {
StringBuilder sb=new StringBuilder();
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m];
LinkedList<Integer> d[]=new LinkedList[n+1];
for(int i=0;i<n;i++) {
d[i]=new LinkedList<Integer>();
a[i]=sc.nextInt();
}
d[n]=new LinkedList<Integer>();
for(int i=0;i<n;i++) if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i);
for(int i=0;i<n;i++) if(b[i]!=a[i]) d[b[i]].add(i);
for(int i=0;i<m;i++) c[i]=sc.nextInt();
if(d[c[m-1]].isEmpty()) {System.out.println("NO");continue;}
for(int i=m-1;i>-1;i--)
if(d[c[i]].isEmpty()) ans[i]=ans[i+1];
else ans[i]=d[c[i]].pollLast();
for(int i=0;i<m;i++) {
a[ans[i]]=c[i];
sb.append(ans[i]+1+" ");
}
for(int i=0;i<n;i++) if(a[i]!=b[i]) {System.out.println("NO"); continue l;}
System.out.println("YES\n"+sb);
}
}
static class scanner {
private InputStream instream;
private byte[] buff = new byte[1000];
private int curChar, numChars;
public scanner(InputStream instream) {
this.instream=instream;
}
private boolean spacedChar(int c) {return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;}
private int read() {
if(numChars==-1) throw new InputMismatchException();
if (curChar>=numChars) {
curChar = 0;
try {numChars = instream.read(buff);}
catch (IOException e) {throw new InputMismatchException();}
if (numChars <= 0) return -1;
}
return buff[curChar++];
}
public int nextInt() {
int c=read();
while(spacedChar(c)) c = read();
int sgn = 1;
if (c=='-') {
sgn=-1;
c=read();
}
int res=0;
do{
if (c<'0'||c>'9') throw new InputMismatchException();
res*=10;
res+=c-'0';
c=read();
}while (!spacedChar(c));
return res*sgn;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 4962ff8a3ea535943dcf10d49f12b15a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class K {
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = scan.nextInt();
for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt(), m = scan.nextInt();
int[] initial = new int[n], desire = new int[n], painter = new int[m];
ArrayDeque<Integer>[] finished = new ArrayDeque[n + 1];
ArrayDeque<Integer>[] queue = new ArrayDeque[n + 1];
int[] freq = new int[n + 1];
int ptr = 1;
int[] ans = new int[m];
for(int i = 0; i < n; i++) initial[i] = scan.nextInt();
for(int i = 0; i < n; i++) desire[i] = scan.nextInt();
for(int i = 0; i < m; i++) painter[i] = scan.nextInt();
for(int i = 0; i <= n; i++) {
finished[i] = new ArrayDeque<>();
queue[i] = new ArrayDeque<>();
}
for(int i = 0; i < n; i++) {
if(initial[i] == desire[i]) {
finished[initial[i]].addLast(i);
}
else queue[desire[i]].addLast(i);
}
for(int i = 0; i < m; i++) {
if(!queue[painter[i]].isEmpty() || !finished[painter[i]].isEmpty()) {
freq[painter[i]]++;
}
}
for(int i = 0; i < m; i++) {
int curr = painter[i];
if(queue[curr].isEmpty()) {
if(finished[curr].isEmpty()) {
while(ptr <= n && freq[ptr] == 0) ptr++;
if(ptr > n) {
out.println("NO");
return;
}
ans[i] = queue[ptr].isEmpty() ? finished[ptr].pollFirst() : queue[ptr].pollFirst();
queue[ptr].addLast(ans[i]);
}
else {
ans[i] = finished[curr].peekFirst();
freq[curr]--;
}
}
else {
ans[i] = queue[curr].pollFirst();
finished[curr].addLast(ans[i]);
freq[curr]--;
}
}
for(int i = 1; i <= n; i++) {
if(!queue[i].isEmpty()) {
out.println("NO");
return;
}
}
out.println("YES");
for(int i = 0; i < m; i++) out.printf("%d ", ans[i] + 1);
out.println();
}
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | e88510138d21a0d30fc237362110bfdf | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
static long m = (long) (1e9 + 7);
public static void main(String[] args) throws IOException {
Scanner scn = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int T = scn.ni(), tcs = 0;
C: while (tcs++ < T) {
int n = scn.ni(), m = scn.ni();
int a[] = scn.nia(n), b[] = scn.nia(n), c[] = scn.nia(m);
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<>();
HashMap<Integer, Integer> thm = new HashMap<>();
int req[] = new int[n + 1];
int pre[] = new int[n + 1];
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (hm.containsKey(b[i])) {
hm.get(b[i]).add(i);
} else {
ArrayList<Integer> al = new ArrayList<>();
al.add(i);
hm.put(b[i], al);
}
} else
thm.put(b[i], i);
}
HashMap<Integer, Integer> ans = new HashMap<>();
boolean ok = true;
HashSet<Integer> hs = new HashSet<>();
for (int i = 0; i < m; i++) {
int col = c[i];
if (hm.containsKey(col)) {
ArrayList<Integer> al = hm.get(col);
int id = al.remove(al.size() - 1);
if (al.size() == 0) {
hm.remove(col);
}
ans.put(i, id);
thm.put(col, id);
}
else if (thm.containsKey(col)) {
ans.put(i, thm.get(col));
} else {
hs.add(i);
}
}
ArrayList<Integer> lll = new ArrayList<>();
if (hm.size() != 0) {
ok = false;
}
if (hs.size() > 0 && !thm.containsKey(c[m - 1]))
ok = false;
if (!ok) {
sb.append("No\n");
continue;
}
else {
sb.append("YES\n");
int id = thm.get(c[m - 1]);
for (Integer key : hs) {
ans.put(key, id);
}
for (int i = 0; i < m; i++) {
sb.append((ans.get(i) + 1) + " ");
}
sb.append("\n");
}
}
out.print(sb);
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int ni() throws IOException {
return Integer.parseInt(next());
}
public long nl() throws IOException {
return Long.parseLong(next());
}
public int[] nia(int n) throws IOException {
int a[] = new int[n];
String sa[] = br.readLine().split(" ");
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(sa[i]);
return a;
}
public long[] nla(int n) throws IOException {
long a[] = new long[n];
String sa[] = br.readLine().split(" ");
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(sa[i]);
return a;
}
public int[] sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int v : a)
l.add(v);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
return a;
}
public long[] sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long v : a)
l.add(v);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
return a;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | d14af401bbc00b629da78fd5917c98c5 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Solution{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
for(int z=0;z<t;z++){
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for(int i=0;i<n;i++) a[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] b = new int[n];
for(int i=0;i<n;i++) b[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] c = new int[m];
for(int i=0;i<m;i++) c[i] = Integer.parseInt(st.nextToken());
TreeMap<Integer,LinkedList<Integer>> need = new TreeMap<Integer,LinkedList<Integer>>();
int[] have = new int[n+1];
Arrays.fill(have,-1);
for(int i=0;i<n;i++){
if(a[i]!=b[i]){
LinkedList<Integer> list;
if(need.containsKey(b[i])) list = need.get(b[i]);
else{
list = new LinkedList<Integer>();
need.put(b[i],list);
}
list.add(i);
}
have[b[i]] = i;
}
int[] res = new int[m];
Arrays.fill(res,-1);
boolean ans = true;
int sup = -1;
for(int j=m-1;j>=0;j--){
if(need.containsKey(c[j])){
LinkedList<Integer> list = need.get(c[j]);
int ind = list.removeFirst();
if(list.isEmpty()) need.remove(c[j]);
res[j] = ind;
sup = ind;
}else{
if(sup!=-1){
res[j] = sup;
}else if(have[c[j]]!=-1){
res[j] = have[c[j]];
sup = res[j];
}else{
ans = false;
break;
}
}
}
if(ans&&need.isEmpty()){
out.println("YES");
for(int i=0;i<m;i++){
res[i]++;
out.print(res[i]+" ");
}
out.println("");
}else out.println("NO");
}
out.flush();
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 4c2d550c0c9fc638054a27c3e36285a8 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
public class temp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n];
int[] p = new int[k];
int[] ans = new int[k];
for(int i=0;i<n;i++){
arr1[i] = sc.nextInt();
}
for(int i=0;i<n;i++){
arr2[i] = sc.nextInt();
}
for(int i=0;i<k;i++){
p[i] = sc.nextInt();
}
ArrayList<Integer>[] total = new ArrayList[n+1];
ArrayList<Integer>[] a = new ArrayList[n+1];
for(int i=0;i<n+1;i++){
total[i] = new ArrayList<Integer>();
a[i] = new ArrayList<Integer>();
}
for(int i=0;i<n;i++){
total[arr2[i]].add(i+1);
if(arr1[i]!=arr2[i]){
a[arr2[i]].add(i+1);
}
}
ArrayList<Integer> tem = new ArrayList<>();
for(int i=0;i<k;i++){
if(!a[p[i]].isEmpty()){
ans[i] = a[p[i]].get(0);
a[p[i]].remove(0);
}
else{
if(!total[p[i]].isEmpty()){
ans[i] = total[p[i]].get(0);
}
else tem.add(i);
}
}
boolean b1 = false;
for(int i=1;i<n+1;i++){
if(!a[i].isEmpty()){
System.out.println("NO");
b1 = true;
break;
}
}
if(b1) continue;
if(ans[k-1]==0){
System.out.println("NO");
}
else{
System.out.println("YES");
for(int i: tem){
ans[i] = ans[k-1];
}
for(int i=0;i<k;i++){
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 92271a4bc9b6cdd45cecb4cccbbc7827 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class A {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
public static void main(String args[]) throws IOException {
int t=in.nextInt();
StringBuilder res=new StringBuilder();
Stack<Integer>index[]=new Stack [100001];
for(int i=0;i<100001;i++)
{
index[i]=new Stack();
}
loop:
while(t-->0)
{
int n=in.nextInt();
int m=in.nextInt();
int a[]=in.readintarray(n);
int b[]=in.readintarray(n);
int c[]=in.readintarray(m);
HashSet<Integer>set=new HashSet<>();
for(int i=0;i<n;i++)
{
index[a[i]].clear();
}
for(int i=0;i<m;i++)
{
index[c[i]].clear();
}
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
index[b[i]].add(i+1);
}
}
int shu=-1;
for(int i=0;i<n;i++)
{
if(a[i]==b[i]&&a[i]==c[m-1])
{
shu=i+1;
}
}
boolean test=true;
int prev=0;
if(shu==-1&&index[c[m-1]].size()>0)
{
prev=index[c[m-1]].peek();
}
else if(shu>=0)
{
prev=shu;
}
if(index[c[m-1]].size()==0&&shu==-1)
{
res.append("NO\n");
continue loop;
}
ArrayList<Integer>list=new ArrayList<>();
for(int i=m-1;i>=0;i--)
{
if(index[c[i]].size()==0)
{
list.add(prev);
}
else
{
//print("in else");
prev=index[c[i]].pop();
list.add(prev);
a[prev-1]=c[i];
//print("i= "+(prev-1)+" : "+b[prev-1]+" c="+c[i]);
}
//print(prev+";"+i);
//for(int x: b)
//{
//System.out.print(x+" ");
//}
}
for(int i=0;i<n;i++)
{
//print(b[i]+" ");
if(a[i]!=b[i])
{
res.append("NO\n");
continue loop;
}
}
res.append("YES\n");
for(int i=list.size()-1;i>=0;i--)
{
res.append(list.get(i)+" ");
}
res.append("\n");
}
print(res);
}
static long power(long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static < E > void print(E res)
{
System.out.println(res);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5901075a0a4d293da1af8610f0d4bf9a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
public class Coding {
private static BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
private static int[] po = new int[20];
public static void main(String[] args) {
try {
run();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void run() throws Exception {
InputModule inp = new InputModule();
OutputModule out = new OutputModule();
int t = inp.cinInt();
po[0] = 1;
for (int i = 1; i < 20; ++i) po[i] = po[i - 1] * 2;
while (t > 0) {
int[] xx = inp.cinIntArray(2);
int n = xx[0];
int m = xx[1];
int[] a = inp.cinIntArray(n);
int[] b = inp.cinIntArray(n);
int[] c = inp.cinIntArray(m);
int[] idxx = new int[n+1];
for(int i=1;i<=n;++i) {
idxx[i] = -1;
}
Map<Integer, LinkedBlockingQueue> mp = new HashMap<>();
for(int i=1;i<=n;++i) {
mp.put(i, new LinkedBlockingQueue());
}
for(int i=0;i<n;++i) {
if (a[i]!=b[i]) {
LinkedBlockingQueue<Integer> q = mp.get(b[i]);
q.add(i+1);
mp.put(b[i], q);
} else {
idxx[b[i]] = i+1;
}
}
boolean ok = true;
int[] pa = new int[m];
int lastP = -1;
int lastPW = -1;
List<Integer> lft = new ArrayList<>();
for(int i=0;i<m;++i) {
LinkedBlockingQueue<Integer> q = mp.get(c[i]);
if (!q.isEmpty()) {
int idx = q.poll();
pa[i] = idx;
lastP = i;
lastPW = idx;
idxx[c[i]] = Math.max(idxx[c[i]], idx);
} else {
if (idxx[c[i]]!=-1) {
pa[i] = idxx[c[i]];
lastP = i;
lastPW = idxx[c[i]];
} else {
lft.add(i);
}
}
}
if (lft.size()>0) {
if (lastP<lft.get(lft.size()-1)) {
ok = false;
} else {
for(int i=0;i<lft.size();++i) {
pa[lft.get(i)] = lastPW;
}
}
}
for(int i=1;i<=n;++i) {
LinkedBlockingQueue q = mp.get(i);
if (!q.isEmpty()) ok = false;
}
if (!ok) out.printString("NO");
else {
out.printString("YES");
out.printIntArray(pa);
}
t--;
}
}
private static class InputModule {
private int cinInt() throws Exception {
return Integer.parseInt(bi.readLine().split(" ")[0].trim());
}
private long cinLong() throws Exception {
return Long.parseLong(bi.readLine().split(" ")[0].trim());
}
private Double cinDouble() throws Exception {
return Double.parseDouble(bi.readLine().split(" ")[0].trim());
}
private String cinString() throws Exception {
return bi.readLine();
}
private int[] cinIntArray(int n) throws Exception {
String input = bi.readLine();
String[] values = input.split(" ");
int[] ar = new int[n];
for (int i = 0; i < n; ++i) {
ar[i] = Integer.parseInt(values[i]);
}
return ar;
}
private int[] cinIntArray() throws Exception {
String input = bi.readLine();
String[] values = input.split(" ");
int[] ar = new int[values.length];
for (int i = 0; i < values.length; ++i) {
ar[i] = Integer.parseInt(values[i]);
}
return ar;
}
private long[] cinLongArray(int n) throws Exception {
String input = bi.readLine();
String[] values = input.split(" ");
long[] ar = new long[n];
for (int i = 0; i < n; ++i) {
ar[i] = Long.parseLong(values[i]);
}
return ar;
}
private String[] cinStringArray(int n) throws Exception {
return bi.readLine().split(" ");
}
}
private static class OutputModule {
private void printInt(int ans) throws Exception {
writer.append(ans + "\n");
writer.flush();
}
private void printLong(long ans) throws Exception {
writer.append(ans + "\n");
writer.flush();
}
private void printDouble(Double ans) throws Exception {
writer.append(String.format("%.10f", ans));
writer.append("\n");
writer.flush();
}
private void printString(String ans) throws Exception {
writer.append(ans + "\n");
writer.flush();
}
private void printIntArray(int[] ans) throws Exception {
for (int i = 0; i < ans.length; ++i) {
writer.append(ans[i] + " ");
}
writer.append("\n");
writer.flush();
}
private void printLongArray(long[] ans) throws Exception {
for (int i = 0; i < ans.length; ++i) {
writer.append(ans[i] + " ");
}
writer.append("\n");
writer.flush();
}
private void printIntList(List<Integer> list) throws Exception {
for (int i = 0; i < list.size(); ++i) {
writer.append(list.get(i) + " ");
}
writer.append("\n");
writer.flush();
}
private void printLongList(List<Long> list) throws Exception {
for (int i = 0; i < list.size(); ++i) {
writer.append(list.get(i) + " ");
}
writer.append("\n");
writer.flush();
}
private void printIntMatrix(int[][] mat, int n, int m) throws Exception {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
writer.append(mat[i][j] + " ");
}
writer.append("\n");
}
writer.flush();
}
private void printLongMatrix(long[][] mat, int n, int m) throws Exception {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
writer.append(mat[i][j] + " ");
}
writer.append("\n");
}
writer.flush();
}
private void printPoint(Point p) throws Exception {
writer.append(p.x + " " + p.y + "\n");
writer.flush();
}
private void printPoints(List<Point> p) throws Exception {
for (Point pp : p) {
writer.append(pp.x + " " + pp.y + "\n");
}
writer.flush();
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | ee6c73b05b65cfef6667a3babd636f3a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | // package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class HH3 {
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] Args)throws Exception{
FastReader scan=new FastReader(System.in);
int t=1;
t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int m=scan.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
int[] brr=new int[n];
Map<Integer,ArrayList<Integer>> changes=new HashMap<>();
Map<Integer,ArrayList<Integer>> chan=new HashMap<>();
int[] bin=new int[n+1];
Arrays.fill(bin,-1);
int rem=0;
for(int i=0;i<n;i++){
brr[i]=scan.nextInt();
if(brr[i]!=arr[i]){
changes.putIfAbsent(brr[i],new ArrayList<>());
changes.get(brr[i]).add(i);
chan.putIfAbsent(brr[i],new ArrayList<>());
chan.get(brr[i]).add(i);
rem++;
}
bin[brr[i]]=i;
}
int[] c=new int[m];
int[] cin=new int[m];
Arrays.fill(cin,-1);
int fi1=-1;
for(int i=0;i<m;i++){
c[i]=scan.nextInt();
if(bin[c[i]]!=-1){
fi1=i;
}
}
int fi=-1;
for(int i=m-1;i>=0&&rem>0;i--){
int cur=c[i];
if(changes.containsKey(cur)){
ArrayList<Integer> ins=changes.get(cur);
if(ins.size()>0){
rem--;
cin[i]=ins.get(ins.size()-1);
ins.remove(ins.size()-1);
if(fi==-1){
fi=i;
}
}
}
}
fi=Math.max(fi,fi1);
if(rem!=0){
out.println("NO");
}else{
// for(int i=0;i<n;i++){
// out.println(cin[i]);
// }
if(fi!=-1){
if(cin[fi]==-1){
cin[fi]=bin[c[fi]];
}
}
for(int i=0;i<fi;i++){
if(cin[i]==-1){
cin[i]=cin[fi];
}
}
for(int i=fi+1;i<m;i++){
if(cin[i]==-1) {
cin[i] = bin[c[i]];
}
}
boolean pos=true;
StringBuilder ans=new StringBuilder();
for(int i=0;i<m;i++){
if(cin[i]==-1){
pos=false;
}
ans.append((cin[i]+1)+" ");
}
if(pos){
out.println("YES");
out.println(ans);
}else{
out.println("NO");
}
}
}
out.flush();
out.close();
}
static class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 3e2e07eba13c8ff30ff072eb517a948c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.StringTokenizer;
public class c {
public static void main(String[] args) {
FastScanner scan=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int t=scan.nextInt();
t:for(int tt=0;tt<t;tt++) {
int n=scan.nextInt(), m=scan.nextInt();
int[] a=new int[n], b=new int[n];
int[] need=new int[n+1];
Arrays.fill(need,-1);
for(int i=0;i<n;i++) {
a[i]=scan.nextInt();
}
for(int i=0;i<n;i++) {
b[i]=scan.nextInt();
if(need[b[i]]==-1) need[b[i]]=0;
if(a[i]!=b[i]) need[b[i]]++;
}
int[] painters=new int[m];
int[] fp=new int[n+1];
for(int i=0;i<m;i++) {
painters[i]=scan.nextInt();
fp[painters[i]]++;
}
if(need[painters[m-1]]==-1) {
out.println("NO");
continue;
}
int last=painters[m-1];
ArrayDeque<Integer>[] q=new ArrayDeque[n+1];
for(int i=0;i<=n;i++) q[i]=new ArrayDeque<>();
for(int i=0;i<=n;i++) {
if(need[i]>fp[i]) {
out.println("NO");
continue t;
}
}
int lastid=-1;
for(int i=0;i<n;i++) {
if(b[i]==last&&lastid==-1) {
if(need[b[i]]>0&&a[i]!=b[i]) lastid=i+1;
else if(need[b[i]]==0) lastid=i+1;
}
else if(a[i]!=b[i]) q[b[i]].offer(i+1);
}
out.println("YES");
// System.out.println(q[2]+" "+q[3]);
for(int i=0;i<m;i++) {
if(q[painters[i]].size()>0) {
out.print(q[painters[i]].poll());
}
else {
out.print(lastid);
}
out.print(" ");
}
out.println();
}
out.close();
}
/*
1
4 6
2 1 1 1
2 2 2 2
2 2 4 4 4 2
*/
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
public int nextInt() {return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 1a7d6d89489e93b89b66b8f711e00fbe | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
import java.awt.*;
public class FencePainting
{
public static void main(String[] args) throws IOException
{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = Integer.parseInt(f.readLine());
for (int z = 0; z<t; z++)
{
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
HashMap<Integer, Stack<Integer>> map = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>();
st = new StringTokenizer(f.readLine());
for (int i=0; i<n; i++)
{
a[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(f.readLine());
for (int i=0; i<n; i++)
{
b[i] = Integer.parseInt(st.nextToken());
if (b[i]!=a[i])
{
if (map.containsKey(b[i]))
{
Stack<Integer> stack = map.get(b[i]);
stack.push(i);
map.put(b[i], stack);
}
else
{
Stack<Integer> stack = new Stack<>();
stack.push(i);
map.put(b[i], stack);
}
}
else
{
if (!map2.containsKey(a[i]))
{
map2.put(a[i], i);
}
}
}
st = new StringTokenizer(f.readLine());
for (int i=0; i<m; i++)
{
c[i] = Integer.parseInt(st.nextToken());
}
int[] ans = new int[m];
boolean works = true;
for (int i= m-1; i>=0; i--)
{
if (map.containsKey(c[i]))
{
Stack<Integer> stack = map.get(c[i]);
ans[i] = stack.pop();
if (stack.isEmpty())
map.remove(c[i]);
else
map.put(c[i], stack);
}
else if (map2.containsKey(c[i]))
{
ans[i] = map2.get(c[i]);
}
else if (i<m-1)
{
ans[i] = ans[i+1];
}
else
{
works = false;
}
}
if (map.size()>0 || !works)
out.println("NO");
else
{
out.println("YES");
for (int i=0; i<m; i++)
{
out.print((ans[i]+1) + " ");
}
out.println();
}
}
out.close();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5bd97a4e3600769909502053c41e9a45 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.StringTokenizer;
public class Problem699C {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int t = Integer.parseInt(reader.readLine());
for (int tt = 0; tt < t; tt++) {
StringTokenizer info = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(info.nextToken());
int m = Integer.parseInt(info.nextToken());
int[] a = new int[n];
StringTokenizer aT = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(aT.nextToken()) - 1;
}
int[] b = new int[n];
StringTokenizer bT = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(bT.nextToken()) - 1;
}
int[] c = new int[m];
StringTokenizer cT = new StringTokenizer(reader.readLine());
for (int i = 0; i < m; i++) {
c[i] = Integer.parseInt(cT.nextToken()) - 1;
}
Deque<Integer>[] colorsNeeded = new ArrayDeque[n];
for (int i = 0; i < n; i++) {
colorsNeeded[i] = new ArrayDeque<>();
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
colorsNeeded[b[i]].add(i);
}
}
// the last painter must be in one of b[i]
int lastPainterIndex = -1;
for (int i = 0; i < n; i++) {
if (c[m - 1] == b[i]) {
lastPainterIndex = i;
break;
}
}
if (lastPainterIndex == -1) {
writer.println("NO");
continue;
}
List<Integer> answer = new ArrayList<>();
for (int i = 0; i < m; i++) {
// we need to paint c[i]
Deque<Integer> x = colorsNeeded[c[i]];
if (x.isEmpty()) {
answer.add(-1);
} else {
answer.add(x.poll());
}
}
if (answer.get(answer.size() - 1) != -1) {
lastPainterIndex = answer.get(answer.size() - 1);
}
// replace all -1 with lastPainterIndex
for (int i = 0; i < answer.size(); i++) {
if (answer.get(i) == -1) {
answer.set(i, lastPainterIndex);
}
}
// ensure colorsNeeded is empty
boolean done = true;
for (int i = 0; i < n; i++) {
if (!colorsNeeded[i].isEmpty()) {
done = false;
break;
}
}
if (done) {
writer.println("YES");
for (int i = 0; i < answer.size() - 1; i++) {
writer.print(answer.get(i) + 1);
writer.print(' ');
}
writer.println(answer.get(answer.size() - 1) + 1);
} else {
writer.println("NO");
}
}
reader.close();
writer.close();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 3b87a22e0352671a189933a33d5c3e8c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
public class code {
// static class pair implements Comparable<pair>{
// int x;int y;
//
// public pair(int x,int y){
// this.x=x;
// this.y=y;
// }
//
//
// @Override
// public int compareTo(pair o) {
// return this.x-o.x;
// }
// }
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() {
int n=sc.nextInt(),m=sc.nextInt();
int[] a=new int[n+1],b=new int[n+1];
for (int i = 1; i <=n; i++) {
a[i]=sc.nextInt();
}
for (int i = 1; i <=n; i++) {
b[i]=sc.nextInt();
}
HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();
HashMap<Integer,Integer> ext=new HashMap<>();
for (int i = 1; i <=n; i++) {
int x=a[i];
int y=b[i];
if(x!=y){
if(!map.containsKey(y)) map.put(y,new ArrayList<>());
map.get(y).add(i);
}
else{
ext.put(y,i);
}
}
int [] am=new int[m];
for (int i = 0; i <m; i++) {
am[i]=sc.nextInt();
}
int[] ans=new int[m];
for (int i = m-1; i>=0 ; i--) {
int temp=am[i];
if(!map.containsKey(temp)) {
if(ext.containsKey(temp)){
ans[i]=ext.get(temp);
}
else {
if (i == m - 1) {
System.out.println("NO");
return;
} else {
ans[i] = ans[i + 1];
}
}
}
else{
if(map.get(temp).size()==0){
ans[i]=ans[i+1];
}
else
ans[i]=map.get(temp).remove(map.get(temp).size()-1);
}
}
for (ArrayList<Integer> e: map.values()) {
if(e.size()>0){
//System.out.println(e);
System.out.println("NO");
return;
}
}
System.out.println("YES");
for (int i = 0; i < m; i++) {
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 719c43255e6a13ef2aa99586c3bd68de | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | /**
* Create by lgc on 2022/5/19
*/
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
public class CodeForces {
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static void solve() throws IOException {
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n + 1], b = new int[n + 1], c = new int[m + 1], ans = new int[m + 1];
for (int i = 1; i <= n; i ++ ) a[i] = sc.nextInt();
for (int i = 1; i <= n; i ++ ) b[i] = sc.nextInt();
for (int i = 1; i <= m; i ++ ) c[i] = sc.nextInt();
int cnt = 0;
Map<Integer, List<Integer>> cmap = new HashMap<>();
Map<Integer, Integer> fmap = new HashMap<>();
for (int i = 1; i <= n; i ++ ) {
fmap.put(b[i], i);
if (b[i] != a[i]) {
if (cmap.get(b[i]) == null) cmap.put(b[i], new ArrayList<>());
cmap.get(b[i]).add(i);
cnt++;
}
}
int pre = n + 1;
for (int i = m; i >= 1; i -- ) {
if (cmap.get(c[i]) != null && !cmap.get(c[i]).isEmpty()) {
int t = cmap.get(c[i]).get(0);
ans[i] = t;
cmap.get(c[i]).remove(0);
cnt --;
pre = t;
} else if (pre == n + 1) {
if (!fmap.containsKey(c[i])) {
pw.println("NO");
return;
}
ans[i] = fmap.get(c[i]);
pre = ans[i];
} else ans[i] = pre;
}
if (cnt > 0) {
pw.println("NO");
return;
}
pw.println("YES");
for (int i = 1; i <= m; i ++ ) pw.printf("%d ", ans[i]);
pw.println();
}
public static void main(String[] args) throws IOException {
int tests = sc.nextInt();
for (int test = 0; test < tests; test++) solve();
pw.flush();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(new FileInputStream(s)));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 4db2723552174bb4dd574af52c6585f5 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0) {
int n=sc.nextInt();
int m=sc.nextInt();
int[] a=new int[n+1];
int[] b=new int[n+1];
int[] c=new int[m+1];
HashMap<Integer,LinkedList<Integer>> map=new HashMap<>();
HashMap<Integer,Integer> hashMap=new HashMap<>();
for(int i=1;i<=n;i++)
a[i]=sc.nextInt();
for(int i=1;i<=n;i++) {
b[i]=sc.nextInt();
if(b[i]!=a[i]) {
if(!map.containsKey(b[i]))
map.put(b[i],new LinkedList<>());
map.get(b[i]).addLast(i);
}
else
hashMap.put(b[i],i);
}
for(int i=1;i<=m;i++)
c[i]=sc.nextInt();
int[] ans=new int[m+1];
int last=0;
for(int i=m;i>0;i--) {
if(map.containsKey(c[i])) {
last=map.get(c[i]).removeLast();
if(map.get(c[i]).isEmpty())
map.remove(c[i]);
}
else if(i==m && last==0) {
if(hashMap.containsKey(c[i]))
last=hashMap.get(c[i]);
else
break;
}
ans[i]=last;
}
boolean flag=true;
for(int i=1;i<=m;i++) {
if(ans[i]==0) {
flag=false;
break;
}
a[ans[i]]=c[i];
}
if(flag) {
for(int i=1;i<=n;i++) {
if(a[i]!=b[i]) {
flag=false;
break;
}
}
if(flag) {
System.out.println("YES");
for(int i=1;i<=m;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
else
System.out.println("NO");
}
else
System.out.println("NO");
}
sc.close();
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | d96cc5ddeaa38d950dea70bed3d5edf3 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class FencePainting{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = sc.readIntArray(n);
int brr[] = sc.readIntArray(n);
int crr[] = sc.readIntArray(m);
HashMap<Integer,Queue<Integer>> map = new HashMap<>();
HashMap<Integer,Integer> mp = new HashMap<>();
for(int i = 0;i<n;i++){
if(arr[i]!=brr[i]){
if(!map.containsKey(brr[i])){
map.put(brr[i],new ArrayDeque<>());
}
map.get(brr[i]).add(i+1);
}else{
mp.put(arr[i],i+1);
}
}
int ans[] = new int[m];
int lst = -1;
for(int i = m-1;i>=0;i--){
int cur = crr[i];
if(map.containsKey(cur)){
ans[i] = map.get(cur).remove();
if(map.get(cur).size()==0) map.remove(cur);
if(lst==-1) lst = ans[i];
}else if(lst!=-1){
ans[i] = lst;
}else if(mp.containsKey(cur)){
ans[i] = mp.get(cur);
if(lst==-1) lst = ans[i];
}else{
out.println("NO");
return;
}
}
if(map.size()!=0){
out.println("NO");
return;
}
out.println("YES");
for(int i = 0;i<m;i++)out.print(ans[i]+" ");
out.println();
}
static class Pair implements Comparable<Pair>{
long a;
long b;
Pair(long aa,long bb){
a = aa;
b = bb;
}
public int compareTo(Pair p){
return Long.compare(this.b,p.b);
}
}
static void reverse(int arr[]){
int i = 0;int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
// solve2();
// solve3();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 6603277b7e580325266b7937066bcc05 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Main {
static FastReader sc;
static StringBuilder sb = new StringBuilder();
static final long MOD = 998244353;
public static void main(String args[]) throws InterruptedException {
sc = new FastReader();
int t=sc.nextInt();
while (t-- > 0) {
int n,m;
n=sc.nextInt();m=sc.nextInt();
int[]a=readArray(n);
int[]b=readArray(n);
int[]c=readArray(m);
Map<Integer,ArrayList<Integer>>fence=new HashMap<>();
Map<Integer,ArrayList<Integer>>worker=new HashMap<>();
for(int i=0;i<n;i++){
if(a[i]!=b[i]){
if(!fence.containsKey(b[i]))
fence.put(b[i],new ArrayList<>());
fence.get(b[i]).add(i);
}
}
for(int i=0;i<m;i++){
if(!worker.containsKey(c[i]))
worker.put(c[i],new ArrayList<>());
worker.get(c[i]).add(i);
}
boolean check=true;
Set<Integer>extraWorker=new HashSet<>();
for(Integer key:fence.keySet()){
List<Integer> fences = fence.get(key);
if(!worker.containsKey(key)||worker.get(key).size()<fences.size()){
check=false;
break;
}
}
if(check==false) {
sb.append("No\n");
continue;
}
int []ans=new int[m];
Arrays.fill(ans,-1);
for(Integer key:worker.keySet()){
if(!fence.containsKey(key)){
extraWorker.addAll(worker.get(key));
}
else{
List<Integer>fences=fence.get(key);
List<Integer>workers=worker.get(key);
for(int i=0;i<workers.size();i++){
if(fences.size()<=i)
ans[workers.get(i)]=fences.get(fences.size()-1);
else
ans[workers.get(i)]=fences.get(i);
}
}
}
if(extraWorker.contains(m-1)){
int pos=-1;
for(int i=0;i<n;i++){
if(b[i]==c[m-1]){
pos=i;
break;
}
}
if(pos==-1){
sb.append("No\n");
continue;
}
else{
for(int w:extraWorker){
ans[w]=pos;
}
}
}else{
for(int w:extraWorker){
ans[w]=ans[m-1];
}
}
sb.append("Yes\n");
for(int i=0;i<m;i++){
sb.append((ans[i]+1)+" ");
}
sb.append("\n");
}
out.println(sb.toString());
}
public static void reverse(int[] b) {
int n=b.length;
int i=0,j=n-1;
while(i<j){
int k=b[i];
b[i]=b[j];
b[j]=k;
i++;
j--;
}
return;
}
public static boolean nextPermutation(int[]a){
int n=a.length;
if(n==0||n==1)return false;
int pos=-1;
for(int i=n-2;i>=0;i--){
if(a[i]<a[i+1]){
pos=i;
int rep=-1;
for(int j=i+1;j<n;j++){
if(a[j]>a[pos]){
rep=j;
}
}
int k=a[pos];
a[pos]=a[rep];
a[rep]=k;
break;
}
}
int i=pos+1,j=n-1;
while(i<j){
int k=a[i];
a[i]=a[j];
a[j]=k;
i++;
j--;
}
return (pos==-1)?false:true;
}
public static long powerMOD(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
x %= MOD;
res %= MOD;
res = (res * x)%MOD;
}
// y must be even now
y = y >> 1; // y = y/2
x%= MOD;
x = (x * x)%MOD; // Change x to x^2
}
return res%MOD;
}
public static long power(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
res = (res * x);
}
// y must be even now
y = y >> 1; // y = y/
x = (x * x);
}
return res;
}
public static void printArray(int[] a){
int n=a.length;
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public static int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
static int cntBits(long n){
int cnt=0;
while(n>0){
if(n%2==1)
cnt++;
n/=2;
}
return cnt;
}
static double p_dist(long x1,long y1,long x2,long y2){
double dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
dist=Math.sqrt(dist);
return dist;
}
static void swap(int[]a,int i,int j){
int k=a[i];
a[i]=a[j];
a[j]=k;
}
static void printArray(char[]a){
int n=a.length;
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void sort(char[] a) {
ArrayList<Character> l=new ArrayList<>();
for (char i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
static int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) sc.nextInt();
}
return res;
}
static long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = sc.nextLong();
}
return res;
}
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
public static class Pair implements Comparable<Pair>{
public long a;
public long b;
public String s;
public char dir;
public long l;
Pair(int a , int b,char dir){
this.a = a;
this.b = b;
this.dir=dir;
}
Pair(long a , long b){
this.a = a;
this.b = b;
}
Pair(int a , String b){
this.a = a;
this.s = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return (a == pair.a && b == pair.b);
}
@Override
public int hashCode() {
return Objects.hash(a,b);
}
@Override
public String toString(){
return "("+a + "," + b+")";
}
@Override
public int compareTo(Pair pair) {
if((a == pair.a && b == pair.b))
return 0;
return 1;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | a21755741e506bf054e86307ace78cfd | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class new1{
public static void main(String[] args) throws IOException{
FastReader s = new FastReader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int m = s.nextInt();
ArrayList<ArrayList<Integer> > aList = new ArrayList<ArrayList<Integer> >(n + 1);
for (int j = 0; j <= n ; j++) {
ArrayList<Integer> list = new ArrayList<>();
aList.add(list);
}
int[] initial = new int[n];
int[] fin = new int[n];
for(int i = 0; i < n; i++) {
initial[i] = s.nextInt();
}
for(int i = 0; i < n; i++) {
int val = s.nextInt();
fin[i] = val;
if(val != initial[i])aList.get(val).add(i + 1);
}
for(int i = 0; i < n; i++) {
int val = fin[i];
if(val == initial[i]) aList.get(val).add(i + 1);
}
int[] ind = new int[n + 1];
int[] ans = new int[m];
int[] painters = new int[m];
for(int i = 0; i < m; i++) {
int val = s.nextInt();
painters[i] = val;
}
for(int i = 1; i <= n; i++) {
ArrayList<Integer> aq = aList.get(i);
//System.out.println(aq.toString());
}
if((aList.get(painters[m - 1]).size() == 0)){
System.out.println("NO");
continue;
}
for(int i = 0; i < m; i++) {
int val = painters[i];
if(aList.get(val).size() == 0) {
int fence = aList.get(painters[m - 1]).get(ind[painters[m - 1]]);
ans[i] = fence;
initial[fence - 1] = painters[i];
continue;
}
int fence = aList.get(val).get(ind[val]);
initial[fence - 1] = painters[i];
ind[val] = (ind[val] + 1) % aList.get(val).size();
ans[i] = fence;
}
int flag = 1;
// System.out.println(Arrays.toString(initial));
// System.out.println(Arrays.toString(fin));
for(int i = 0; i < n; i++) {
if(initial[i] != fin[i]) flag = 0;
}
if(flag == 0) {
System.out.println("NO");
continue;
}
else {
System.out.println("YES");
for(int i = 0; i < ans.length; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
}
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | bd9def4f785746f8d814369671606aec | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | //package CodeForces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
public class FencePainting1481C {
public static void main (String[] Z) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder op = new StringBuilder();
StringTokenizer stz;
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
stz = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stz.nextToken());
int m = Integer.parseInt(stz.nextToken());
int[] a = new int[n+1];
int[] b = new int[n+1];
int[] c = new int[m];
int[] colsReq = new int[n+1];
ArrayList<Stack<Integer>> indices = new ArrayList<>(n+1);
for(int i = 0 ; i <= n ; ++i) indices.add(new Stack<>());
stz = new StringTokenizer(br.readLine());
for(int i = 1 ; i <= n ; ++i) a[i] = Integer.parseInt(stz.nextToken());
stz = new StringTokenizer(br.readLine());
for(int i = 1 ; i <= n ; ++i){
b[i] = Integer.parseInt(stz.nextToken());
if(b[i] != a[i]) {
indices.get(b[i]).push(i);
}
}
stz = new StringTokenizer(br.readLine());
for(int i = 0 ; i < m ; ++i) c[i] = Integer.parseInt(stz.nextToken());
int lastPainterColor = c[m-1];
int index = -1;
for (int i = 1 ; i <= n; i++) {
if(lastPainterColor == b[i]) {
index = i;
if(a[i] != b[i]) {
break;
}
}
}
if(index == -1) {
op.append("NO\n");
continue;
}
ArrayList<Integer> ans = new ArrayList<>(n);
for (int i = 0 ; i < m ; i++) {
int color = c[i];
if(indices.get(color).size() != 0) {
int ind = indices.get(color).pop();
ans.add(ind);
a[ind] = color;
}
else {
a[index] = color;
ans.add(index);
}
}
boolean possible = true;
for (int i = 1 ; i <= n ; i++) {
if(a[i] != b[i]) {
possible = false;
break;
}
}
if(possible) {
op.append("YES\n");
for (int e: ans) op.append(e + " ");
op.append("\n");
}
else {
op.append("NO\n");
}
}
System.out.println(op);
// END OF CODE
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | c7214a2df5305614f25e3e2ca619a7ee | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
//Scanner f = new Scanner(new File("uva.in"));
//Scanner f = new Scanner(System.in);
//BufferedReader f = new BufferedReader(new FileReader("uva.in"));
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = Integer.parseInt(f.readLine());
while(t-- > 0) {
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(f.readLine());
HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, ArrayList<Integer>> replace = new HashMap<>();
for(int i = 0; i < n; i++) {
int b = Integer.parseInt(st.nextToken());
map.put(b, i+1);
if(b != a[i]) {
replace.putIfAbsent(b, new ArrayList<>());
replace.get(b).add(i+1);
}
}
st = new StringTokenizer(f.readLine());
int[] c = new int[m];
for(int i = 0; i < m; i++) {
c[i] = Integer.parseInt(st.nextToken());
}
int[] res = new int[m];
int dummy = 0;
boolean flag = false;
for(int i = m-1; i >= 0; i--) {
if(replace.containsKey(c[i])) {
res[i] = replace.get(c[i]).remove(0);
if(replace.get(c[i]).size() == 0) {
replace.remove(c[i]);
}
dummy = res[i];
} else if(dummy > 0) {
res[i] = dummy;
} else if(map.containsKey(c[i])) {
res[i] = map.get(c[i]);
dummy = res[i];
} else {
flag = true;
break;
}
}
if(flag || replace.size() > 0) {
out.println("NO");
} else {
out.println("YES");
out.print(res[0]);
for(int i = 1; i < m; i++) {
out.print(" " + res[i]);
}
out.println();
}
}
f.close();
out.close();
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5d860c57a4b28c6b45653815deab5873 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class C2 {
public void solve() {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(), m = in.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
LinkedList<Integer> [] map = new LinkedList[n+1];
map[n] = new LinkedList<>();
for (int i = 0; i<n; i++) {
a[i] = in.nextInt();
map[i] = new LinkedList<>();
}
for (int i = 0; i<n; i++) {
b[i] = in.nextInt();
}
for (int i = 0; i<m; i++) {
c[i] = in.nextInt();
}
for (int i = 0; i<n; i++) {
if (a[i] == b[i]) {
map[b[i]].add(i);
}
}
for (int i = 0; i<n; i++) {
if (a[i] != b[i]) {
map[b[i]].add(i);
}
}
if (map[c[m-1]].isEmpty()) {
out.println("NO");
continue;
}
int paint[] = new int[m];
for (int i = m - 1; i >= 0; i--) {
if (map[c[i]].isEmpty()) {
paint[i] = paint[i + 1];
} else {
paint[i] = map[c[i]].pollLast();
}
}
for (int i = 0; i<m; i++) {
a[paint[i]] = c[i];
}
boolean answer = true;
for (int i = 0; i<n; i++) {
if (a[i] != b[i])
answer = false;
}
if (!answer) {
out.println("NO");
continue;
}
out.println("YES");
for (int i = 0; i<m; i++) {
out.print((paint[i] + 1) + " ");
}
out.println();
}
}
String input = "";
String output = "";
FastScanner in;
PrintWriter out;
void run() throws Exception {
if (input.length() == 0) {
in = new FastScanner(System.in);
} else {
in = new FastScanner(new File(input));
}
if (output.length() == 0) {
out = new PrintWriter(System.out);
} else {
out = new PrintWriter(new File(output));
}
solve();
out.close();
}
public static void main(String[] args) throws Exception {
new C2().run();
}
class FastScanner {
BufferedReader bf;
StringTokenizer st;
public FastScanner(InputStream is) {
bf = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File fr) throws FileNotFoundException {
bf = new BufferedReader(new FileReader(fr));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
} catch (IOException ex) {
ex.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readIntArray(int length) {
int arr[] = new int[length];
for (int i = 0; i<length; i++)
arr[i] = nextInt();
return arr;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 550b4b582d393ea31310d9ba434cd027 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public long process(int x, int y) {
long result = 0;
for (int i=1; i*i < x; i++) {
result += Math.max(0, Math.min(y, x/i-1) - i);
}
return result;
}
public void solve() {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(), m = in.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
HashMap<Integer, ArrayList<Integer>> paintList1 = new HashMap<>();
HashMap<Integer, Integer> paintList3 = new HashMap<>();
for (int i = 0; i<n; i++)
a[i] = in.nextInt();
for (int i = 0; i<n; i++) {
b[i] = in.nextInt();
}
for (int i = 0; i<n; i++) {
if (a[i] != b[i]) {
ArrayList<Integer> list = paintList1.get(b[i]);
if (list == null) {
list = new ArrayList<>();
}
list.add(i);
paintList1.put(b[i], list);
} else {
paintList3.put(a[i], i + 1);
}
}
HashMap<Integer, ArrayList<Integer>> paintList2 = new HashMap<>();
for (int i = 0; i<m; i++) {
c[i] = in.nextInt();
ArrayList<Integer> list = paintList2.get(c[i]);
if (list == null) {
list = new ArrayList<>();
}
list.add(i);
paintList2.put(c[i], list);
}
boolean answer = true;
int paint[] = new int[m];
Arrays.fill(paint, -1);
for (Integer p: paintList1.keySet()) {
if (!paintList2.containsKey(p) || paintList2.get(p).size() < paintList1.get(p).size()) {
answer = false;
}
}
if (!answer) {
out.println("NO");
continue;
}
for (Integer p: paintList2.keySet()) {
if (!paintList1.containsKey(p)) {
if (paintList3.containsKey(p)) {
for (int j : paintList2.get(p)) {
paint[j] = paintList3.get(p);
}
}
} else if (paintList2.get(p).size() >= paintList1.get(p).size()) {
int ind = 0;
for (int j : paintList2.get(p)) {
int i = paintList1.get(p).get(ind);
paint[j] = i + 1;
if (ind + 1 < paintList1.get(p).size())
ind++;
}
} else {
answer = false;
}
}
if (!answer) {
out.println("NO");
continue;
}
int last = -1;
for (int i = m-1; i>=0; i--) {
if (paint[i] != -1) {
last = paint[i];
} else {
if (last != -1)
paint[i] = last;
else {
answer = false;
break;
}
}
}
if (!answer) {
out.println("NO");
continue;
}
out.println("YES");
for (int i = 0; i<m; i++) {
out.print(paint[i] + " ");
}
out.println();
}
}
String input = "";
String output = "";
FastScanner in;
PrintWriter out;
void run() throws Exception {
if (input.length() == 0) {
in = new FastScanner(System.in);
} else {
in = new FastScanner(new File(input));
}
if (output.length() == 0) {
out = new PrintWriter(System.out);
} else {
out = new PrintWriter(new File(output));
}
solve();
out.close();
}
public static void main(String[] args) throws Exception {
new C().run();
}
class FastScanner {
BufferedReader bf;
StringTokenizer st;
public FastScanner(InputStream is) {
bf = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File fr) throws FileNotFoundException {
bf = new BufferedReader(new FileReader(fr));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
} catch (IOException ex) {
ex.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readIntArray(int length) {
int arr[] = new int[length];
for (int i = 0; i<length; i++)
arr[i] = nextInt();
return arr;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 42df43f82314df12901320ae6b851791 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class M{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
l: while(t-->0) {
StringBuilder sb=new StringBuilder();
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m];
LinkedList<Integer> d[]=new LinkedList[n+1];
d[n]=new LinkedList<Integer>();
for(int i=0;i<n;i++) {
d[i]=new LinkedList<Integer>();
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {//d:b中的所有颜色和对应的位置
if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i);
}
for(int i=0;i<n;i++) {
if(b[i]!=a[i]) d[b[i]].add(i);
}
for(int i=0;i<m;i++) {
c[i]=sc.nextInt();
}
if(d[c[m-1]].isEmpty()) {//判断最后一个是否可以画
System.out.println("NO");
continue;
}
for(int i=m-1;i>-1;i--)
if(d[c[i]].isEmpty()) ans[i]=ans[i+1];
else ans[i]=d[c[i]].pollLast();
for(int i=0;i<m;i++) {
a[ans[i]]=c[i];
sb.append(ans[i]+1+" ");
}
for(int i=0;i<n;i++) if(a[i]!=b[i]) {
System.out.println("NO");
continue l;
}
System.out.println("YES\n"+sb);
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 1502501bbbe999da94818aa38967436b | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
import java.util.StringTokenizer;
public class FencePainting {
static final int MAXN = 1000_006;
static final long MOD = (long) 1e9 + 7;
public static void main(String[] args) throws IOException {
MyScanner s = new MyScanner();
Print p = new Print();
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int m = s.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
for (int i = 0; i < n; i++)
a[i] = s.nextInt();
for (int i = 0; i < n; i++)
b[i] = s.nextInt();
for (int i = 0; i < m; i++)
c[i] = s.nextInt();
HashMap<Integer, LinkedList<Integer>> hm1 = new HashMap<>();
HashMap<Integer, LinkedList<Integer>> hm2 = new HashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
LinkedList<Integer> ll = hm1.getOrDefault(b[i], new LinkedList<Integer>());
ll.add(i);
hm1.put(b[i], ll);
} else {
LinkedList<Integer> ll = hm2.getOrDefault(b[i], new LinkedList<Integer>());
ll.add(i);
hm2.put(b[i], ll);
}
}
boolean ans = true;
int[] sol = new int[m];
int tmp = -1;
Arrays.fill(sol, -1);
for (int i = m - 1; i >= 0; i--) {
if (hm2.containsKey(c[i]) && hm2.get(c[i]).size() > 0) {
sol[i] = hm2.get(c[i]).removeLast();
if (hm2.get(c[i]).size() == 0)
hm2.remove(c[i]);
if (tmp == -1)
tmp = sol[i];
} else if (hm1.containsKey(c[i]) && hm1.get(c[i]).size() > 0) {
sol[i] = hm1.get(c[i]).removeLast();
if (hm1.get(c[i]).size() == 0)
hm1.remove(c[i]);
if (tmp == -1)
tmp = sol[i];
} else if (tmp != -1) {
sol[i] = tmp;
} else {
ans = false;
break;
}
}
if (ans && hm2.size() == 0) {
p.println("Yes");
for (int i = 0; i < m; i++) {
p.print((sol[i] + 1) + " ");
}
p.println("");
} else {
p.println("No");
}
}
p.close();
}
public static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int a, int b) {
this.first = a;
this.second = b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + first;
result = prime * result + second;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (first != other.first)
return false;
if (second != other.second)
return false;
return true;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return o.first - first;
}
}
public static class Helper {
long MOD = (long) 1e9 + 7;
int MAXN = 1000_006;;
Random rnd;
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public Helper() {
}
public static int[] sieve;
public static ArrayList<Integer> primes;
public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for (i = 2; i < MAXN; ++i)
if (sieve[i] == 0) {
primes.add(i);
for (j = i; j < MAXN; j += i) {
sieve[j] = i;
}
}
}
public static long[] factorial;
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i)
factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null)
setFactorial();
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n)
return 0;
if (factorial == null)
setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
public long[] getLongArray(int size, MyScanner s) throws Exception {
long[] ar = new long[size];
for (int i = 0; i < size; ++i)
ar[i] = s.nextLong();
return ar;
}
public int[] getIntArray(int size, MyScanner s) throws Exception {
int[] ar = new int[size];
for (int i = 0; i < size; ++i)
ar[i] = s.nextInt();
return ar;
}
public int[] getIntArray(String s) throws Exception {
s = s.trim().replaceAll("\\s+", " ");
String[] strs = s.split(" ");
int[] arr = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
return arr;
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public long max(long[] ar) {
long ret = ar[0];
for (long itr : ar)
ret = Math.max(ret, itr);
return ret;
}
public int max(int[] ar) {
int ret = ar[0];
for (int itr : ar)
ret = Math.max(ret, itr);
return ret;
}
public long min(long[] ar) {
long ret = ar[0];
for (long itr : ar)
ret = Math.min(ret, itr);
return ret;
}
public int min(int[] ar) {
int ret = ar[0];
for (int itr : ar)
ret = Math.min(ret, itr);
return ret;
}
public long sum(long[] ar) {
long sum = 0;
for (long itr : ar)
sum += itr;
return sum;
}
public long sum(int[] ar) {
long sum = 0;
for (int itr : ar)
sum += itr;
return sum;
}
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1)
ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
}
static class Print {
private BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | d577f54a2e259798116171502d6b1c70 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for( int tc=1 ; tc<=t ; ++tc ) {
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
ArrayList<Integer> list ;
HashMap<Integer,ArrayList<Integer>> unmatch = new HashMap<Integer, ArrayList<Integer>>();
for( int ind=0 ; ind<n ; ++ind ) {
a[ind] = sc.nextInt();
}
for( int ind=0 ; ind<n ; ++ind ) {
b[ind] = sc.nextInt();
if( b[ind]!=a[ind] ) {
if( !unmatch.containsKey(b[ind]) ) {
list = new ArrayList<Integer>();
list.add(ind);
unmatch.put(b[ind],list);
}else {
list = unmatch.get(b[ind]);
list.add(ind);
}
}
}
for( int ind=0 ; ind<m ; ++ind ) {
c[ind] = sc.nextInt();
}
int last = -1;
if( unmatch.containsKey(c[m-1]) ) {
list = unmatch.get(c[m-1]);
last = list.get(0);
list.remove(0);
if( list.isEmpty() ) {
unmatch.remove(c[m-1]);
}
}else {
for( int ind=0 ; ind<n ; ++ind ) {
if( b[ind]==c[m-1] ) {
last = ind ;
break;
}
}
}
if( last==-1 ) {
out.println("NO");
continue;
}
int ans[] = new int[m];
for( int ind=0 ; ind<m-1 ; ++ind ) {
if( unmatch.containsKey(c[ind]) ) {
list = unmatch.get(c[ind]);
if( list.isEmpty() ) {
ans[ind] = last+1;
continue;
}
ans[ind] = list.get(0)+1;
list.remove(0);
}else {
ans[ind] = last+1;
}
}
ans[m-1] = last+1;
boolean isInavlid = false;
for( int key : unmatch.keySet() ) {
list = unmatch.get(key);
if( !list.isEmpty() ) {
out.println("NO");
isInavlid = true;
break;
}
}
if( !isInavlid ) {
out.println("YES");
for( int ind=0 ; ind<m ; ++ind ) {
out.print(ans[ind]+" ");
}
out.println();
}
}
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 7cbb82fb6144b3f3507d6ea58931bc87 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
public class Ideone {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = ni(br.readLine());
int []a,b,c, sa , hc,ta;// = new int[1];
char [] aa;
char ca[], ch;
int n,m,i,j,k,l,x,f, lastHouseIndex;
Map<Integer, List<Integer>> ctpMap = new HashMap<>();
List<Integer> tl = null;
x = -1;
while (t-- > 0) {
ta = nia(br);
n = ta[0];
m = ta[1];
a = nia(br);
b = nia(br);
c = nia(br);
f =0;
lastHouseIndex = -1;
ctpMap = new HashMap<>();
for(i =0; i < m; i++) {
tl = ctpMap.get(c[i]);
if(tl == null) {
tl = new LinkedList<>();
ctpMap.put(c[i], tl);
}
tl.add(i);
}
for(i =0; i < n; i++) {
if(b[i] == c[m-1]) {
if(lastHouseIndex == -1)
lastHouseIndex = i;
else if(a[i] != b[i]) {
lastHouseIndex = i;
}
}
}
if(lastHouseIndex == -1)
System.out.println("NO");
else {
Arrays.fill(c, lastHouseIndex);
for(i =0; i < n; i++) {
if(a[i] != b[i]) {
tl = ctpMap.get(b[i]);
if(tl == null || tl.isEmpty()) {
lastHouseIndex = -1;
break;
}
c[tl.remove(0)] = i;
}
}
if(lastHouseIndex == -1)
System.out.println("NO");
else {
System.out.println("YES");
for(i =0; i < m; i++) {
System.out.print(c[i] +1 + " ");
}
System.out.println();
}
}
}
}
private static long lcm(long i, long j) {
return i *j / gcd(i,j);
}
private static long gcd(long i, long j) {
return j ==0 ? i : gcd(j, i %j);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
static Integer[] nIa(BufferedReader br) throws Exception {
String sa[] = br.readLine().split(" ");
Integer[] a = new Integer[sa.length];
for (int i = 0; i < sa.length; i++) {
a[i] = ni(sa[i]);
}
return a;
}
static int[] nia(BufferedReader br) throws Exception {
String sa[] = br.readLine().split(" ");
int[] a = new int[sa.length];
for (int i = 0; i < sa.length; i++) {
a[i] = ni(sa[i]);
}
return a;
}
static long[] nla(BufferedReader br) throws Exception {
String sa[] = br.readLine().split(" ");
long[] a = new long[sa.length];
for (int i = 0; i < sa.length; i++) {
a[i] = nl(sa[i]);
}
return a;
}
static int ni(String s) {
return Integer.parseInt(s);
}
static long nl(String s) {
return Long.parseLong(s);
}
static float nf(String s) {
return Float.parseFloat(s);
}
static double nd(String s) {
return Double.parseDouble(s);
}
////// specific to problem
static int[][] nia(BufferedReader br, int a[][]) throws Exception {
for (int i = 0; i < a.length; i++) {
String sa[] = br.readLine().split(" ");
a[i][0] = ni(sa[0]);
a[i][1] = ni(sa[1]);
}
return a;
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 6fb9242a19450863e3ab3f3504bf28ae | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 1e9 + 10;
static final long OO = (long) 2e18 + 10;
static final int MOD = (int) 1e9 + 7;
static void solve() {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = in.nextInt();
}
int[] c = new int[m];
LinkedList<Integer>[] color = new LinkedList[n + 1]; Arrays.setAll(color, i -> new LinkedList<>());
for (int i = 0; i < m; i++) {
c[i] = in.nextInt();
color[c[i]].add(i);
}
int[] ans = new int[m]; Arrays.fill(ans, -1);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (color[b[i]].size() == 0) {
out.println("NO");
return;
}
int j = color[b[i]].pollFirst();
ans[j] = i;
}
}
if (ans[m - 1] == -1) {
for (int i = 0; i < n; i++) {
if (b[i] == c[m - 1]) {
ans[m - 1] = i;
break;
}
}
}
if (ans[m - 1] == -1) {
out.println("NO");
return;
}
out.println("YES");
for (int i = 0; i < m; i++) {
if (ans[i] == -1)
ans[i] = ans[m - 1];
out.print((ans[i] + 1) + " ");
}
out.println();
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int T = 1;
T = in.nextInt();
while (T-- > 0)
solve();
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 14a3b071657352d13d5545a8f4fe2bea | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 1e9 + 10;
static final long OO = (long) 2e18 + 10;
static final int MOD = (int) 1e9 + 7;
static void solve() {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt() - 1;
}
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = in.nextInt() - 1;
}
int[] c = new int[m];
for (int i = 0; i < m; i++) {
c[i] = in.nextInt() - 1;
}
boolean[] isColor = new boolean[n];
int[] first = new int[n];
Arrays.fill(first, -1);
ArrayList<Integer>[] which = new ArrayList[n];
Arrays.setAll(which, i -> new ArrayList<>());
for (int i = 0; i < n; i++) {
isColor[b[i]] = true;
if (first[b[i]] == -1)
first[b[i]] = i;
if (a[i] != b[i])
which[b[i]].add(i);
}
if (!isColor[c[m - 1]]) {
out.println("NO");
return;
}
int[] ptr = new int[n];
int[] ans = new int[m];
for (int i = m - 1; i >= 0; i--) {
if (isColor[c[i]]) {
if (which[c[i]].size() > 0) {
ptr[c[i]] = Math.min(ptr[c[i]], which[c[i]].size() - 1);
ans[i] = which[c[i]].get(ptr[c[i]]++);
} else
ans[i] = first[c[i]];
} else
ans[i] = ans[i + 1];
}
for (int i = 0; i < n; i++) {
if (which[i].size() > 0 && ptr[i] < which[i].size()) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < m; i++) {
out.print((ans[i] + 1) + " ");
}
out.println();
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int T = 1;
T = in.nextInt();
while (T-- > 0)
solve();
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 88d5379c5d4c75fbcb43c59a1ef05a73 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static void calc(int n){
int i,j;
dp[n-1]=0;
if(n>1)
dp[n-2]=1;
for(i=n-3;i>=0;i--){
long ind=n-i-1;
dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[];
static int visited[][];
static ArrayList<Integer> adj[];
//static int dp[][][];
static int R,G,B,MOD=1000000007;
static int[] par,size;
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static char ch[][], ch1[][];
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0;
t=in.nextInt();
//t=1;
while(t-->0){
tno++;
int n,m,flag=0;
n=in.nextInt();
m=in.nextInt();
int arr[]=new int[n];
int brr[]=new int[n];
int crr[]=new int[m];
//int freq1[]=new int[100002];
// int freq1[]=new int[100002];
HashMap<Integer,Integer> hm=new HashMap<>();
//HashMap<Integer,Integer> bm=new HashMap<>();
HashMap<Integer,LinkedList<Integer>> cm=new HashMap<>();
for(i=0;i<n;i++){
arr[i]=in.nextInt();
}
for(i=0;i<n;i++){
brr[i]=in.nextInt();
//bm.put(brr[i],bm.getOrDefault(brr[i],0)+1);
if(brr[i]!=arr[i]){
//freq1[brr[i]]++;
hm.put(brr[i],hm.getOrDefault(brr[i],0)+1);
if(cm.containsKey(brr[i])){
LinkedList<Integer> lin=cm.get(brr[i]);
lin.add(i);
cm.put(brr[i],lin);
}else{
LinkedList<Integer> lin=new LinkedList<>();
lin.add(i);
cm.put(brr[i],lin);
}
}
}
for(i=0;i<m;i++){
crr[i]=in.nextInt();
if(hm.containsKey(crr[i])){
hm.put(crr[i],hm.get(crr[i])-1);
if(hm.get(crr[i])==0){
hm.remove(crr[i]);
}
}
}
int ind=0;
for(i=n-1;i>=0;i--){
if(brr[i]==crr[m-1]){
ind=i;
break;
}
}
if(i<0){
w.println("NO");
continue;
}
if(!hm.isEmpty()){
w.println("NO");
continue;
}
if(cm.containsKey(crr[m-1]))
ind=cm.get(crr[m-1]).getLast();
LinkedList<Integer> li=new LinkedList<>();
for(i=0;i<m;i++){
if(cm.containsKey(crr[i])){
LinkedList<Integer> lin=cm.get(crr[i]);
li.add(lin.pollFirst());
if(lin.isEmpty()){
cm.remove(crr[i]);
}else{
cm.put(crr[i],lin);
}
}else{
li.add(ind);
}
}
w.println("YES");
while(!li.isEmpty()){
w.print(li.pollFirst()+1+" ");
}
w.println();
}
w.close();
}
}
// 21 22
// 15 17 10 20 3 9 6 20 6 12 19 8 13 17 8 14 19 19 9 1 4
// 7 1 10 20 3 9 6 20 11 12 19 8 13 17 8 19 4 19 9 1 9
// 7 17 7 13 11 21 16 8 19 11 18 9 16 14 8 19 16 7 4 21 21 1 | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | f3cffe67848d1585d573d794c3f2b94f | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static FastScanner sc = new FastScanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
static ArrayList<TreeSet<Integer>> mapA,mapB;
public static void main(String[] args) throws Exception {
int Q = sc.nextInt();
for(int i = 0; i < Q; i++) solve();
pw.flush();
}
static void solve(){
int N = sc.nextInt();
int M = sc.nextInt();
mapA = new ArrayList<>();
mapB = new ArrayList<>();
int[] A = sc.nextIntArray(N);
int[] B = sc.nextIntArray(N);
for(int i = 0; i <= N; i++){
mapA.add(new TreeSet<>());
mapB.add(new TreeSet<>());
}
for(int i = 0; i < N; i++){
mapA.get(A[i]).add(i);
if(A[i] != B[i]){
mapB.get(B[i]).add(i);
}
}
int[] C = sc.nextIntArray(M);
int[] ans = new int[M];
Deque<Integer> stock = new ArrayDeque<>();
for(int i = 0; i < M; i++){
if(mapB.get(C[i]).size() > 0){
int Key = mapB.get(C[i]).first();
ans[i] = Key+1;
mapA.get(A[Key]).remove(Key);
A[Key] = C[i];
mapA.get(C[i]).add(Key);
mapB.get(C[i]).remove(Key);
while(stock.size() > 0){
ans[stock.poll()] = Key+1;
}
}else if(mapA.get(C[i]).size() > 0){
//pw.println(mapA.get(C[i]));
int Key = mapA.get(C[i]).first();
ans[i] = Key+1;
while(stock.size() > 0){
ans[stock.poll()] = Key+1;
}
}else{
stock.add(i);
}
}
boolean flg = true;
for(int i = 0; i < N; i++){
if(A[i] != B[i]){
flg = false;
break;
}
}
if(flg && stock.size() == 0){
pw.println("YES");
pw.println(ArraysToString(ans));
}else{
pw.println("NO");
}
return;
}
private static String ArraysToString(int[] arr){
String s = Arrays.toString(arr);
s = s.replaceAll(",","");
s = s.substring(1,s.length()-1);
return s;
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String[] nextArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 05acc5b8f6b2045e4ff444f300944154 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static void main(String[] args) throws Exception{
//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
PrintWriter out = new PrintWriter(System.out);
for(int z=0;z<t;z++){
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] a = new int[n+1];
int[] b = new int[n+1];
int[] c = new int[m+1];
int nb = -1;
st = new StringTokenizer(br.readLine());
for(int i=1;i<=n;i++) {
a[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for(int i=1;i<=n;i++){
b[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for(int i=1;i<=m;i++){
c[i] = Integer.parseInt(st.nextToken());
}
LinkedList<Integer>[] l = new LinkedList[n+1];
for(int i=1;i<=n;i++) l[i] = new LinkedList<Integer>();
int[] exist = new int[n+1];
for(int i=1;i<=n;i++){
if(a[i] != b[i]) l[b[i]].add(i);
else{
exist[a[i]] = i;
}
}
//for(int i=1;i<=n;i++) out.print(exist[i]+" ");
//out.println("");
boolean ok = true;
for(int i=m;i>0;i--){
if(l[c[i]].isEmpty()){
if(nb==-1) {
if(exist[c[i]]!=0){
nb = exist[c[i]];
c[i] = nb;
}else{
ok = false;
break;
}
}else{
c[i] = nb;
}
}else{
nb = l[c[i]].poll();
c[i] = nb;
}
}
for(int i=1;i<=n;i++){
if(!l[i].isEmpty()) ok = false;
}
if(!ok) out.println("NO");
else {
out.println("YES");
for(int i=1;i<=m;i++) out.print(c[i]+" ");
out.println("");
}
}
out.flush();
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 060a6252a8fb2a0371f6fc66afa1b5d9 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
private static boolean TESTS = true;
private final Input in;
private final PrintStream out;
public Solution(final Input in, final PrintStream out) {
this.in = in;
this.out = out;
}
public void solve() {
for (int test = 1, tests = TESTS ? in.nextInt() : 1; test <= tests; test++) {
final int n = in.nextInt();
final int m = in.nextInt();
final int[] a = in.nextInts(n);
final int[] b = in.nextInts(n);
final int[] c = in.nextInts(m);
final int[] dx = new int[m];
Arrays.fill(dx, -1);
final HashMap<Integer, List<Pair>> pairs = new HashMap<>();
for (int i = 0; i < m; i++) {
final Pair p = new Pair(c[i], i);
final List<Pair> list = pairs.computeIfAbsent(p.color, integer -> new ArrayList<>(2));
list.add(p);
}
boolean ok = true;
int max = -1;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
final List<Pair> list = pairs.get(b[i]);
if (list == null) {
ok = false;
break;
}
final Pair pair = list.remove(list.size() - 1);
dx[pair.index] = i + 1;
max = Math.max(max, pair.index);
if (list.isEmpty()) {
pairs.remove(b[i]);
}
}
}
if (ok) {
for (int i = n - 1; i >= 0; i--) {
final List<Pair> list = pairs.remove(b[i]);
if (list == null) {
continue;
}
for (final Pair pair : list) {
dx[pair.index] = i + 1;
max = Math.max(max, pair.index);
}
}
}
if (ok) {
for (final List<Pair> list : pairs.values()) {
for (final Pair pair : list) {
if (pair.index < max) {
dx[pair.index] = dx[max];
} else {
ok = false;
break;
}
}
if (!ok) {
break;
}
}
}
if (ok) {
out.println("Yes");
for (int i = 0; i < m; i++) {
out.print(dx[i] + " ");
}
out.println();
} else {
out.println("No");
}
}
}
private static class Pair {
int color;
int index;
public Pair(final int color, final int index) {
this.color = color;
this.index = index;
}
}
public static void main(final String[] args) {
final Input in = new Input();
final PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
try {
new Solution(in, out).solve();
} finally {
out.flush();
}
}
private static final class Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
public String nextString() {
try {
while (!tokenizer.hasMoreTokens()) {
final String line = reader.readLine();
tokenizer = new StringTokenizer(line, " ");
}
return tokenizer.nextToken();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public long nextLong() {
return Long.parseLong(nextString());
}
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextInts(final int size) {
final int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongs(final int size) {
final long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubles(final int size) {
final double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 410bb051ab9631310c1a01cbaa152329 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static long inf = Long.MAX_VALUE;
public static void main(String[] args) throws IOException {
for (int iiiiii = nextInt(); iiiiii > 0; iiiiii--) {
int n = nextInt();
int m = nextInt();
boolean[] wasb = new boolean[n];
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
for (int i = 0; i < n; i++) a[i] = nextInt();
for (int i = 0; i < n; i++) b[i] = nextInt();
for (int i = 0; i < m; i++) c[i] = nextInt();
boolean was = false;
int idx = -1;
int hi = -1;
for (int i = 0; i < n; i++) {
if (b[i] == c[m - 1]) {
was = true;
hi = i;
}
if (b[i] != a[i] && c[m - 1] == b[i]) idx = i;
}
if (!was) out.println("NO");
else {
if (idx == -1) idx = hi;
boolean answered = false;
ArrayList<Integer>[] list = new ArrayList[n];
for (int i = 0; i < n; i++) list[i] = new ArrayList<>();
int[] ans = new int[m];
for (int i = 0; i < m - 1; i++) {
list[c[i] - 1].add(i);
}
for (int i = 0; i < n; i++) {
if (b[i] != a[i] && i != idx) {
if (list[b[i] - 1].size() == 0) {
out.println("NO");
answered = true;
break;
} else {
ans[list[b[i] - 1].remove(0)] = i + 1;
}
}
}
if (!answered) {
out.println("YES");
for (int i : ans) {
if (i == 0) out.print((idx + 1) + " ");
else out.print(i + " ");
}
out.println();
}
}
}
out.close();
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static StringTokenizer in = new StringTokenizer("");
static class pii implements Comparable<pii> {
int f;
int s;
pii(int x, int t) {
this.f = x;
this.s = t;
}
public int compareTo(pii o) {
if (f != o.f) return Integer.compare(f, o.f);
return Integer.compare(s, o.s);
}
}
static class pll implements Comparable<pll> {
long f;
long s;
pll(long f, long s) {
this.f = f;
this.s = s;
}
public int compareTo(pll o) {
if (f != o.f) return Long.compare(f, o.f);
return Long.compare(s, o.s);
}
}
static long nodlong(long a, long b) {
while (a % b != 0) {
long h = b;
b = a % b;
a = h;
}
return Math.abs(b);
}
public static boolean hasNext() throws IOException {
if (in.hasMoreTokens()) return true;
String s;
while ((s = br.readLine()) != null) {
in = new StringTokenizer(s);
if (in.hasMoreTokens()) return true;
}
return false;
}
public static String nextToken() throws IOException {
while (!in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 504fa72687509dbd104ca37ecda0605b | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class c {
public static int nPost;
public static int nPaint;
public static int[] init;
public static int[] last;
public static int[] paint;
public static boolean[] need;
public static ArrayList<Integer>[] colorMap;
public static void main(String[] args) throws Exception {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer(stdin.readLine());
int nC = Integer.parseInt(tok.nextToken());
for (int loop=0; loop<nC; loop++) {
tok = new StringTokenizer(stdin.readLine());
nPost = Integer.parseInt(tok.nextToken());
nPaint = Integer.parseInt(tok.nextToken());
init = new int[nPost];
tok = new StringTokenizer(stdin.readLine());
for (int i=0; i<nPost; i++)
init[i] = Integer.parseInt(tok.nextToken())-1;
need = new boolean[nPost];
last = new int[nPost];
tok = new StringTokenizer(stdin.readLine());
for (int i=0; i<nPost; i++) {
last[i] = Integer.parseInt(tok.nextToken())-1;
need[last[i]] = true;
}
colorMap = new ArrayList[nPost];
for (int i=0; i<nPost; i++) colorMap[i] = new ArrayList<Integer>();
paint = new int[nPaint];
tok = new StringTokenizer(stdin.readLine());
for (int i=0; i<nPaint; i++) {
paint[i] = Integer.parseInt(tok.nextToken())-1;
if (i<nPaint-1) colorMap[paint[i]].add(i);
}
go();
}
}
public static void go() {
if (!need[paint[nPaint-1]]) {
System.out.println("NO");
return;
}
int[] res = new int[nPaint];
int lastItem = -1;
for (int i=0; i<nPost; i++) {
if (last[i] == paint[nPaint-1] && last[i] != init[i]) {
res[nPaint-1] = i+1;
lastItem = i;
break;
}
}
if (lastItem == -1) {
for (int i=0; i<nPost; i++) {
if (last[i] == paint[nPaint-1]) {
res[nPaint-1] = i+1;
lastItem = i;
break;
}
}
}
// look at everything that needs to change...
int[] idx = new int[nPost];
boolean canDo = true;
for (int i=0; i<nPost; i++) {
if (i == lastItem) continue;
if (init[i] != last[i]) {
if (idx[last[i]] >= colorMap[last[i]].size()) {
canDo = false;
break;
}
int painter = colorMap[last[i]].get(idx[last[i]]);
res[painter] = i+1;
idx[last[i]]++;
}
}
for (int i=0; i<nPaint; i++)
if (res[i] == 0)
res[i] = lastItem+1;
if (!canDo)
System.out.println("NO");
else {
System.out.println("YES");
StringBuffer sb = new StringBuffer();
sb.append(""+res[0]);
for (int i=1; i<nPaint; i++)
sb.append(" "+res[i]);
System.out.println(sb);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 814497a039bd3e6b054b50289baa93d2 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;import java.util.*;import java.math.*;
public class Main
{
static long mod=1000000007l;
static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static StringBuilder sb;
static public void main(String[] args)throws Exception
{
st=new StringTokenizer(br.readLine());
int t=i();
sb=new StringBuilder(1000000);
o:
while(t-->0)
{
int n=i();
int m=i();
int a[]=ari(n);
int b[]=ari(n);
int c[]=ari(m);
int ar[]=new int[m];
LinkedList<Integer> l[]=new LinkedList[n+1];
for(int x=0;x<=n;x++)l[x]=new LinkedList<Integer>();
int s[]=new int[n+1];
for(int x=0;x<=n;x++)s[x]=-1;
for(int x=0;x<n;x++)
{
if(a[x]!=b[x])l[b[x]].add(x);
else s[b[x]]=x;
}
int p=-1;
for(int x=m-1;x>=0;x--)
{
int v=c[x];
if(l[v].size()==0)
{
if(s[v]!=-1)
{
p=s[v];
ar[x]=s[v]+1;
}
else if(p==-1)
{
sl("NO");
continue o;
}
else ar[x]=p+1;
}
else
{
int va=l[v].remove();
ar[x]=va+1;
p=va;
}
}
for(LinkedList<Integer> ll:l)
{
if(ll.size()!=0)
{
sl("NO");
continue o;
}
}
sl("YES");
s(ar);
}
p(sb);
}
static int[] so(int ar[])
{
Integer r[]=new Integer[ar.length];
for(int x=0;x<ar.length;x++)r[x]=ar[x];
Arrays.sort(r);
for(int x=0;x<ar.length;x++)ar[x]=r[x];
return ar;
}
static long[] so(long ar[])
{
Long r[]=new Long[ar.length];
for(int x=0;x<ar.length;x++)r[x]=ar[x];
Arrays.sort(r);
for(int x=0;x<ar.length;x++)ar[x]=r[x];
return ar;
}
static char[] so(char ar[])
{
Character r[]=new Character[ar.length];
for(int x=0;x<ar.length;x++)r[x]=ar[x];
Arrays.sort(r);
for(int x=0;x<ar.length;x++)ar[x]=r[x];
return ar;
}
static void s(String s){sb.append(s);}
static void s(int s){sb.append(s);}
static void s(long s){sb.append(s);}
static void s(char s){sb.append(s);}
static void s(double s){sb.append(s);}
static void ss(){sb.append(' ');}
static void sl(String s){sb.append(s);sb.append("\n");}
static void sl(int s){sb.append(s);sb.append("\n");}
static void sl(long s){sb.append(s);sb.append("\n");}
static void sl(char s){sb.append(s);sb.append("\n");}
static void sl(double s){sb.append(s);sb.append("\n");}
static void sl(){sb.append("\n");}
static int max(int ...a){int m=a[0];for(int e:a)m=(m>=e)?m:e;return m;}
static int min(int ...a){int m=a[0];for(int e:a)m=(m<=e)?m:e;return m;}
static int abs(int a){return Math.abs(a);}
static long max(long ...a){long m=a[0];for(long e:a)m=(m>=e)?m:e;return m;}
static long min(long ...a){long m=a[0];for(long e:a)m=(m<=e)?m:e;return m;}
static long abs(long a){return Math.abs(a);}
static int sq(int a){return (int)Math.sqrt(a);}
static long sq(long a){return (long)Math.sqrt(a);}
static long gcd(long a,long b){return b==0l?a:gcd(b,a%b);}
static boolean pa(String s,int i,int j)
{
while(i<j)if(s.charAt(i++)!=s.charAt(j--))return false;
return true;
}
static int ncr(int n,int c,long m)
{
long a=1l;
for(int x=n-c+1;x<=n;x++)a=((a*x)%m);
long b=1l;
for(int x=2;x<=c;x++)b=((b*x)%m);
return (int)((a*(mul((int)b,m-2,m)%m))%m);
}
static boolean[] si(int n)
{
boolean bo[]=new boolean[n+1];
bo[0]=true;bo[1]=true;
for(int x=4;x<=n;x+=2)bo[x]=true;
for(int x=3;x*x<=n;x+=2)
{
if(!bo[x])
{
int vv=(x<<1);
for(int y=x*x;y<=n;y+=vv)bo[y]=true;
}
}
return bo;
}
static int[] fac(int n)
{
int bo[]=new int[n+1];
for(int x=1;x<=n;x++)for(int y=x;y<=n;y+=x)bo[y]++;
return bo;
}
static long mul(long a,long b,long m)
{
long r=1l;
a%=m;
while(b>0)
{
if((b&1)==1)r=(r*a)%m;
b>>=1;
a=(a*a)%m;
}
return r;
}
static int i()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
static long l()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
static String s()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static double d()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());
}
static void p(Object p){System.out.print(p);}
static void p(String p){System.out.print(p);}
static void p(int p){System.out.print(p);}
static void p(double p){System.out.print(p);}
static void p(long p){System.out.print(p);}
static void p(char p){System.out.print(p);}
static void p(boolean p){System.out.print(p);}
static void pl(Object p){System.out.println(p);}
static void pl(String p){System.out.println(p);}
static void pl(int p){System.out.println(p);}
static void pl(char p){System.out.println(p);}
static void pl(double p){System.out.println(p);}
static void pl(long p){System.out.println(p);}
static void pl(boolean p){System.out.println(p);}
static void pl(){System.out.println();}
static void s(int a[])
{
for(int e:a)
{
sb.append(e);
sb.append(' ');
}
sb.append("\n");
}
static void s(long a[])
{
for(long e:a)
{
sb.append(e);
sb.append(' ');
}
sb.append("\n");
}
static void s(int ar[][])
{
for(int a[]:ar)
{
for(int e:a)
{
sb.append(e);
sb.append(' ');
}
sb.append("\n");
}
}
static void s(char a[])
{
for(char e:a)
{
sb.append(e);
sb.append(' ');
}
sb.append("\n");
}
static void s(char ar[][])
{
for(char a[]:ar)
{
for(char e:a)
{
sb.append(e);
sb.append(' ');
}
sb.append("\n");
}
}
static int[] ari(int n)throws IOException
{
int ar[]=new int[n];
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=Integer.parseInt(st.nextToken());
return ar;
}
static int[][] ari(int n,int m)throws IOException
{
int ar[][]=new int[n][m];
for(int x=0;x<n;x++)
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());
}
return ar;
}
static long[] arl(int n)throws IOException
{
long ar[]=new long[n];
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken());
return ar;
}
static long[][] arl(int n,int m)throws IOException
{
long ar[][]=new long[n][m];
for(int x=0;x<n;x++)
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());
}
return ar;
}
static String[] ars(int n)throws IOException
{
String ar[]=new String[n];
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=st.nextToken();
return ar;
}
static double[] ard(int n)throws IOException
{
double ar[]=new double[n];
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());
return ar;
}
static double[][] ard(int n,int m)throws IOException
{
double ar[][]=new double[n][m];
for(int x=0;x<n;x++)
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Double.parseDouble(st.nextToken());
}
return ar;
}
static char[] arc(int n)throws IOException
{
char ar[]=new char[n];
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);
return ar;
}
static char[][] arc(int n,int m)throws IOException
{
char ar[][]=new char[n][m];
for(int x=0;x<n;x++)
{
String s=br.readLine();
for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);
}
return ar;
}
static void p(int ar[])
{
StringBuilder sb=new StringBuilder(2*ar.length);
for(int a:ar)
{
sb.append(a);
sb.append(' ');
}
System.out.println(sb);
}
static void p(int ar[][])
{
StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);
for(int a[]:ar)
{
for(int aa:a)
{
sb.append(aa);
sb.append(' ');
}
sb.append("\n");
}
p(sb);
}
static void p(long ar[])
{
StringBuilder sb=new StringBuilder(2*ar.length);
for(long a:ar)
{
sb.append(a);
sb.append(' ');
}
System.out.println(sb);
}
static void p(long ar[][])
{
StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);
for(long a[]:ar)
{
for(long aa:a)
{
sb.append(aa);
sb.append(' ');
}
sb.append("\n");
}
p(sb);
}
static void p(String ar[])
{
int c=0;
for(String s:ar)c+=s.length()+1;
StringBuilder sb=new StringBuilder(c);
for(String a:ar)
{
sb.append(a);
sb.append(' ');
}
System.out.println(sb);
}
static void p(double ar[])
{
StringBuilder sb=new StringBuilder(2*ar.length);
for(double a:ar)
{
sb.append(a);
sb.append(' ');
}
System.out.println(sb);
}
static void p(double ar[][])
{
StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);
for(double a[]:ar)
{
for(double aa:a)
{
sb.append(aa);
sb.append(' ');
}
sb.append("\n");
}
p(sb);
}
static void p(char ar[])
{
StringBuilder sb=new StringBuilder(2*ar.length);
for(char aa:ar)
{
sb.append(aa);
sb.append(' ');
}
System.out.println(sb);
}
static void p(char ar[][])
{
StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);
for(char a[]:ar)
{
for(char aa:a)
{
sb.append(aa);
sb.append(' ');
}
sb.append("\n");
}
p(sb);
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 0da5d9ab6e9af7e329f9a0ed55b9345c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n, m, k;
static TreeSet<Integer>[] ad, adInv;
static int[][][] memo;
static boolean vis[];
static long[] f, inv, ncr[];
static HashMap<Integer, Integer> hm;
static char[][] g;
static int[] pre, suf, Smax[], Smin[];
static int idmax, idmin;
static ArrayList<Integer> av;
static HashMap<Integer, Integer> mm;
static boolean[] corr;
static int[] lazy[], lazyCount;
static int[] a, in;
static char[] num;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
boolean[] wanted = new boolean[n + 1];
int[] a = sc.nextArrInt(n);
int[] b = new int[n];
ad = new TreeSet[n + 1];
for (int i = 0; i < n + 1; i++)
ad[i] = new TreeSet<>();
for (int i = 0; i < n; i++)
wanted[b[i] = sc.nextInt()] = true;
int all = 0;
TreeSet<Integer> idc = new TreeSet<>();
for (int i = 0; i < n; i++)
if (b[i] != a[i]) {
ad[b[i]].add(i);
idc.add(i);
all++;
}
int[] painters = sc.nextArrInt(m);
sb = new StringBuilder();
int i = 0;
for (; i < m && all != 0; i++) {
if (ad[painters[i]].size() > 0) {
all--;
int id = ad[painters[i]].pollFirst();
idc.remove(id);
sb.append((1 + id) + " ");
} else {
sb.append((1 + idc.first()) + " ");
}
}
if (wanted[painters[m - 1]]) {
int idk = 0;
for (int j = 0; j < n; j++)
if (b[j] == painters[m - 1]) {
idk = j;
break;
}
for (; i < m; i++)
sb.append((idk + 1) + " ");
} else {
out.println("NO");
continue;
}
if (all == 0) {
out.println("YES");
out.println(sb);
} else
out.println("NO");
}
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 0966d9144f9bf884e14dd883736b023e | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | //created by Whiplash99
import java.io.*;
import java.util.*;
public class C
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
int T=Integer.parseInt(br.readLine().trim());
StringBuilder sb=new StringBuilder();
while (T-->0)
{
String[] s=br.readLine().trim().split(" ");
N=Integer.parseInt(s[0]);
int M=Integer.parseInt(s[1]);
int[] a=new int[N];
int[] b=new int[N];
int[] c=new int[M];
s=br.readLine().trim().split(" ");
for(i=0;i<N;i++) a[i]=Integer.parseInt(s[i])-1;
s=br.readLine().trim().split(" ");
for(i=0;i<N;i++) b[i]=Integer.parseInt(s[i])-1;
s=br.readLine().trim().split(" ");
for(i=0;i<M;i++) c[i]=Integer.parseInt(s[i])-1;
HashMap<Integer,Integer> map=new HashMap<>();
ArrayDeque<Integer>[] pos=new ArrayDeque[N];
for(i=0;i<N;i++) pos[i]=new ArrayDeque<>();
boolean flag=true;
for(i=0;i<N;i++)
{
if(a[i]!=b[i])
pos[b[i]].add(i);
}
for(i=0;i<N;i++) map.put(b[i],i);
int[] ans=new int[M];
ArrayDeque<Integer> repaint=new ArrayDeque<>();
for(i=M-1;i>=0;i--)
{
if(!map.containsKey(c[i]))
{
if(repaint.isEmpty())
{
flag=false;
break;
}
else ans[i]=repaint.peekFirst()+1;
continue;
}
int p;
if(!pos[c[i]].isEmpty()) p = pos[c[i]].pollFirst();
else p = map.get(c[i]);
ans[i]=p+1;
repaint.add(p);
}
for(i=0;i<N;i++) if(!pos[i].isEmpty()) flag=false;
if(!flag) sb.append("NO\n");
else
{
sb.append("YES\n");
for(int x:ans) sb.append(x).append(" ");
sb.append("\n");
}
}
System.out.println(sb);
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | daf81b1e72949e83e1f3fc727bf7d34b | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main {
static PrintWriter out;
static Reader in;
public static void main(String[] args) throws IOException {
input_output();
//out = new PrintWriter(System.out);
//in = new Reader(new FileInputStream("card.in"));
Main solver = new Main();
solver.solve();
out.close();
out.flush();
}
static int INF = (int)1e9+2;
static int maxn = (int)1e6+5;
static int mod = (int)1e9+7;
static int n, m, q, t, k;
static double pi = 3.141592653589;
void solve() throws IOException{
t = in.nextInt();
while (t --> 0) {
n = in.nextInt();
m = in.nextInt();
int[] a = new int[n],
b = new int[n],
c = new int[m];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
for (int i = 0; i < n; i++) b[i] = in.nextInt();
int[] need = new int[n+1];
boolean[] vis = new boolean[n+1];
int[] idx = new int[n+1];
List<Integer> place[] = new ArrayList[n+1];
for (int i = 1; i <= n; i++) place[i] = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
vis[b[i]] = true;
if (b[i] != a[i]) {
need[b[i]]++;
place[b[i]].add(i+1);
}
}
int[] have = new int[n+1];
int last = 0;
for (int i = 0; i < m; i++) {
last = in.nextInt();
c[i] = last;
have[last]++;
}
int def = 0;
if (place[last].size() != 0) def = place[last].get(0);
else {
for (int i = 0; i < n; i++) {
if (b[i] == last) {def = i+1;break;}
}
}
boolean start = vis[last], good = true;
for (int i = 1; i <= n; i++) {
if (need[i] > have[i]) good = false;
}
if (good && start) {
out.println("YES");
int[] ans = new int[m];
for (int i = m-1; i >= 0; i--) {
if (idx[c[i]] == place[c[i]].size()) ans[i] = def;
else ans[i] = place[c[i]].get(idx[c[i]]++);
}
for (int i = 0; i < m; i++) out.print(ans[i]+" ");
out.println();
} else out.println("NO");
}
}
//<>
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static void input_output() throws IOException {
File f = new File("in.txt");
if(f.exists() && !f.isDirectory()) {
in = new Reader(new FileInputStream("in.txt"));
} else in = new Reader();
f = new File("out.txt");
if(f.exists() && !f.isDirectory()) {
out = new PrintWriter(new File("out.txt"));
} else out = new PrintWriter(System.out);
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5fc3fa24f7d578644c87dd7b3dc9d9b0 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
public class C1481 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int M = in.nextInt();
int[] A = new int[N];
for (int n=0; n<N; n++) {
A[n] = in.nextInt();
}
int[] B = new int[N];
int[] posOfColor = new int[N+1];
for (int n=0; n<N; n++) {
int b = in.nextInt();
B[n] = b;
posOfColor[b] = n+1;
}
Set<Integer> hasWrongColor = new HashSet<>();
Queue<Integer>[] needsColor = new Queue[N+1];
for (int n=0; n<N; n++) {
if (A[n] != B[n]) {
Queue<Integer> queue = needsColor[B[n]];
if (queue == null) {
needsColor[B[n]] = queue = new LinkedList<>();
}
queue.add(n+1);
hasWrongColor.add(n+1);
}
}
int[] C = new int[M];
int posOfLastGoodColor = 1;
for (int m=0; m<M; m++) {
int c = in.nextInt();
C[m] = c;
if (posOfColor[c] != 0) {
posOfLastGoodColor = posOfColor[c];
}
}
StringBuilder out = new StringBuilder();
for (int c : C) {
Queue<Integer> queue = needsColor[c];
int idx;
if (queue == null || queue.isEmpty()) {
idx = posOfColor[c];
if (idx == 0) {
if (hasWrongColor.isEmpty()) {
idx = posOfLastGoodColor;
} else {
idx = hasWrongColor.iterator().next();
}
}
} else {
idx = queue.poll();
hasWrongColor.remove(idx);
}
A[idx-1] = c;
out.append(idx).append(' ');
}
boolean ok = true;
for (int n=0; n<N; n++) {
if (A[n] != B[n]) {
ok = false;
}
}
System.out.println(ok ? "YES" : "NO");
if (ok) {
System.out.println(out);
}
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 6834abe7e9500f1b9c132c70edc2da1a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Main1{
static Scanner in=new Scanner();
static long systemTime;
static long mod = 1000000007;
//static ArrayList<ArrayList<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
public static void main(String[] args) throws Exception{
int z=in.readInt();
ncr(1000,1000);
for(int test=1;test<=z;test++) {
//setTime();
solve();
//printTime();
//printMemory();
}
}
static void solve() {
int n=in.readInt();
int m=in.readInt();
int a[]=nia(n);
int b[]=nia(n);
Map<Integer,TreeSet<Integer>> mp=new HashMap<>();
Map<Integer,Set<Integer>> mp2=new HashMap<>();
for(int i=0;i<n;i++) {
if(b[i]!=a[i]) {
mp.putIfAbsent(b[i], new TreeSet<>());
mp.get(b[i]).add(i);
}
}
int c[]=nia(m);
int last=-1;
for(int i=n-1;i>=0;i--) {
if(b[i]==c[m-1]&&b[i]!=a[i]) {
last=i;
break;
}
}
if(last==-1) {
for(int i=n-1;i>=0;i--) {
if(b[i]==c[m-1]) {
last=i;
break;
}
}
}
if(last==-1) {
print("NO");
return;
}
int ans[]=new int[m];
for(int i=0;i<m-1;i++) {
if(mp.containsKey(c[i])) {
TreeSet<Integer> st=mp.get(c[i]);
int tmp=st.pollFirst();
ans[i]=tmp+1;
a[tmp]=c[i];
if(st.isEmpty()) {
mp.remove(Integer.valueOf(c[i]));
}
}
else {
ans[i]=last+1;
}
}
a[last]=c[m-1];
ans[m-1]=last+1;
for(int i=0;i<n;i++) {
if(a[i]!=b[i]) {
print("NO");
return;
}
}
print("YES");
print(ans);
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n%mod;
else {
long r=pow(n,m/2);
if(m%2==0)
return (r*r)%mod;
else
return (r*r*n)%mod;
}
}
static long maxsumsub(ArrayList<Long> al) {
long max=0;
long sum=0;
for(int i=0;i<al.size();i++) {
sum+=al.get(i);
if(sum<0) {
sum=0;
}
max=Math.max(max,sum);
}
long a[]=al.stream().mapToLong(i -> i).toArray();
return max;
}
static long abs(long a) {
return Math.abs(a);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] =( C[i - 1][j - 1] + C[i - 1][j])%mod;
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a)
l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++)
a[i]=l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a)
l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++)
a[i]=l.get(i);
}
static int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long[] nla1(int n){
long[] arr= new long[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 79f7041880f29f04bd71aafdad757a42 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class M{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
l: while(t-->0) {
StringBuilder sb=new StringBuilder();
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m];
LinkedList<Integer> d[]=new LinkedList[n+1];
d[n]=new LinkedList<Integer>();
for(int i=0;i<n;i++) {
d[i]=new LinkedList<Integer>();
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i);
}
for(int i=0;i<n;i++) {
if(b[i]!=a[i]) d[b[i]].add(i);
}
for(int i=0;i<m;i++) {
c[i]=sc.nextInt();
}
if(d[c[m-1]].isEmpty()) {
System.out.println("NO");
continue;
}
for(int i=m-1;i>-1;i--)
if(d[c[i]].isEmpty()) ans[i]=ans[i+1];
else ans[i]=d[c[i]].pollLast();
for(int i=0;i<m;i++) {
a[ans[i]]=c[i];
sb.append(ans[i]+1+" ");
}
for(int i=0;i<n;i++) if(a[i]!=b[i]) {
System.out.println("NO");
continue l;
}
System.out.println("YES\n"+sb);
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5c7cdcf198aa5108323555ac3cf11716 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class FencePainting {
static class Tuple {
int f;
int s; //
int d;
Tuple() {
}
Tuple(int f, int s, int d) {
this.f = f;
this.s = s;
this.d = d;
}
}
static class Fast {
BufferedReader br;
StringTokenizer st;
public Fast() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readArray1(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/* static long noOfDivisor(long a)
{
long count=0;
long t=a;
for(long i=1;i<=(int)Math.sqrt(a);i++)
{
if(a%i==0)
count+=2;
}
if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a)))
{
count--;
}
return count;
}*/
static boolean isPrime(long a) {
for (long i = 2; i <= (long) Math.sqrt(a); i++) {
if (a % i == 0)
return false;
}
return true;
}
static void primeFact(int n) {
int temp = n;
HashMap<Integer, Integer> h = new HashMap<>();
for (int i = 2; i * i <= n; i++) {
if (temp % i == 0) {
int c = 0;
while (temp % i == 0) {
c++;
temp /= i;
}
h.put(i, c);
}
}
if (temp != 1)
h.put(temp, 1);
}
static void reverseArray(int a[]) {
int n = a.length;
for (int i = 0; i < n / 2; i++) {
a[i] = a[i] ^ a[n - i - 1];
a[n - i - 1] = a[i] ^ a[n - i - 1];
a[i] = a[i] ^ a[n - i - 1];
}
}
/* static void sort(Pair[] a) {
ArrayList<Pair> l = new ArrayList<>();
for (Pair i : a) l.add(i);
Collections.sort(l,new sortbyfirst());
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}*/
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static int max(int a, int b) {
return a > b ? a : b;
}
static long min(long a, long b) {
return a < b ? a : b;
}
static long abs(long a) {
return a > 0 ? a : -1 * a;
}
static int ans = 0;
static ArrayList<Integer> adj[];
static void build(int n) //do n+1 in actual parameter if index starts from 1 else n
{
adj = new ArrayList[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<>();
}
static void addEdge(int s, int d) //v-1 || n-1 edges no need to change the actual parameters (use n only_)
{
adj[s].add(d);
adj[d].add(s);
}
public static void main(String args[]) throws IOException {
Fast sc = new Fast();
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while (t1-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = sc.readArray(n);
int b[] = sc.readArray(n);
int c[] = sc.readArray(m);
HashMap<Integer, TreeSet<Integer>> req = new HashMap<>();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++)
{
if(req.get(b[i])==null)
req.put(b[i],new TreeSet<>());
if(map.get(b[i])==null)
map.put(b[i],i);
if(a[i]!=b[i])
{
req.get(b[i]).add(i);
}
}
int f=1;
int ans[]=new int[m];
Arrays.fill(ans,-1);
for(int i=0;i<m;i++)
{
if(req.get(c[i])==null)
{
continue;
}
if(req.get(c[i]).size()>0)
{
int idx=req.get(c[i]).pollFirst();
ans[i]=idx;
}
else
{
ans[i]=map.get(c[i]);
}
}
for(TreeSet ts1:req.values())
if(ts1.size()>0)
{
f=0;break;
}
if(ans[m-1]==-1)
f=0;
if(f==0)
out.println("NO");
else {
out.println("YES");
for (int i = m - 2; i >= 0; i--) {
if (ans[i] == -1)
ans[i] = ans[i + 1];
}
for (int ne : ans)
out.print((ne+1) + " ");
out.println();
}
}
out.close();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 86c90c76fe6640940e9126d3312afcc4 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
//import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
import java.lang.*;
import java.io.*;
public class Main
{
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static long gcd(long a,long b) {if(a==0)return b; return gcd(b%a,a);}
public static int gcd(int a,int b) {if(a==0)return b; return gcd(b%a,a);};
static int power(int x, int y){
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
static long power(long x , long y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
public static long rep(long num,long x)
{
long count=(long)1;
while(num%x==(long)0)
{
count++;
num/=x;
}
return count;
}
public static int find_par(int x,int parent[])
{
if(parent[x]==x)
return x;
return parent[x]=find_par(parent[x],parent);
}
public static void union(int u,int v,int parent[],int rank[])
{
int x=find_par(u,parent);
int y=find_par(v,parent);
if(rank[x]>rank[y])
parent[y]=x;
else
if(rank[x]<rank[y])
parent[x]=y;
else
{
parent[y]=x;
rank[x]++;
}
}
public int findMinArrowShots(int[][] points) {
Arrays.sort(points,(n1,n2)->n1[1]-n2[1]==0?n1[0]-n2[0]:n1[1]-n2[1]);
int c=1;
int arr_lmt=points[0][1];
for(int i=1;i<points.length;i++)
{
if(arr_lmt>=points[i][0])
{}
else
{
c++;
arr_lmt=points[i][1];
}
}
return c;
}
static List<List<int[]>>adj;
public static void helper(List<List<int[]>>adj,int i,boolean visited[],boolean flag)
{
visited[i]=true;
for(int it[]:adj.get(i))
{
if(!visited[it[0]])
{
if(flag)
it[1]=2;
else
it[1]=3;
helper(adj,it[0],visited,!flag);
}
}
}
public static void main (String[] args) throws java.lang.Exception
{
long mod=1000000007;
FastReader in=new FastReader();
if(in.hasNext()){
if(in.hasNext()){
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int test=in.nextInt();
outer:while(test-->0)
{
int n=in.nextInt();
int m=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++)
a[i]=in.nextInt();
for(int i=0;i<n;i++)
b[i]=in.nextInt();
int p[]=new int[m];
for(int i=0;i<m;i++)
{
p[i]=in.nextInt();
}
Map<Integer,Integer>map=new HashMap<>();
int ans[]=new int[m];
Map<Integer,Stack<Integer>>maplist=new HashMap<>();
for(int i=0;i<m;i++)
{
map.put(p[i], map.getOrDefault(p[i], 0)+1);
if(maplist.containsKey(p[i]))
{
Stack<Integer>stk=maplist.get(p[i]);
stk.push(i);
maplist.put(p[i], stk);
}
else
{
Stack<Integer>stk=new Stack<>();
stk.push(i);
maplist.put(p[i], stk);
}
}
Arrays.fill(ans, -1);
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
if(map.containsKey(b[i])) {
Stack<Integer>stk=maplist.get(b[i]);
int indx=stk.pop();
//System.out.println(indx);
ans[indx]=i;
maplist.put(b[i], stk);
a[i]=b[i];
map.put(b[i], map.get(b[i])-1);
if(map.get(b[i])==0)
map.remove(b[i]);
}
else
{
System.out.println("NO");
continue outer;
}
}
}
int index=0;
if(ans[m-1]!=-1)
index=ans[m-1];
else
{
boolean flag=true;
for(int i=0;i<n;i++)
{
if(a[i]==p[m-1])
{
index=i;
flag=false;
break;
}
}
if(flag)
{
System.out.println("NO");
continue outer;
}
}
for(int i=0;i<m;i++)
{
if(ans[i]==-1)
ans[i]=index;
}
System.out.println("YES");
for(int i=0;i<m;i++)
{
System.out.print((ans[i]+1)+" ");
}
System.out.println();
}
}
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 746e73e20d22b8005559da0facd79536 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastReader f = new FastReader();
StringBuffer sb=new StringBuffer();
int test=f.nextInt();
out:
while(test-->0)
{
int n=f.nextInt();
int m=f.nextInt();
int a[]=new int[n],b[]=new int[n];
int p[]=new int[m];
for(int i=0;i<n;i++)
a[i]=f.nextInt();
for(int i=0;i<n;i++)
b[i]=f.nextInt();
for(int i=0;i<m;i++)
p[i]=f.nextInt();
Map<Integer,TreeSet<Integer>> need=new HashMap<>();
Map<Integer,TreeSet<Integer>> have=new HashMap<>();
int[] ans=new int[m];
for(int i=0;i<n;i++)
{
if(a[i]==b[i])
{
TreeSet<Integer> list=have.getOrDefault(a[i],new TreeSet<>());
list.add(i);
have.put(a[i],list);
}
else
{
TreeSet<Integer> list=need.getOrDefault(b[i],new TreeSet<>());
list.add(i);
need.put(b[i], list);
TreeSet<Integer> l1=have.getOrDefault(a[i],new TreeSet<>());
l1.add(i);
have.put(a[i],l1);
}
}
// System.out.println("need = "+need);
// System.out.println("have = "+have);
for(int i=0;i<m;i++)
{
int val=p[i];
if(need.containsKey(val))
{
TreeSet<Integer> set=need.get(val);
int idx=set.pollFirst();
if(set.isEmpty())
need.remove(val);
else
need.put(val,set);
ans[i]=idx+1;
int clr=a[idx];
TreeSet<Integer> set1=have.get(clr);
set1.remove(idx);
if(set1.isEmpty())
have.remove(clr);
else
have.put(clr,set1);
TreeSet<Integer> set2=have.getOrDefault(val,new TreeSet<>());
set2.add(idx);
have.put(val, set2);
}
else if(have.containsKey(val))
{
TreeSet<Integer> set=have.get(val);
int idx=set.first();
ans[i]=idx+1;
}
else
ans[i]=-1;
}
int i=m-1;
while(i>=0 && ans[i]==-1)
i--;
if(i>=0)
{
int curr=ans[i];
for(int j=0;j<i;j++)
if(ans[j]==-1)
ans[j]=curr;
}
if(!need.isEmpty())
{
sb.append("NO\n");
continue;
}
for(i=0;i<m;i++)
if(ans[i]==-1)
{
sb.append("NO\n");
continue out;
}
sb.append("YES\n");
for(int k:ans)
sb.append(k+" ");
sb.append("\n");
}
System.out.println(sb);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 8 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | f659a42d8ac0a7fc34dbc1b206edd3f0 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | //1
//27 30
//12 8 15 4 20 22 4 13 20 19 6 22 21 1 10 10 2 5 1 15 19 11 9 3 4 3 19
//12 8 15 4 20 22 4 13 20 19 6 22 21 17 10 4 2 5 12 15 19 11 6 12 4 3 13
//11 27 10 7 21 12 14 5 14 4 1 22 4 19 17 11 13 12 23 12 8 26 6 27 26 9 17 6 2 4
//1
//26 30
//18 24 10 3 23 21 11 1 23 26 24 17 7 25 21 19 8 23 18 21 8 24 18 6 19 10
//18 24 10 3 5 20 9 1 23 19 24 17 7 25 21 19 18 23 18 21 8 5 18 6 19 16
//20 25 24 6 11 2 17 9 25 12 17 12 13 15 17 19 20 1 19 18 16 8 13 9 24 3 22 24 5 5
//1
//29 24
//3 21 23 1 11 7 8 9 11 6 21 16 22 3 22 25 29 14 28 25 15 14 3 7 13 15 21 27 5
//3 21 12 1 11 7 8 9 11 16 21 16 22 3 24 8 15 1 28 25 15 14 27 1 13 15 21 27 5
//2 22 19 16 3 10 22 16 27 24 25 16 24 24 15 12 2 8 5 25 1 4 28 1
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution{
public static void main(String[] args) {
TaskA solver = new TaskA();
int t = in.nextInt();
for (int i = 1; i <= t ; i++) {
solver.solve(i, in, out);
}
// solver.solve(1, in, out);
out.flush();
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n= in.nextInt();
int m= in.nextInt();
int[]a=input(n);
int[]b=input(n);
int[]c=input(m);
ArrayList<Integer>[]different=new ArrayList[n+1];
ArrayList<Integer>[]color=new ArrayList[n+1];
for(int i=0;i<=n;i++) {
different[i]=new ArrayList<>();
color[i]=new ArrayList<>();
}
HashSet<Integer>bColors=new HashSet<>();
for(int i=0;i<n;i++) {
bColors.add(b[i]);
color[b[i]].add(i);
if(a[i]!=b[i]) {
different[b[i]].add(i);
}
}
int lstcol=c[m-1];
if(!bColors.contains(lstcol)) {
println("No");return;
}
int last=color[lstcol].get(color[lstcol].size()-1);
if(different[lstcol].size()>0) {
last=different[lstcol].get(different[lstcol].size()-1);
}
int[]ans=new int[m];
for(int i=0;i<m;i++) {
int col=c[i];
if(col==c[m-1]) {
if(different[col].size()>0) {
int size=different[col].size();
int fenceNo=different[col].get(0);
a[fenceNo]=col;
ans[i]=fenceNo+1;
different[col].remove(0);
}
else {
a[last]=col;
ans[i]=last+1;
}
}
else if(different[col].size()>0) {
int size=different[col].size();
int fenceNo=different[col].get(size-1);
a[fenceNo]=col;
ans[i]=fenceNo+1;
different[col].remove(size-1);
}
else if(color[col].size()>0) {
if(col==c[m-1]) {
ans[i]=last+1;a[last]=col;continue;
}
int fenceNo=color[col].get(color[col].size()-1);
ans[i]=fenceNo+1;
a[fenceNo]=col;
}
else {
a[last]=col;
ans[i]=last+1;
}
}
for(int i=0;i<n;i++) {
if(a[i]!=b[i]) {
println("NO");return;
}
}
println("YES");
println(ans);
}
}
static int[] query(int l,int r) {
System.out.println("? "+l+" "+r);
System.out.print ("\n");System.out.flush();
int[]arr=new int[r-l+1];
for(int i=0;i<r-l+1;i++) {
arr[i]=in.nextInt();
}
return arr;
}
static int[]presum(int[]arr){
int n= arr.length;
int[]pre=new int[n];
for(int i=0;i<n;i++) {
if(i>0) {
pre[i]=pre[i-1];
}
pre[i]+=arr[i];
}
return arr;
}
static int max(int[]arr) {
int max=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++) {
max=Math.max(max, arr[i]);
}
return max;
}
static int min(int[]arr) {
int min=Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++) {
min=Math.min(min, arr[i]);
}
return min;
}
static int ceil(int a,int b) {
int ans=a/b;if(a%b!=0) {
ans++;
}
return ans;
}
static long sum(int[]arr) {
long s=0;
for(int x:arr) {
s+=x;
}
return s;
}
static long sum(long[]arr) {
long s=0;
for(long x:arr) {
s+=x;
}
return s;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void println(int[][]arr) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[0].length;j++) {
print(arr[i][j]+" ");
}
print("\n");
}
}
static void println(long[][]arr) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[0].length;j++) {
print(arr[i][j]+" ");
}
print("\n");
}
}
static void println(int[]arr){
for(int i=0;i<arr.length;i++) {
print(arr[i]+" ");
}
print("\n");
}
static void println(long[]arr){
for(int i=0;i<arr.length;i++) {
print(arr[i]+" ");
}
print("\n");
}
static int[]input(int n){
int[]arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
static int[]input(){
int n= in.nextInt();
int[]arr=new int[(int)n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
////////////////////////////////////////////////////////
static class Pair {
int first;
int second;
Pair(int x, int y)
{
this.first = x;
this.second = y;
}
}
static void sortS(Pair arr[])
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return (p1.second - p2.second);
}
});
}
static void sortF(Pair arr[])
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return (p1.first - p2.first);
}
});
}
/////////////////////////////////////////////////////////////
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static void println(long c) {
out.println(c);
}
static void print(long c) {
out.print(c);
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
static void println(boolean b) {
out.println(b);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 9a896d5e8f159ae4cb15aa81e93256d8 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
for(int i =0;i<n;i++){
a[i] = i();
}
for(int i =0;i<n;i++){
b[i] = i();
}
for(int i=0;i<m;i++){
c[i] = i();
}
HashMap<Integer,LinkedList<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
if(a[i]!=b[i]){
if(!map.containsKey(b[i])) map.put(b[i],new LinkedList());
map.get(b[i]).add(i);
}
}
int last = -1;
if(map.containsKey(c[m-1])){
last = map.get(c[m-1]).removeLast();
if(map.get(c[m-1]).size()==0){
map.remove(c[m-1]);
}
}else{
for(int i =0;i<n;i++){
if(b[i]==c[m-1]){
last = i;
}
}
}
if(last==-1){
sb.append("NO\n");
return;
}
int ans[] = new int[m];
ans[m-1] = last;
for(int i =0;i<m-1;i++){
if(map.containsKey(c[i])){
// required in repaint
int idx = map.get(c[i]).removeLast();
ans[i] = idx;
if(map.get(c[i]).size()==0){
map.remove(c[i]);
}
}else{
ans[i] = last;
}
}
if(map.size()!=0){
// >=1 fence left which should have been repainted
// but have not been repainted
sb.append("NO\n");
}else{
sb.append("YES\n");
for(int i =0;i<m;i++){
sb.append(ans[i]+1).append(" ");
}
sb.append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | aade27b7d5ce0aad02d5315309862fee | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
for(int i =0;i<n;i++){
a[i] = i();
}
for(int i =0;i<n;i++){
b[i] = i();
}
for(int i=0;i<m;i++){
c[i] = i();
}
HashMap<Integer,LinkedList<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
if(a[i]!=b[i]){
if(!map.containsKey(b[i])) map.put(b[i],new LinkedList());
map.get(b[i]).add(i);
}
}
int last = -1;
if(map.containsKey(c[m-1])){
last = map.get(c[m-1]).removeFirst();
if(map.get(c[m-1]).size()==0){
map.remove(c[m-1]);
}
}else{
for(int i =0;i<n;i++){
if(b[i]==c[m-1]){
last = i;
}
}
}
if(last==-1){
sb.append("NO\n");
return;
}
int ans[] = new int[m];
ans[m-1] = last;
for(int i =0;i<m-1;i++){
if(map.containsKey(c[i])){
// required in repaint
int idx = map.get(c[i]).removeFirst();
ans[i] = idx;
if(map.get(c[i]).size()==0){
map.remove(c[i]);
}
}else{
ans[i] = last;
}
}
if(map.size()!=0){
// >=1 fence left which should have been repainted
// but have not been repainted
sb.append("NO\n");
}else{
sb.append("YES\n");
for(int i =0;i<m;i++){
sb.append(ans[i]+1).append(" ");
}
sb.append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 7727a3f37e343a13f3d3202004f12723 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
for(int i =0;i<n;i++){
a[i] = i();
}
for(int i =0;i<n;i++){
b[i] = i();
}
for(int i=0;i<m;i++){
c[i] = i();
}
HashMap<Integer,LinkedList<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
if(a[i]!=b[i]){
if(!map.containsKey(b[i])) map.put(b[i],new LinkedList());
map.get(b[i]).add(i);
}
}
int last = -1;
if(map.containsKey(c[m-1])){
last = map.get(c[m-1]).remove();
if(map.get(c[m-1]).size()==0){
map.remove(c[m-1]);
}
}else{
for(int i =0;i<n;i++){
if(b[i]==c[m-1]){
last = i;
}
}
}
if(last==-1){
sb.append("NO\n");
return;
}
int ans[] = new int[m];
ans[m-1] = last;
for(int i =0;i<m-1;i++){
if(map.containsKey(c[i])){
// required in repaint
int idx = map.get(c[i]).remove();
ans[i] = idx;
if(map.get(c[i]).size()==0){
map.remove(c[i]);
}
}else{
ans[i] = last;
}
}
if(map.size()!=0){
// >=1 fence left which should have been repainted
// but have not been repainted
sb.append("NO\n");
}else{
sb.append("YES\n");
for(int i =0;i<m;i++){
sb.append(ans[i]+1).append(" ");
}
sb.append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5c656a2a7e0e03cff3b51e07cf3b2017 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
for(int i =0;i<n;i++){
a[i] = i();
}
for(int i =0;i<n;i++){
b[i] = i();
}
for(int i=0;i<m;i++){
c[i] = i();
}
HashMap<Integer,LinkedList<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
if(a[i]!=b[i]){
if(!map.containsKey(b[i])) map.put(b[i],new LinkedList());
map.get(b[i]).add(i);
}
}
int last = -1;
if(map.containsKey(c[m-1])){
last = map.get(c[m-1]).remove();
if(map.get(c[m-1]).size()==0){
map.remove(c[m-1]);
}
}else{
for(int i =0;i<n;i++){
if(b[i]==c[m-1]){
last = i;
break;
}
}
}
if(last==-1){
sb.append("NO\n");
return;
}
int ans[] = new int[m];
ans[m-1] = last;
for(int i =0;i<m-1;i++){
if(map.containsKey(c[i])){
// required in repaint
int idx = map.get(c[i]).remove();
ans[i] = idx;
if(map.get(c[i]).size()==0){
map.remove(c[i]);
}
}else{
ans[i] = last;
}
}
if(map.size()!=0){
// >=1 fence left which should have been repainted
// but have not been repainted
sb.append("NO\n");
}else{
sb.append("YES\n");
for(int i =0;i<m;i++){
sb.append(ans[i]+1).append(" ");
}
sb.append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 876d7baf52b52094f85ebbbbc86ae19a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int initColor[] = new int[n];
for(int i =0;i<n;i++) initColor[i] = i();
int desColor[] = new int[n];
HashMap<Integer,Queue<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
desColor[i] = i();
Queue<Integer> queue = map.getOrDefault(desColor[i],new ArrayDeque());
queue.add(i);
map.put(desColor[i],queue);
}
int painterColor[] = new int[m];
for(int i =0;i<m;i++){
painterColor[i] = i();
}
int validPainter = -1;
int validPainterIdx = -1;
for(int i = m-1;i>=0;i--){
if(map.containsKey(painterColor[i])){
validPainter = i;
for(int idk : map.get(painterColor[i])){
validPainterIdx = idk;
//haha
}
if(initColor[validPainterIdx]==desColor[validPainterIdx]){
for(int idk : map.get(painterColor[i])){
if(initColor[idk]!=desColor[idk]){
validPainterIdx = idk;
}
}
}
break;
}
}
boolean flag = true;
StringBuilder currSb = new StringBuilder();
for(int i =0;i<painterColor.length;i++){
if(map.containsKey(painterColor[i])){
if(i==validPainter){
currSb.append(validPainterIdx+1).append(" ");
initColor[validPainterIdx] = painterColor[i];
}else{
Queue<Integer> queue = map.get(painterColor[i]);
while(queue.size()>1&&(initColor[queue.peek()]==desColor[queue.peek()])) queue.remove();
int idx = (queue.size()==1?queue.peek():queue.remove());
initColor[idx] = painterColor[i];
currSb.append(idx+1).append(" ");
}
}else{
if(validPainter>=i){
currSb.append(validPainterIdx+1).append(" ");
}else{
flag = false;
break;
}
}
}
boolean allSame = true;
for(int i =0;i<initColor.length;i++){
if(initColor[i]!=desColor[i]){
allSame = false;
break;
}
}
if(!flag) allSame = false;
if(allSame){
sb.append("YES").append("\n");
sb.append(currSb).append("\n");
}else{
sb.append("NO").append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 05620bdd3dcfbf0898f42fb23c5fda86 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int m = i();
int initColor[] = new int[n];
for(int i =0;i<n;i++) initColor[i] = i();
int desColor[] = new int[n];
HashMap<Integer,Queue<Integer>> map = new HashMap();
for(int i =0;i<n;i++){
desColor[i] = i();
Queue<Integer> queue = map.getOrDefault(desColor[i],new ArrayDeque());
queue.add(i);
map.put(desColor[i],queue);
}
int painterColor[] = new int[m];
for(int i =0;i<m;i++){
painterColor[i] = i();
}
int validPainter = -1;
int validPainterIdx = -1;
for(int i = m-1;i>=0;i--){
if(map.containsKey(painterColor[i])){
validPainter = i;
for(int idk : map.get(painterColor[i])){
validPainterIdx = idk;
}
if(initColor[validPainterIdx]==desColor[validPainterIdx]){
for(int idk : map.get(painterColor[i])){
if(initColor[idk]!=desColor[idk]){
validPainterIdx = idk;
}
}
}
break;
}
}
boolean flag = true;
StringBuilder currSb = new StringBuilder();
for(int i =0;i<painterColor.length;i++){
if(map.containsKey(painterColor[i])){
if(i==validPainter){
currSb.append(validPainterIdx+1).append(" ");
initColor[validPainterIdx] = painterColor[i];
}else{
Queue<Integer> queue = map.get(painterColor[i]);
while(queue.size()>1&&(initColor[queue.peek()]==desColor[queue.peek()])) queue.remove();
int idx = (queue.size()==1?queue.peek():queue.remove());
initColor[idx] = painterColor[i];
currSb.append(idx+1).append(" ");
}
}else{
if(validPainter>=i){
currSb.append(validPainterIdx+1).append(" ");
}else{
flag = false;
break;
}
}
}
boolean allSame = true;
for(int i =0;i<initColor.length;i++){
if(initColor[i]!=desColor[i]){
allSame = false;
break;
}
}
if(!flag) allSame = false;
if(allSame){
sb.append("YES").append("\n");
sb.append(currSb).append("\n");
}else{
sb.append("NO").append("\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 0242e7d0ad57a05c70586f222c1a21cf | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solver {
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while (t-- > 0) {
solve(in);
}
}
public static void solve(FastReader in) {
int n = in.nextInt(), m = in.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
int[] x = new int[m];
HashMap<Integer, LinkedList<Integer>> idxs = new HashMap<>();
int diff = 0;
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
for (int i = 0; i < n; i++)
b[i] = in.nextInt();
for (int i = 0; i < m; i++)
c[i] = in.nextInt();
int target = -1;
for (int i = 0; i < n; i++) {
if (target < 0 && b[i] == c[m - 1]) {
target = i;
}
if (a[i] != b[i]) {
if(b[i] == c[m-1])
target = i;
if (!idxs.containsKey(b[i]))
idxs.put(b[i], new LinkedList<>());
idxs.get(b[i]).addLast(i);
diff++;
}
}
if (m < diff || target < 0) {
System.out.println("NO");
return;
}
for (int i = 0; i < m; i++) {
if (!idxs.containsKey(c[i]) || idxs.get(c[i]).isEmpty()) {
x[i] = target;
} else {
x[i] = idxs.get(c[i]).removeFirst();
diff--;
}
}
if (diff > 0) {
System.out.println("NO");
} else {
System.out.println("YES");
StringBuilder s = new StringBuilder();
for (int i = 0; i < m; i++) {
s.append(x[i] + 1);
s.append(" ");
}
System.out.println(s);
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
private FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 31f59662dda68b868107bd6eea1c38dc | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solver {
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while (t-- > 0) {
solve(in);
}
}
public static void solve(FastReader in) {
int n = in.nextInt(), m = in.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
int[] x = new int[m];
HashMap<Integer, LinkedList<Integer>> idxs = new HashMap<>();
int diff = 0;
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
for (int i = 0; i < n; i++)
b[i] = in.nextInt();
for (int i = 0; i < m; i++)
c[i] = in.nextInt();
int target = -1;
for (int i = 0; i < n; i++) {
if (target < 0 && b[i] == c[m - 1]) {
target = i;
}
if (a[i] != b[i]) {
if(b[i] == c[m-1])
target = i;
if (!idxs.containsKey(b[i]))
idxs.put(b[i], new LinkedList<>());
idxs.get(b[i]).addLast(i);
diff++;
}
}
if (m < diff || target < 0) {
System.out.println("NO");
return;
}
for (int i = 0; i < m; i++) {
if (!idxs.containsKey(c[i]) || idxs.get(c[i]).isEmpty()) {
x[i] = target;
} else {
x[i] = idxs.get(c[i]).removeFirst();
diff--;
}
}
x[m-1] = target;
if (diff > 0) {
System.out.println("NO");
} else {
System.out.println("YES");
StringBuilder s = new StringBuilder();
for (int i = 0; i < m; i++) {
s.append(x[i] + 1);
s.append(" ");
}
System.out.println(s);
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
private FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | c44cb7d6154e57cd6d60914a611b07e4 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class First {
// *** ++
// +=-==+ +++=-
// +-:---==+ *+=----=
// +-:------==+ ++=------==
// =-----------=++=========================
// +--:::::---:-----============-=======+++====
// +---:..:----::-===============-======+++++++++
// =---:...---:-===================---===++++++++++
// +----:...:-=======================--==+++++++++++
// +-:------====================++===---==++++===+++++
// +=-----======================+++++==---==+==-::=++**+
// +=-----================---=======++=========::.:-+*****
// +==::-====================--: --:-====++=+===:..-=+*****
// +=---=====================-... :=..:-=+++++++++===++*****
// +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+
// +=======++++++++++++=+++++++============++++++=======+******
// +=====+++++++++++++++++++++++++==++++==++++++=:... . .+****
// ++====++++++++++++++++++++++++++++++++++++++++-. ..-+****
// +======++++++++++++++++++++++++++++++++===+====:. ..:=++++
// +===--=====+++++++++++++++++++++++++++=========-::....::-=++*
// ====--==========+++++++==+++===++++===========--:::....:=++*
// ====---===++++=====++++++==+++=======-::--===-:. ....:-+++
// ==--=--====++++++++==+++++++++++======--::::...::::::-=+++
// ===----===++++++++++++++++++++============--=-==----==+++
// =--------====++++++++++++++++=====================+++++++
// =---------=======++++++++====+++=================++++++++
// -----------========+++++++++++++++=================+++++++
// =----------==========++++++++++=====================++++++++
// =====------==============+++++++===================+++==+++++
// =======------==========================================++++++
/*
* created by : Nitesh Gupta
*
* -----REMAINDERS--------
*
* do not use arrays.sort()
*
* can == in Integers and Long instead use Integer.equals(Integer) or if
* (x.intValue() == y.intValue())
*
* Higher dimension dp gives tle
*/
public static void main(String[] args) throws Exception {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// int t = Integer.parseInt(br.readLine());
// StringBuilder sb = new StringBuilder();
// while (t-- > 0) {
// String[] scn = (br.readLine()).trim().split(" ");
// int n = Integer.parseInt(scn[0]);
// long[] arr = new long[n];
// scn = (br.readLine()).trim().split(" ");
// for (int i = 0; i < n; i++) {
// arr[i] = Long.parseLong(scn[i]);
// }
// sb.append("\n");
// }
// System.out.println(sb);
// return;
// first();
// sec();
third();
// four();
// fif();
// six();
}
private static void first() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int xx = Integer.parseInt(scn[0]);
int yy = Integer.parseInt(scn[1]);
String str = br.readLine();
int n = str.length();
int u = 0, d = 0, l = 0, r = 0;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
if (str.charAt(i) == 'U') {
u += 1;
y += 1;
} else if (str.charAt(i) == 'D') {
d += 1;
y -= 1;
} else if (str.charAt(i) == 'L') {
l += 1;
x -= 1;
} else if (str.charAt(i) == 'R') {
r += 1;
x += 1;
}
}
boolean can = false;
boolean cann = false;
if (xx >= 0) {
if (r >= xx) {
can = true;
}
}
if (xx < 0) {
if (l >= -xx) {
can = true;
}
}
if (yy >= 0) {
if (u >= yy) {
cann = true;
}
}
if (yy < 0) {
if (d >= -yy) {
cann = true;
}
}
if (can && cann)
sb.append("YES");
else {
sb.append("NO");
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void sec() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] arr = new long[n];
int count = 0;
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
long x = 1, f = 0, pos = -1;
while (x > 0) {
x = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] < arr[i + 1]) {
x += 1;
}
if (arr[i] >= arr[i + 1]) {
continue;
} else if (arr[i] < arr[i + 1]) {
arr[i] += 1;
count++;
x = n + 1;
}
if (count == m) {
f = 1;
pos = i + 1;
break;
}
if (x == n + 1) {
break;
}
}
if (f > 0) {
break;
}
}
if (f != 0) {
sb.append(pos);
} else {
sb.append("-1");
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void third() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] arr = new long[n];
long[] brr = new long[n];
long[] col = new long[m];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
ArrayList<pair> list = new ArrayList<>();
HashMap<Long, Integer> map = new HashMap<>();
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
brr[i] = Long.parseLong(scn[i]);
if (arr[i] != brr[i]) {
list.add(new pair(brr[i], i + 1));
}
map.put(brr[i], i + 1);
}
ArrayList<pair> paint = new ArrayList<>();
scn = (br.readLine()).trim().split(" ");
int[] ans = new int[m + 1];
for (int i = 0; i < m; i++) {
col[i] = Long.parseLong(scn[i]);
paint.add(new pair(col[i], i + 1));
}
Collections.sort(list, (a, b) -> {
if (a.col == b.col) {
return a.idx - b.idx;
}
return a.col > b.col ? 1 : -1;
});
Collections.sort(paint, (a, b) -> {
if (a.col == b.col) {
return a.idx - b.idx;
}
return a.col > b.col ? 1 : -1;
});
int s = 0;
int e = 0;
boolean can = true;
while (s < list.size() && e < paint.size()) {
pair a = list.get(s);
pair b = paint.get(e);
if (a.col == b.col) {
ans[b.idx] = a.idx;
s += 1;
e += 1;
} else if (a.col < b.col) {
s += 1;
can = false;
break;
} else {
e += 1;
}
}
if (s != list.size()) {
can = false;
}
if (!can) {
sb.append("NO");
} else {
int idx = ans[m];
if (idx == 0 && map.containsKey(col[m - 1])) {
idx = map.get(col[m - 1]);
}
if (idx == 0) {
sb.append("NO");
} else {
sb.append("YES\n");
for (int i = 1; i <= m; i++) {
if (ans[i] == 0) {
sb.append(idx + " ");
} else {
sb.append(ans[i] + " ");
}
}
}
}
sb.append("\n");
}
System.out.println(sb);
return;
}
static class pair {
long col;
int idx;
pair(long col, int idx) {
this.col = col;
this.idx = idx;
}
}
private static void four() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void fif() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void six() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
public static void sort(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
long temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | fef2ca29cdb8f3702bd6029f7e3ed6e5 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class First {
// *** ++
// +=-==+ +++=-
// +-:---==+ *+=----=
// +-:------==+ ++=------==
// =-----------=++=========================
// +--:::::---:-----============-=======+++====
// +---:..:----::-===============-======+++++++++
// =---:...---:-===================---===++++++++++
// +----:...:-=======================--==+++++++++++
// +-:------====================++===---==++++===+++++
// +=-----======================+++++==---==+==-::=++**+
// +=-----================---=======++=========::.:-+*****
// +==::-====================--: --:-====++=+===:..-=+*****
// +=---=====================-... :=..:-=+++++++++===++*****
// +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+
// +=======++++++++++++=+++++++============++++++=======+******
// +=====+++++++++++++++++++++++++==++++==++++++=:... . .+****
// ++====++++++++++++++++++++++++++++++++++++++++-. ..-+****
// +======++++++++++++++++++++++++++++++++===+====:. ..:=++++
// +===--=====+++++++++++++++++++++++++++=========-::....::-=++*
// ====--==========+++++++==+++===++++===========--:::....:=++*
// ====---===++++=====++++++==+++=======-::--===-:. ....:-+++
// ==--=--====++++++++==+++++++++++======--::::...::::::-=+++
// ===----===++++++++++++++++++++============--=-==----==+++
// =--------====++++++++++++++++=====================+++++++
// =---------=======++++++++====+++=================++++++++
// -----------========+++++++++++++++=================+++++++
// =----------==========++++++++++=====================++++++++
// =====------==============+++++++===================+++==+++++
// =======------==========================================++++++
/*
* created by : Nitesh Gupta
*
* -----REMAINDERS--------
*
* do not use arrays.sort()
*
* can == in Integers and Long instead use Integer.equals(Integer) or if
* (x.intValue() == y.intValue())
*
* Higher dimension dp gives tle
*/
public static void main(String[] args) throws Exception {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// int t = Integer.parseInt(br.readLine());
// StringBuilder sb = new StringBuilder();
// while (t-- > 0) {
// String[] scn = (br.readLine()).trim().split(" ");
// int n = Integer.parseInt(scn[0]);
// long[] arr = new long[n];
// scn = (br.readLine()).trim().split(" ");
// for (int i = 0; i < n; i++) {
// arr[i] = Long.parseLong(scn[i]);
// }
// sb.append("\n");
// }
// System.out.println(sb);
// return;
// first();
// sec();
third();
// four();
// fif();
// six();
}
private static void first() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int xx = Integer.parseInt(scn[0]);
int yy = Integer.parseInt(scn[1]);
String str = br.readLine();
int n = str.length();
int u = 0, d = 0, l = 0, r = 0;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
if (str.charAt(i) == 'U') {
u += 1;
y += 1;
} else if (str.charAt(i) == 'D') {
d += 1;
y -= 1;
} else if (str.charAt(i) == 'L') {
l += 1;
x -= 1;
} else if (str.charAt(i) == 'R') {
r += 1;
x += 1;
}
}
boolean can = false;
boolean cann = false;
if (xx >= 0) {
if (r >= xx) {
can = true;
}
}
if (xx < 0) {
if (l >= -xx) {
can = true;
}
}
if (yy >= 0) {
if (u >= yy) {
cann = true;
}
}
if (yy < 0) {
if (d >= -yy) {
cann = true;
}
}
if (can && cann)
sb.append("YES");
else {
sb.append("NO");
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void sec() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] arr = new long[n];
int count = 0;
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
long x = 1, f = 0, pos = -1;
while (x > 0) {
x = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] < arr[i + 1]) {
x += 1;
}
if (arr[i] >= arr[i + 1]) {
continue;
} else if (arr[i] < arr[i + 1]) {
arr[i] += 1;
count++;
x = n + 1;
}
if (count == m) {
f = 1;
pos = i + 1;
break;
}
if (x == n + 1) {
break;
}
}
if (f > 0) {
break;
}
}
if (f != 0) {
sb.append(pos);
} else {
sb.append("-1");
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void third() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
int m = Integer.parseInt(scn[1]);
long[] arr = new long[n];
long[] brr = new long[n];
long[] col = new long[m];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
ArrayList<pair> list = new ArrayList<>();
HashMap<Long, Integer> map = new HashMap<>();
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
brr[i] = Long.parseLong(scn[i]);
if (arr[i] != brr[i]) {
list.add(new pair(brr[i], i + 1));
}
map.put(brr[i], i + 1);
}
ArrayList<pair> paint = new ArrayList<>();
scn = (br.readLine()).trim().split(" ");
int[] ans = new int[m + 1];
for (int i = 0; i < m; i++) {
col[i] = Long.parseLong(scn[i]);
paint.add(new pair(col[i], i + 1));
}
Collections.sort(list, (a, b) -> {
if (a.col == b.col) {
return a.idx - b.idx;
}
return a.col > b.col ? 1 : -1;
});
Collections.sort(paint, (a, b) -> {
if (a.col == b.col) {
return a.idx - b.idx;
}
return a.col > b.col ? 1 : -1;
});
int s = 0;
int e = 0;
boolean can = true;
while (s < list.size() && e < paint.size()) {
pair a = list.get(s);
pair b = paint.get(e);
if (a.col == b.col) {
ans[b.idx] = a.idx;
s += 1;
e += 1;
} else if (a.col < b.col) {
s += 1;
can = false;
break;
} else {
e += 1;
}
}
if (s != list.size()) {
can = false;
}
if (!can) {
sb.append("NO");
} else {
int idx = ans[m];
if (idx == 0 && map.containsKey(col[m - 1])) {
idx = map.get(col[m - 1]);
}
if (idx == 0) {
sb.append("NO");
} else {
sb.append("YES\n");
for (int i = 1; i <= m; i++) {
if (ans[i] == 0) {
sb.append(idx + " ");
arr[idx - 1] = col[i - 1];
} else {
sb.append(ans[i] + " ");
arr[ans[i] - 1] = col[i - 1];
}
}
}
}
sb.append("\n");
}
System.out.println(sb);
return;
}
static class pair {
long col;
int idx;
pair(long col, int idx) {
this.col = col;
this.idx = idx;
}
}
private static void four() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void fif() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
private static void six() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] scn = (br.readLine()).trim().split(" ");
int t = Integer.parseInt(scn[0]);
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
sb.append("\n");
}
System.out.println(sb);
return;
}
public static void sort(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
long temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 90f570b24e0b57ec2fd0db5ab5e123a0 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int testCases = sc.nextInt();
for (int t = 0; t < testCases; t++) {
int n = sc.nextInt();
int m = sc.nextInt();
int in_col[] = new int[n];
for (int i = 0; i < n; i++) {
in_col[i] = sc.nextInt();
}
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
int des_col[] = new int[n];
for (int i = 0; i < n; i++) {
des_col[i] = sc.nextInt();
if (in_col[i] == des_col[i]) {
continue;
}
ArrayList<Integer> al = map.getOrDefault(des_col[i], new ArrayList<>());
al.add(i);
map.put(des_col[i], al);
}
int paint[] = new int[m];
for (int i = 0; i < m; i++) {
paint[i] = sc.nextInt();
}
if (n == 1) {
sb.append("YES").append("\n");
sb.append("1").append("\n");
continue;
}
int toColor = -1;
for (int i = n - 1; i >= 0; i--) {
if (des_col[i] == paint[m - 1] && in_col[i] != des_col[i]) {
toColor = i;
break;
}
}
if (toColor == -1) {
for (int i = n - 1; i >= 0; i--) {
if (des_col[i] == paint[m - 1]) {
toColor = i;
break;
}
}
}
if (toColor == -1) {
sb.append("NO").append("\n");
continue;
}
ArrayList<Integer> temp = map.get(paint[m - 1]);
if (temp != null) {
temp.remove((Object) toColor);
map.put(paint[m - 1], temp);
}
int index[] = new int[n + 1];
StringBuilder ans = new StringBuilder();
for (int i = 0; i < m - 1; i++) {
int cur = paint[i];
if (!map.containsKey(cur)) {
ans.append(toColor + 1).append(" ");
continue;
}
ArrayList<Integer> al = map.get(cur);
if (index[cur] >= al.size()) {
ans.append(toColor + 1).append(" ");
continue;
}
ans.append(al.get(index[cur]) + 1).append(" ");
in_col[al.get(index[cur])] = cur;
index[cur]++;
}
ans.append(toColor + 1).append(" ");
in_col[toColor] = paint[m - 1];
boolean poss = true;
for (int i = 0; i < n; i++) {
if (in_col[i] != des_col[i]) {
poss = false;
}
}
if (!poss) {
sb.append("NO").append("\n");
continue;
}
sb.append("YES").append("\n");
sb.append(ans).append("\n");
}
System.out.print(sb);
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 8775c54871b0b82d7331c8950fae69db | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class Main {
static long MOD = (long) 1e9 + 7;
public static void main(String[] args) {
InputReader fs = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = fs.readInt();
for (int tt = 0; tt < t; tt++) {
int n = fs.readInt(), m = fs.readInt();
int[] a = fs.readIntArrayShifted(n);
int[] b = fs.readIntArrayShifted(n);
int[] c = fs.readIntArrayShifted(m);
HashSet<Integer> available = new HashSet<>();
for (int i: c) available.add(i);
boolean shuruMeHi = false;
for (int i = 1; i <= n; i++) {
if (a[i] == b[i]) continue;
if (!available.contains(b[i])) {
pw.println("NO");
// System.out.println("top");
shuruMeHi = true;
break;
}
}
if (shuruMeHi) {
continue;
}
ArrayList<ArrayList<Integer>> changeTo = new ArrayList<>();
ArrayList<ArrayList<Integer>> same = new ArrayList<>();
for (int i = 0; i <= n; i++) changeTo.add(new ArrayList<>());
for (int i = 0; i <= n; i++) same.add(new ArrayList<>());
for (int i = 1; i <= n; i++) {
if (b[i] != a[i]) changeTo.get(b[i]).add(i);
else same.get(b[i]).add(i);
}
for (ArrayList<Integer> list: changeTo) Collections.reverse(list);
int[] res = new int[m+1];
// ArrayDeque<Integer> notUsed = new ArrayDeque<>();
// System.out.println("changeTo: " + changeTo);
// System.out.println("same: " + same);
boolean[] coloured = new boolean[n+1];
for (int i = 1; i <= n; i++) if (a[i] == b[i]) coloured[a[i]] = true;
for (int i = 1; i <= m; i++) {
if (changeTo.get(c[i]).size() > 0) {
res[i] = changeTo.get(c[i]).remove(changeTo.get(c[i]).size() - 1);
coloured[c[i]] = true;
}
}
if (!coloured[c[m]]) {
pw.println("NO");
// System.out.println("second");
continue;
}
else {
int idx = -1;
if (res[m] != 0) idx = res[m];
else for (int i = n; i >= 0; i--) {
if (b[i] == c[m]) {
idx = i;
break;
}
}
for (int i = 1; i<= m; i++) {
if (res[i] == 0) res[i] = idx;
}
}
for (int i = 1; i <= m; i++) {
a[res[i]] = c[i];
}
boolean lastPakka = false;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i]) {
lastPakka = true;
break;
}
}
if (lastPakka) {
pw.println("NO");
continue;
}
pw.println("YES");
for (int i = 1; i <= m; i++) {
pw.print(res[i] + " ");
}
pw.println();
}
pw.flush();
pw.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = readInt();
}
return a;
}
public int[] readIntArrayShifted(int n) {
int[] a = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i + 1] = readInt();
}
return a;
}
public long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = readLong();
}
return a;
}
public String next() {
return readString();
}
interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 36ed353ca4d50e497812d9689d636368 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.io.*;
import java.util.*;
public class NewClass {
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String in = br.readLine();
int t=Integer.parseInt(in);
while(t-->0){
String num[] = br.readLine().split(" ");
int n = Integer.parseInt(num[0]);
int m = Integer.parseInt(num[1]);
String[] a = br.readLine().split(" ");
String[] b = br.readLine().split(" ");
String[] c = br.readLine().split(" ");
ArrayList<Integer> req = new ArrayList<Integer>();
int last_ind =-1;
for(int i=0;i<n;i++){
if(!a[i].equalsIgnoreCase(b[i])){
req.add(i);
}
if(c[m-1].equalsIgnoreCase(b[i])){
last_ind = i;
}
}
for(int i=0;i<req.size();i++){
if(c[m-1].equalsIgnoreCase(b[req.get(i)])){
last_ind = req.get(i);
}
}
if(last_ind<0){
System.out.println("NO");
}
else{
boolean visit[] = new boolean[m];
int paint[] = new int[m];
int[] fis = new int[n+1];
int[] sec = new int[n+1];
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> arr2 = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<n+1;i++){
arr.add(new ArrayList<Integer>());
arr2.add(new ArrayList<Integer>());
}
for(int i=0;i<req.size();i++){
fis[Integer.parseInt(b[req.get(i)])]++;
arr.get(Integer.parseInt(b[req.get(i)])).add(req.get(i));
}
for(int i=0;i<m;i++){
sec[Integer.parseInt(c[i])]++;
arr2.get(Integer.parseInt(c[i])).add(i);
}
// for(int i:fis){
// System.out.print(i+" ");
// }
// System.out.println("");
// for(int i=0;i<n+1;i++){
// System.out.print(arr.get(i).size()+" ");
// }
// System.out.println("");
// for(int i:sec){
// System.out.print(i+" ");
// }
// System.out.println("");
// for(int i=0;i<n+1;i++){
// System.out.print(arr2.get(i).size()+" ");
// }
// System.out.println("");
boolean ch=true;
for(int i=1;i<n+1;i++){
if(fis[i]>sec[i]){
ch=false;
break;
}
else{
for(int j=0;j<fis[i];j++){
paint[arr2.get(i).get(j)]=arr.get(i).get(j)+1;
}
}
}
if(!ch){
System.out.println("NO");
}
else{
System.out.println("YES");
for(int i=0;i<m;i++){
if(paint[i]==0){
System.out.print((last_ind+1)+" ");
}
else{
System.out.print(paint[i]+" ");
}
}
System.out.println("");
}
}
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 71955c51fb0dd423bc3b7b21bdb936d5 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public final class Codechef {
public static void main(String[] args){
// Scanner sc=new Scanner(System.in);
FastReader sc=new FastReader();
PrintWriter writer=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt(), m=sc.nextInt();
ArrayList<Integer>[] aa=new ArrayList[n+1];
for(int i=0;i<n+1;i++) aa[i]=new ArrayList<>();
int[] a=new int[n], b=new int[n], c=new int[m];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
for(int i=0;i<n;i++) {
b[i]=sc.nextInt();
if(a[i]!=b[i]) { aa[b[i]].add(i); }
}
for(int i=0;i<m;i++) c[i]=sc.nextInt();
int j=0;
String s="YES";
int[] ans=new int[m];
int x=-1;
if(aa[c[m-1]].size()>0) x=aa[c[m-1]].get(aa[c[m-1]].size()-1);
else {
for(int i=0;i<n;i++) {
if(b[i]==c[m-1]) x=i;
}
}
if(x==-1) {
s="NO";
}else {
for(int i=0;i<m;i++) {
if(aa[c[i]].size()>0) ans[j]=aa[c[i]].remove(0);
else ans[j]=x;
j++;
}
for(int i=0;i<n+1;i++) {
if(aa[i].size()!=0) {s="NO";break;}
}
}
if(s.equals("NO")) {
writer.print(s);
}else{
writer.print(s+"\n");
for(int i=0;i<m;i++) writer.print((ans[i]+1)+" ");
}
writer.print("\n");
}
writer.flush();
writer.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 64dec3f60c156761161bb8117170af68 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
final public class C {
static HashMap<Integer, ArrayList<Integer>> hm = new HashMap<>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int tst = Integer.parseInt(br.readLine());
while(tst-->0){
hm.clear();
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]), m = Integer.parseInt(str[1]);
StringTokenizer st1 = new StringTokenizer(br.readLine());
StringTokenizer st2 = new StringTokenizer(br.readLine());
StringTokenizer st3 = new StringTokenizer(br.readLine());
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
int[] need = new int[n+1];
for(int i = 0; i<n; i++){
a[i] = Integer.parseInt(st1.nextToken());
b[i] = Integer.parseInt(st2.nextToken());
if(a[i]!=b[i]){
need[b[i]]++;
ArrayList<Integer> l;
if(hm.containsKey(b[i])){
l = hm.get(b[i]);
}
else {
l = new ArrayList<>();
}
l.add(i);
hm.put(b[i], l);
}
}
int[] avail = new int[n+1];
for(int i = 0; i<m; i++){
c[i] = Integer.parseInt(st3.nextToken());
avail[c[i]]++;
}
boolean poss = true;
int last = 0;
for(int i = 0; i<n+1; i++){
if(avail[i]<need[i]){
poss = false;
break;
}
}
boolean lastPresent = false;
for(int j = 0; j<n; j++){
if(b[j] == c[m-1]){
if(need[c[m-1]] == 0) {
last = j;
lastPresent = true;
break;
}
else{
if(b[j]!=a[j]){
last = j;
lastPresent = true;
break;
}
}
}
}
if(poss&lastPresent){
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0; i<m; i++){
if(i == m-1) ans.add(last+1);
else if(need[c[i]]>0){
need[c[i]]--;
ArrayList<Integer> ndx = hm.get(c[i]);
ans.add(ndx.get(ndx.size()-1)+1);
ndx.remove(ndx.size()-1);
hm.put(c[i], ndx);
}
else ans.add(last+1);
}
sb.append("YES").append('\n');
for(int i: ans) sb.append(i+" ");
sb.append('\n');
}
else sb.append("NO").append('\n');
}
System.out.println(sb);
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | b73bd5b5225b1bc53f59e8fd0368d60b | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.Scanner;
public class P1481C{
public static void main (String arr[]){
Scanner ip = new Scanner(System .in);
int t = ip.nextInt();
int m , n;
int a[],b[],c[],ans[];
int put;
boolean pass,bre;
while(t-->0){
pass = true;
bre = false;
m = ip.nextInt();
n = ip.nextInt();
a = new int [m];
b = new int [m];
c = new int [n];
ans = new int [n];
for(int i=0; i < m ; i++){
a[i] = ip.nextInt();
}
for(int i=0; i < m ; i++){
b[i] = ip.nextInt();
}
for(int i=0; i < n ; i++){
c[i] = ip.nextInt();
ans[i]=-1;
}
for(int i=m-1; i >=0 ; i--){
if(a[i]!=b[i]){
bre=true;
for(int j=n-1;j>=0;j--){
if(c[j] == b[i]){
ans[j] = i+1;
c[j]=-1;
bre=false;
break;
}
}
}
if(bre)
break;
}
if(bre){
System.out.println("no");
continue;
}
bre=true;
if(ans[n-1]==-1){
bre=false;
for(int j=m-1;j>=0;j--){
if(b[j]==c[n-1]){
ans[n-1]=j+1;
bre=true;
break;
}
}
}
if(!bre){
System.out.println("no");
continue;
}
put = ans[n-1];
for(int i=n-2;i>=0;i--){
if(ans[i]==-1)
ans[i]=put;
}
System.out.println("yes");
for(int i=0;i<n;i++){
System.out.printf("%d ",ans[i]);
}
System.out.println();
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 17270e4b03c3e15cd97b2120009c16c9 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;import java.io.*;import java.math.*;
public class FencePainting
{
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
ArrayList<Integer> lst = new ArrayList<>();
for(int i : a)
lst.add(i);
Collections.sort(lst);
for(int i = 0; i < n; i++)
a[i] = lst.get(i);
}
static void ruffleSort(long[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
ArrayList<Long> lst = new ArrayList<>();
for(long i : a)
lst.add(i);
Collections.sort(lst);
for(int i = 0; i < n; i++)
a[i] = lst.get(i);
}
public static void process()throws IOException
{
int n = ni(), m = ni();
int[] a = nai(n);
int[] b = nai(n);
int[] c = nai(m);
TreeSet<Integer> lst[] = new TreeSet[n+1];
for(int i = 0; i <= n; i++)
lst[i] = new TreeSet<>();
int[] dp = new int[n+1];
Arrays.fill(dp, -1);
int pos = 0, f = 0;
for(int i = 0; i < n; i++){
if(a[i] != b[i]){
lst[b[i]].add(i);
//pos = i;
}
else
dp[a[i]] = i;
}
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0; i < m; i++){
if(lst[c[i]].size() != 0){
ans.add(lst[c[i]].first());
pos = lst[c[i]].last();
dp[c[i]] = pos;
a[lst[c[i]].first()] = c[i];
lst[c[i]].remove(lst[c[i]].first());
}
else if(dp[c[i]] >= 0){
pos = dp[c[i]];
ans.add(dp[c[i]]);
}
else{
ans.add(-1);
}
}
for(int i = 0; i < n; i++){
if(a[i] != b[i])
f = 1;
}
/*for(int i : a){
p(i+" ");
}*/
//pn(ans);
if(ans.get(ans.size()-1) == -1){
pn("NO");
return;
}
if(f == 0){
pn("YES");
for(int i : ans){
if(i == -1)
p(pos+1+" ");
else
p(i+1+" ");
}
pn("");
}
else
pn("NO");
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
long s = System.currentTimeMillis();
int t=1;
t=ni();
//int k = t;
while(t-->0) {/*p("Case #"+ (k-t) + ": ")*/;process();}
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static long power(long k, long c, long mod){
long y = 1;
while(c > 0){
if(c%2 == 1)
y = y * k % mod;
c = c/2;
k = k * k % mod;
}
return y;
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 7af903cd5476e1f131a9b6e07c68625d | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;import java.io.*;import java.math.*;
public class FencePainting
{
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
ArrayList<Integer> lst = new ArrayList<>();
for(int i : a)
lst.add(i);
Collections.sort(lst);
for(int i = 0; i < n; i++)
a[i] = lst.get(i);
}
static void ruffleSort(long[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
ArrayList<Long> lst = new ArrayList<>();
for(long i : a)
lst.add(i);
Collections.sort(lst);
for(int i = 0; i < n; i++)
a[i] = lst.get(i);
}
public static void process()throws IOException
{
int n = ni(), m = ni();
int[] a = nai(n);
int[] b = nai(n);
int[] c = nai(m);
TreeSet<Integer> lst[] = new TreeSet[n+1];
for(int i = 0; i <= n; i++)
lst[i] = new TreeSet<>();
int[] dp = new int[n+1];
Arrays.fill(dp, -1);
int pos = 0, f = 0;
for(int i = 0; i < n; i++){
if(a[i] != b[i]){
lst[b[i]].add(i);
//pos = i;
}
else
dp[a[i]] = i;
}
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0; i < m; i++){
if(lst[c[i]].size() != 0){
ans.add(lst[c[i]].first());
pos = lst[c[i]].first();
dp[c[i]] = pos;
a[lst[c[i]].first()] = c[i];
lst[c[i]].remove(lst[c[i]].first());
}
else if(dp[c[i]] >= 0){
pos = dp[c[i]];
ans.add(dp[c[i]]);
}
else{
ans.add(-1);
}
}
for(int i = 0; i <= n; i++){
if(lst[i].size() > 0){
pn("NO");
return;
}
}
/*for(int i : a){
p(i+" ");
}*/
//pn(ans);
if(ans.get(ans.size()-1) == -1){
pn("NO");
return;
}
if(f == 0){
pn("YES");
for(int i : ans){
if(i == -1)
p(pos+1+" ");
else
p(i+1+" ");
}
pn("");
}
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
long s = System.currentTimeMillis();
int t=1;
t=ni();
//int k = t;
while(t-->0) {/*p("Case #"+ (k-t) + ": ")*/;process();}
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static long power(long k, long c, long mod){
long y = 1;
while(c > 0){
if(c%2 == 1)
y = y * k % mod;
c = c/2;
k = k * k % mod;
}
return y;
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 54a076fc03f166e07d6cc7e60d2aae5c | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int c[] = new int[m];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
for(int i=0;i<n;i++) {
b[i] = sc.nextInt();
}
for(int i=0;i<m;i++) {
c[i] = sc.nextInt();
}
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for(int i=0; i<n; i++){
if(a[i]!=b[i]){
if(!map.containsKey(b[i])) {
map.put(b[i], new ArrayList<>());
}
map.get(b[i]).add(i+1);
}
}
int fixed = c[m-1];
int ind = -1;
if(map.containsKey(fixed)){
ind = map.get(fixed).get(0);
map.get(fixed).remove(0);
}
else{
for(int i=0; i<n; i++){
if(b[i]==fixed){
ind = i+1;
break;
}
}
}
if(ind==-1){
System.out.println("NO");
continue;
}
StringBuilder sb = new StringBuilder();
for(int i=0; i<m; i++){
if(i==m-1) {
sb.append(ind);
}
else if(!map.containsKey(c[i]) || map.get(c[i]).size()==0){
sb.append(ind).append(" ");
}
else{
sb.append(map.get(c[i]).get(0)).append(" ");
map.get(c[i]).remove(0);
}
}
boolean confirm = false;
for(Map.Entry<Integer, ArrayList<Integer>> entry : map.entrySet()){
if(entry.getValue().size()>=1){
confirm = true;
break;
}
}
if(confirm) {
System.out.println("NO");
}
else{
System.out.println("YES");
System.out.println(sb);
}
}
System.out.println(res);
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | edfff8cddaa0ed8284e7c31730343425 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
//@author->.....future_me......//
//..............Learning.........//
/*Compete against yourself*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
public class A {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
//
int t = sc.nextInt();
while (t-- > 0)
A.go();
out.flush();
// } catch (Exception e) {
// return;
// }
}
static int count = 0;
static HashSet<Integer> set = new HashSet<Integer>();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static void go() {
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n; i++)
b[i] = sc.nextInt();
for (int i = 0; i < m; i++)
c[i] = sc.nextInt();
LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
LinkedHashMap<Integer, ArrayList<Integer>> map1 = new LinkedHashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (map.containsKey(b[i])) {
ArrayList<Integer> aa = map.get(b[i]);
aa.add(i);
map.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map.put(b[i], ind);
}
} else {
if (map1.containsKey(b[i])) {
ArrayList<Integer> aa = map1.get(b[i]);
aa.add(i);
map1.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map1.put(b[i], ind);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(map1.values());
if (map.size() == 0) {
ArrayList<Integer> forces = new ArrayList<>();
ArrayList<Integer> anss = new ArrayList<>();
for (int i = 0; i < m; i++) {
if (map1.containsKey(c[i])) {
for (int j = 0; j < forces.size(); j++) {
anss.add((map1.get(c[i]).get(0) + 1));
}
forces.clear();
anss.add((map1.get(c[i]).get(0) + 1));
} else {
forces.add(c[i]);
}
}
if (forces.size() == 0) {
out.println("YES");
for (int i : anss) {
out.print(i + " ");
}
out.println();
} else {
out.println("NO");
}
return;
}
ArrayList<Integer> ans = new ArrayList<>();
ArrayList<Integer> forced = new ArrayList<>();
for (int i = 0; i < m; i++) {
int x = c[i];
if (map.containsKey(x)) {
ArrayList<Integer> aa = map.get(x);
// ArrayList<Integer> ass = new ArrayList<>();
//
// ass.add(aa.get(0));
if (map1.containsKey(x)) {
ArrayList<Integer> aft = map1.get(x);
aft.add(aa.get(0));
map1.put(x, aft);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(aa.get(0));
map1.put(x, ind);
}
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
map.remove(x);
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
if (map1.containsKey(x)) {
// out.print("--" + map1.get(x).size());
ArrayList<Integer> aa = map1.get(x);
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
forced.add(x);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(forced.size());
// for (int i : forced) {
// if (map1.containsKey(i) == false) {
// out.println("NO");
// return;
// }
// }
if (map.size() == 0&&forced.size()==0) {
out.println("YES");
for (int i : ans) {
out.print((i + 1) + " ");
}
out.println();
} else {
out.println("NO");
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int prime[] = new int[1000005];
static int N = 1000005;
static void sieve() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i <= N; i++) {
if (prime[i] == 0) {
for (int j = i * i; j < N; j += i) {
prime[j] = 1;
}
}
}
}
/*
* 5 16 9 8 7 7 1
*/
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (Integer i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (Long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static int mod = (int) 1e9 + 7;
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class cell {
long f;
int s;
cell(long f, int s) {
this.f = f;
this.s = s;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 3f0bc9806a6ff9de002a69758f66a657 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
//@author->.....future_me......//
//..............Learning.........//
/*Compete against yourself*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
public class A {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
// try {
//
int t = sc.nextInt();
while (t-- > 0)
A.go();
out.flush();
// } catch (Exception e) {
// return;
// }
}
static int count = 0;
static HashSet<Integer> set = new HashSet<Integer>();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static void go() {
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n; i++)
b[i] = sc.nextInt();
for (int i = 0; i < m; i++)
c[i] = sc.nextInt();
LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
LinkedHashMap<Integer, ArrayList<Integer>> map1 = new LinkedHashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (map.containsKey(b[i])) {
ArrayList<Integer> aa = map.get(b[i]);
aa.add(i);
map.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map.put(b[i], ind);
}
} else {
if (map1.containsKey(b[i])) {
ArrayList<Integer> aa = map1.get(b[i]);
aa.add(i);
map1.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map1.put(b[i], ind);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(map1.values());
// if (map.size() == 0) {
// ArrayList<Integer> forces = new ArrayList<>();
// ArrayList<Integer> anss = new ArrayList<>();
// for (int i = 0; i < m; i++) {
// if (map1.containsKey(c[i])) {
// for (int j = 0; j < forces.size(); j++) {
// anss.add((map1.get(c[i]).get(0) + 1));
// }
// forces.clear();
// anss.add((map1.get(c[i]).get(0) + 1));
// } else {
// forces.add(c[i]);
// }
// }
// if (forces.size() == 0) {
// out.println("YES");
// for (int i : anss) {
// out.print(i + " ");
// }
// out.println();
// } else {
// out.println("NO");
// }
// return;
// }
ArrayList<Integer> ans = new ArrayList<>();
ArrayList<Integer> forced = new ArrayList<>();
for (int i = 0; i < m; i++) {
int x = c[i];
if (map.containsKey(x)) {
ArrayList<Integer> aa = map.get(x);
// ArrayList<Integer> ass = new ArrayList<>();
//
// ass.add(aa.get(0));
if (map1.containsKey(x)) {
ArrayList<Integer> aft = map1.get(x);
aft.add(aa.get(0));
map1.put(x, aft);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(aa.get(0));
map1.put(x, ind);
}
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
map.remove(x);
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
if (map1.containsKey(x)) {
// out.print("--" + map1.get(x).size());
ArrayList<Integer> aa = map1.get(x);
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
forced.add(x);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(forced.size());
for (int i : forced) {
if (map1.containsKey(i) == false) {
out.println("NO");
return;
}
}
if (map.size() == 0) {
out.println("YES");
for (int i : ans) {
out.print((i + 1) + " ");
}
out.println();
} else {
out.println("NO");
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int prime[] = new int[1000005];
static int N = 1000005;
static void sieve() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i <= N; i++) {
if (prime[i] == 0) {
for (int j = i * i; j < N; j += i) {
prime[j] = 1;
}
}
}
}
/*
* 5 16 9 8 7 7 1
*/
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (Integer i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (Long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static int mod = (int) 1e9 + 7;
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class cell {
long f;
int s;
cell(long f, int s) {
this.f = f;
this.s = s;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | f78a44611c36922d9c726e6a022a7013 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
//@author->.....future_me......//
//..............Learning.........//
/*Compete against yourself*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
public class A {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
// try {
//
int t = sc.nextInt();
while (t-- > 0)
A.go();
out.flush();
// } catch (Exception e) {
// return;
// }
}
static int count = 0;
static HashSet<Integer> set = new HashSet<Integer>();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static void go() {
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n; i++)
b[i] = sc.nextInt();
for (int i = 0; i < m; i++)
c[i] = sc.nextInt();
LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
LinkedHashMap<Integer, ArrayList<Integer>> map1 = new LinkedHashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (map.containsKey(b[i])) {
ArrayList<Integer> aa = map.get(b[i]);
aa.add(i);
map.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map.put(b[i], ind);
}
} else {
if (map1.containsKey(b[i])) {
ArrayList<Integer> aa = map1.get(b[i]);
aa.add(i);
map1.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map1.put(b[i], ind);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(map1.values());
if (map.size() == 0) {
ArrayList<Integer> forces = new ArrayList<>();
ArrayList<Integer> anss = new ArrayList<>();
for (int i = 0; i < m; i++) {
if (map1.containsKey(c[i])) {
for (int j = 0; j < forces.size(); j++) {
anss.add((map1.get(c[i]).get(0) + 1));
}
forces.clear();
anss.add((map1.get(c[i]).get(0) + 1));
} else {
forces.add(c[i]);
}
}
if (forces.size() == 0) {
out.println("YES");
for (int i : anss) {
out.print(i + " ");
}
out.println();
} else {
out.println("NO");
}
return;
}
ArrayList<Integer> ans = new ArrayList<>();
ArrayList<Integer> forced = new ArrayList<>();
for (int i = 0; i < m; i++) {
int x = c[i];
if (map.containsKey(x)) {
ArrayList<Integer> aa = map.get(x);
// ArrayList<Integer> ass = new ArrayList<>();
//
// ass.add(aa.get(0));
if (map1.containsKey(x)) {
ArrayList<Integer> aft = map1.get(x);
aft.add(aa.get(0));
map1.put(x, aft);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(aa.get(0));
map1.put(x, ind);
}
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
map.remove(x);
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
if (map1.containsKey(x)) {
// out.print("--" + map1.get(x).size());
ArrayList<Integer> aa = map1.get(x);
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
forced.add(x);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(forced.size());
// for (int i : forced) {
// if (map1.containsKey(i) == false) {
// out.println("NO");
// return;
// }
// }
if (map.size() == 0&&forced.size()==0) {
out.println("YES");
for (int i : ans) {
out.print((i + 1) + " ");
}
out.println();
} else {
out.println("NO");
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int prime[] = new int[1000005];
static int N = 1000005;
static void sieve() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i <= N; i++) {
if (prime[i] == 0) {
for (int j = i * i; j < N; j += i) {
prime[j] = 1;
}
}
}
}
/*
* 5 16 9 8 7 7 1
*/
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (Integer i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (Long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static int mod = (int) 1e9 + 7;
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class cell {
long f;
int s;
cell(long f, int s) {
this.f = f;
this.s = s;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 6da4ac383ce9d66a3f76241ff52951b5 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
//@author->.....future_me......//
//..............Learning.........//
/*Compete against yourself*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
public class A {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
// try {
//
int t = sc.nextInt();
while (t-- > 0)
A.go();
out.flush();
// } catch (Exception e) {
// return;
// }
}
static int count = 0;
static HashSet<Integer> set = new HashSet<Integer>();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static void go() {
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n], b = new int[n], c = new int[m];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n; i++)
b[i] = sc.nextInt();
for (int i = 0; i < m; i++)
c[i] = sc.nextInt();
LinkedHashMap<Integer, ArrayList<Integer>> map = new LinkedHashMap<>();
LinkedHashMap<Integer, ArrayList<Integer>> map1 = new LinkedHashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (map.containsKey(b[i])) {
ArrayList<Integer> aa = map.get(b[i]);
aa.add(i);
map.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map.put(b[i], ind);
}
} else {
if (map1.containsKey(b[i])) {
ArrayList<Integer> aa = map1.get(b[i]);
aa.add(i);
map1.put(b[i], aa);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(i);
map1.put(b[i], ind);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(map1.values());
if (map.size() == 0) {
ArrayList<Integer> forces = new ArrayList<>();
ArrayList<Integer> anss = new ArrayList<>();
for (int i = 0; i < m; i++) {
if (map1.containsKey(c[i])) {
for (int j = 0; j < forces.size(); j++) {
anss.add((map1.get(c[i]).get(0) + 1));
}
forces.clear();
anss.add((map1.get(c[i]).get(0) + 1));
} else {
forces.add(c[i]);
}
}
if (forces.size() == 0) {
out.println("YES");
for (int i : anss) {
out.print(i + " ");
}
out.println();
} else {
out.println("NO");
}
return;
}
ArrayList<Integer> ans = new ArrayList<>();
ArrayList<Integer> forced = new ArrayList<>();
for (int i = 0; i < m; i++) {
int x = c[i];
if (map.containsKey(x)) {
ArrayList<Integer> aa = map.get(x);
// ArrayList<Integer> ass = new ArrayList<>();
//
// ass.add(aa.get(0));
if (map1.containsKey(x)) {
ArrayList<Integer> aft = map1.get(x);
aft.add(aa.get(0));
map1.put(x, aft);
} else {
ArrayList<Integer> ind = new ArrayList<>();
ind.add(aa.get(0));
map1.put(x, ind);
}
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
map.remove(x);
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
if (map1.containsKey(x)) {
// out.print("--" + map1.get(x).size());
ArrayList<Integer> aa = map1.get(x);
for (int j = 0; j < forced.size(); j++) {
ans.add(aa.get(0));
}
forced.clear();
if (aa.size() == 1) {
ans.add(aa.get(0));
} else {
if (aa.size() != 0) {
ans.add(aa.get(0));
aa.remove(0);
}
}
} else {
forced.add(x);
}
}
}
// out.println(map.keySet());
// out.println(map1.keySet());
// out.println(forced.size());
for (int i : forced) {
if (map1.containsKey(i) == false) {
out.println("NO");
return;
}
}
if (map.size() == 0) {
out.println("YES");
for (int i : ans) {
out.print((i + 1) + " ");
}
out.println();
} else {
out.println("NO");
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int prime[] = new int[1000005];
static int N = 1000005;
static void sieve() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i <= N; i++) {
if (prime[i] == 0) {
for (int j = i * i; j < N; j += i) {
prime[j] = 1;
}
}
}
}
/*
* 5 16 9 8 7 7 1
*/
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (Integer i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (Long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static int mod = (int) 1e9 + 7;
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class cell {
long f;
int s;
cell(long f, int s) {
this.f = f;
this.s = s;
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 09f288babb059cd867d925fc7b6e0be2 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static void calc(int n){
int i,j;
dp[n-1]=0;
if(n>1)
dp[n-2]=1;
for(i=n-3;i>=0;i--){
long ind=n-i-1;
dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[];
static int visited[][];
static ArrayList<Integer> adj[];
//static int dp[][][];
static int R,G,B,MOD=1000000007;
static int[] par,size;
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static char ch[][], ch1[][];
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0;
t=in.nextInt();
//t=1;
while(t-->0){
tno++;
int n,m,flag=0;
n=in.nextInt();
m=in.nextInt();
int arr[]=new int[n];
int brr[]=new int[n];
int crr[]=new int[m];
//int freq1[]=new int[100002];
// int freq1[]=new int[100002];
HashMap<Integer,Integer> hm=new HashMap<>();
//HashMap<Integer,Integer> bm=new HashMap<>();
HashMap<Integer,LinkedList<Integer>> cm=new HashMap<>();
for(i=0;i<n;i++){
arr[i]=in.nextInt();
}
for(i=0;i<n;i++){
brr[i]=in.nextInt();
//bm.put(brr[i],bm.getOrDefault(brr[i],0)+1);
if(brr[i]!=arr[i]){
//freq1[brr[i]]++;
hm.put(brr[i],hm.getOrDefault(brr[i],0)+1);
if(cm.containsKey(brr[i])){
LinkedList<Integer> lin=cm.get(brr[i]);
lin.add(i);
cm.put(brr[i],lin);
}else{
LinkedList<Integer> lin=new LinkedList<>();
lin.add(i);
cm.put(brr[i],lin);
}
}
}
for(i=0;i<m;i++){
crr[i]=in.nextInt();
if(hm.containsKey(crr[i])){
hm.put(crr[i],hm.get(crr[i])-1);
if(hm.get(crr[i])==0){
hm.remove(crr[i]);
}
}
}
// if(tno==19){
// String s="_";
// // s+=n+"_";
// // s+=m+"_";
// // for(i=0;i<n;i++){
// // s+=arr[i]+"_";
// // }
// // for(i=0;i<n;i++){
// // s+=brr[i]+"_";
// // }
// for(i=0;i<m;i++){
// s+=crr[i]+"_";
// }
// w.println(s);
// }
int ind=0;
for(i=n-1;i>=0;i--){
if(brr[i]==crr[m-1]){
ind=i;
break;
}
}
if(i<0){
w.println("NO");
continue;
}
if(!hm.isEmpty()){
w.println("NO");
continue;
}
if(cm.containsKey(crr[m-1]))
ind=cm.get(crr[m-1]).getLast();
LinkedList<Integer> li=new LinkedList<>();
for(i=0;i<m;i++){
if(cm.containsKey(crr[i])){
LinkedList<Integer> lin=cm.get(crr[i]);
li.add(lin.pollFirst());
if(lin.isEmpty()){
cm.remove(crr[i]);
}else{
cm.put(crr[i],lin);
}
}else{
li.add(ind);
}
}
w.println("YES");
while(!li.isEmpty()){
w.print(li.pollFirst()+1+" ");
}
w.println();
}
w.close();
}
}
// 21 22
// 15 17 10 20 3 9 6 20 6 12 19 8 13 17 8 14 19 19 9 1 4
// 7 1 10 20 3 9 6 20 11 12 19 8 13 17 8 19 4 19 9 1 9
// 7 17 7 13 11 21 16 8 19 11 18 9 16 14 8 19 16 7 4 21 21 1 | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 603a6dd60a1bf49e81da8f5cea0e126a | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.*;
public class Main extends PrintWriter {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main () { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); }
void main() throws IOException {
StringBuilder sb = new StringBuilder();
int t=1;
t=i(s()[0]);
while(t-->0) {
String[] s1=s();int n=i(s1[0]);int m=i(s1[1]);
int[] a=new int[n];
int[] b=new int[n];int[] c=new int[m];arri(a,n);arri(b,n);arri(c,m);
int[] same=new int[n+1];Arrays.fill(same,-1);
Deque<Integer>[] diff=new LinkedList[n+1];
for(int i=0;i<=n;i++) diff[i]=new LinkedList<>();
for(int i=0;i<n;i++){
if(b[i]==a[i]){
same[b[i]]=i;
}else{
diff[b[i]].add(i);
}
}int flag=0;int[] ans=new int[m];
int last=-1;int lastval=0;
for(int i=m-1;i>=0;i--){
if(same[c[i]]!=-1||diff[c[i]].isEmpty()==false){
last=i;
if(diff[c[i]].isEmpty()==false) lastval=diff[c[i]].peekLast();else lastval=same[c[i]];
break;
}
}if(last<m-1){
flag=1;
}else{
for(int i=0;i<m;i++){
if(diff[c[i]].isEmpty()){
if(same[c[i]]==-1) {
ans[i]=lastval+1;
}else ans[i]=same[c[i]]+1;
}else{
ans[i]=diff[c[i]].pollFirst()+1;same[c[i]]=ans[i]-1;
}
}
}
for(int i=0;i<=n;i++){
if(diff[i].isEmpty()==false){
flag=1;break;
}
}
if(flag==0){
sb.append("YES\n");for(int i:ans) sb.append(i+" ");sb.append("\n");
}else sb.append("NO\n");
}
System.out.println(sb);
}
static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); }
static int i(String ss) {return Integer.parseInt(ss); }
static long l(String ss) {return Long.parseLong(ss); }
public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }}
public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=i(s2[i]); }}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 2346d9495107bb5cbbbe7ba79a717ec9 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String args[]) throws Exception
{
BufferedReader Rb = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.valueOf(Rb.readLine());
int counter = 0;
while(counter++<count)
{
String temp[] = Rb.readLine().split(" "), temp1[];
int n = Integer.valueOf(temp[0]);
int m = Integer.valueOf(temp[1]);
temp = Rb.readLine().split(" ");
temp1 = Rb.readLine().split(" ");
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
HashMap<Integer, Integer> anchor = new HashMap<Integer, Integer>();
for(int i = 0; i < n; i++)
{
int a = Integer.valueOf(temp[i]);
int b = Integer.valueOf(temp1[i]);
if(!anchor.containsKey(a) && a == b)
{anchor.put(a, i);}
else if(a == b)
{anchor.replace(a, i);}
if(a != b)
{
ArrayList<Integer> current = new ArrayList<Integer>();
if(hm.containsKey(b))
{current = hm.get(b);}
else
{hm.put(b, current);}
current.add(i);
}
}
temp = Rb.readLine().split(" ");
int[] output = new int[m];
int last = 0, none_last = 0, last_el = 0;
for(int i = 0; i < m; i++)
{
int color = Integer.valueOf(temp[i]);
if(hm.containsKey(color) && hm.get(color).size() > 0)
{
output[i] = hm.get(color).get(0);
hm.get(color).remove(0);
if(!anchor.containsKey(color))
{anchor.put(color, output[i]);}
else
{anchor.replace(color, output[i]);}
last = i;
last_el = output[i];
}
else if(anchor.containsKey(color))
{
last = i;
output[i] = anchor.get(color);
last_el = output[i];
}
else
{output[i] = -1; none_last = i;}
}
boolean flag = true;
for(ArrayList<Integer> i : hm.values())
{if(i.size() != 0){flag = false; break;}}
if(!flag || none_last > last)
{System.out.println("NO");}
else
{
System.out.println("YES");
for(int i = 0; i < m; i++)
{
if(output[i] == -1)
{System.out.print(last_el + 1 + " ");}
else
{System.out.print(output[i] + 1 + " ");}
}
System.out.println("");
}
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | d52999fb5c0c06a14dbf3008aafa3e31 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
/*
JAI MATA DI
*/
public class CP {
static class FR{
BufferedReader br;
StringTokenizer st;
public FR() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static int mod = 1000000007;
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] prime(int num) {
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
return bool;
}
static class Pair implements Comparable<Pair>{
int first;
int second;
Pair(int key , int value){
this.first = key;
this.second = value;
}
@Override
public int compareTo(Pair o) {
if(this.second == o.second) {
return this.first - o.first;
}
return -(this.second - o.second);
}
@Override
public int hashCode(){
return first + second;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-- > 0) {
int n = sc.nextInt() , k =sc.nextInt();
int[] ip = new int[n+1];
int[] fp = new int[n+1];
for(int i = 1 ; i<n+1 ; i++) ip[i] = sc.nextInt();
for(int i = 1 ; i<n+1 ; i++) fp[i] = sc.nextInt();
int[] painter = new int[k+1];
for(int i = 1 ; i<=k ; i++) painter[i] = sc.nextInt();
// paint , count
Map<Integer , LinkedList<Integer>> map1 = new HashMap<>();
Map<Integer , Integer> map2 = new HashMap<Integer, Integer>();
for(int i = 1; i<=n ;i++) {
if(ip[i] == fp[i]) {
map2.put(ip[i] , i);
}else {
if(map1.containsKey(fp[i])) map1.get(fp[i]).add(i);
else {
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.add(i);
map1.put(fp[i], ll);
}
}
}
int prv = -1;
int[] ans = new int[k+1];
boolean flag = true;
for(int i = k ; i>=1 ; i--) {
int pp = painter[i];
if(map1.containsKey(pp)) {
prv = map1.get(pp).removeFirst();
ans[i] = prv;
map2.put(pp, prv);
if(map1.get(pp).isEmpty()) {
map1.remove(pp);
}
}else if(map2.containsKey(pp)) {
prv = map2.get(pp);
ans[i] = prv;
}else {
if(prv == -1) {
flag = false;
break;
}
ans[i] = prv;
}
}
if(!flag || !map1.isEmpty()) {
sb.append("NO").append("\n");
}else {
sb.append("YES\n");
for(int i = 1 ; i<=k ;i++) {
sb.append(ans[i]+" ");
}sb.append("\n");
}
}
System.out.println(sb);
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 22b1dd137e608033a981d2076457a363 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
// U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................
//JUst keep faith in ur strengths ..................................................
// ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219
// odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4
// FOR ANY ODD NO N : N,N-1,N-2
//ALL ARE PAIRWISE COPRIME
//THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS
// two consecutive odds are always coprime to each other
// two consecutive even have always gcd = 2 ;
// Rectangle r = new Rectangle(int x , int y,int widht,int height)
//Creates a rect. with bottom left cordinates as (x, y) and top right as ((x+width),(y+height))
//BY DEFAULT Priority Queue is MIN in nature in java
//to use as max , just push with negative sign and change sign after removal
// We can make a sieve of max size 1e7 .(no time or space issue)
// In 1e7 starting nos we have about 66*1e4 prime nos
// In 1e6 starting nos we have about 78,498 prime nos
public class Main
{
// static int[] arr = new int[100002] ;
// static int[] dp = new int[100002] ;
static PrintWriter out;
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(System.out);
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st= new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str=br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return str;
}
}
////////////////////////////////////////////////////////////////////////////////////
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
/////////////////////////////////////////////////////////////////////////////////////////
public static int sumOfDigits(long n)
{
if( n< 0)return -1 ;
int sum = 0;
while( n > 0)
{
sum = sum + (int)( n %10) ;
n /= 10 ;
}
return sum ;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
public static long arraySum(int[] arr , int start , int end)
{
long ans = 0 ;
for(int i = start ; i <= end ; i++)ans += arr[i] ;
return ans ;
}
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public static void swapArray(int[] arr , int start , int end)
{
while(start < end)
{
int temp = arr[start] ;
arr[start] = arr[end];
arr[end] = temp;
start++ ;end-- ;
}
}
//////////////////////////////////////////////////////////////////////////////////
static long factorial(long a)
{
if(a== 0L || a==1L)return 1L ;
return a*factorial(a-1L) ;
}
///////////////////////////////////////////////////////////////////////////////
public static int[][] rotate(int[][] input){
int n =input.length;
int m = input[0].length ;
int[][] output = new int [m][n];
for (int i=0; i<n; i++)
for (int j=0;j<m; j++)
output [j][n-1-i] = input[i][j];
return output;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// ////////////////////////////////////////////////
public static boolean isPowerOfTwo(long n)
{
if(n==0)
return false;
if(((n ) & (n-1)) == 0 ) return true ;
else return false ;
}
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
public static String reverse(String input)
{
StringBuilder str = new StringBuilder("") ;
for(int i =input.length()-1 ; i >= 0 ; i-- )
{
str.append(input.charAt(i));
}
return str.toString() ;
}
///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean isPossibleTriangle(int a ,int b , int c)
{
if( a + b > c && c+b > a && a +c > b)return true ;
else return false ;
}
////////////////////////////////////////////////////////////////////////////////////////////
static long xnor(long num1, long num2) {
if (num1 < num2) {
long temp = num1;
num1 = num2;
num2 = temp;
}
num1 = togglebit(num1);
return num1 ^ num2;
}
static long togglebit(long n) {
if (n == 0)
return 1;
long i = n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return i ^ n;
}
///////////////////////////////////////////////////////////////////////////////////////////////
public static int xorOfFirstN(int n)
{
if( n % 4 ==0)return n ;
else if( n % 4 == 1)return 1 ;
else if( n % 4 == 2)return n+1 ;
else return 0 ;
}
//////////////////////////////////////////////////////////////////////////////////////////////
public static int gcd(int a, int b )
{
if(b==0)return a ;
else return gcd(b,a%b) ;
}
public static long gcd(long a, long b )
{
if(b==0)return a ;
else return gcd(b,a%b) ;
}
////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a, int b ,int c , int d )
{
int temp = lcm(a,b , c) ;
int ans = lcm(temp ,d ) ;
return ans ;
}
///////////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a, int b ,int c )
{
int temp = lcm(a,b) ;
int ans = lcm(temp ,c) ;
return ans ;
}
////////////////////////////////////////////////////////////////////////////////////////
public static int lcm(int a , int b )
{
int gc = gcd(a,b);
return (a/gc)*b ;
}
public static long lcm(long a , long b )
{
long gc = gcd(a,b);
return (a/gc)*b;
}
///////////////////////////////////////////////////////////////////////////////////////////
static boolean isPrime(long n)
{
if(n==1)
{
return false ;
}
boolean ans = true ;
for(long i = 2L; i*i <= n ;i++)
{
if(n% i ==0)
{
ans = false ;break ;
}
}
return ans ;
}
static boolean isPrime(int n)
{
if(n==1)
{
return false ;
}
boolean ans = true ;
for(int i = 2; i*i <= n ;i++)
{
if(n% i ==0)
{
ans = false ;break ;
}
}
return ans ;
}
///////////////////////////////////////////////////////////////////////////
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
public static void sieveOfEratosthenes()
{
// FALSE == prime
// TRUE == COMPOSITE
// FALSE== 1
// time complexity = 0(NlogLogN)== o(N)
// gives prime nos bw 1 to N
for(int i = 4; i<= sieve ; i++)
{
prime[i] = true ;
i++ ;
}
for(int p = 3; p*p <= sieve; p++)
{
if(prime[p] == false)
{
for(int i = p*p; i <= sieve; i += p)
prime[i] = true;
}
p++ ;
}
}
///////////////////////////////////////////////////////////////////////////////////
public static void sortD(int[] arr , int s , int e)
{
sort(arr ,s , e) ;
int i =s ; int j = e ;
while( i < j)
{
int temp = arr[i] ;
arr[i] =arr[j] ;
arr[j] = temp ;
i++ ; j-- ;
}
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////
public static long countSubarraysSumToK(long[] arr ,long sum )
{
HashMap<Long,Long> map = new HashMap<>() ;
int n = arr.length ;
long prefixsum = 0 ;
long count = 0L ;
for(int i = 0; i < n ; i++)
{
prefixsum = prefixsum + arr[i] ;
if(sum == prefixsum)count = count+1 ;
if(map.containsKey(prefixsum -sum))
{
count = count + map.get(prefixsum -sum) ;
}
if(map.containsKey(prefixsum ))
{
map.put(prefixsum , map.get(prefixsum) +1 );
}
else{
map.put(prefixsum , 1L );
}
}
return count ;
}
///////////////////////////////////////////////////////////////////////////////////////////////
// KMP ALGORITHM : TIME COMPL:O(N+M)
// FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING
//RETURN THE ARRAYLIST OF INDEXES
// IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING
public static ArrayList<Integer> kmpAlgorithm(String str , String pat)
{
ArrayList<Integer> list =new ArrayList<>();
int n = str.length() ;
int m = pat.length() ;
String q = pat + "#" + str ;
int[] lps =new int[n+m+1] ;
longestPefixSuffix(lps, q,(n+m+1)) ;
for(int i =m+1 ; i < (n+m+1) ; i++ )
{
if(lps[i] == m)
{
list.add(i-2*m) ;
}
}
return list ;
}
public static void longestPefixSuffix(int[] lps ,String str , int n)
{
lps[0] = 0 ;
for(int i = 1 ; i<= n-1; i++)
{
int l = lps[i-1] ;
while( l > 0 && str.charAt(i) != str.charAt(l))
{
l = lps[l-1] ;
}
if(str.charAt(i) == str.charAt(l))
{
l++ ;
}
lps[i] = l ;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n
// TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1
// or n and the no will be coprime in nature
//time : O(n*(log(logn)))
public static void eulerTotientFunction(int[] arr ,int n )
{
for(int i = 1; i <= n ;i++)arr[i] =i ;
for(int i= 2 ; i<= n ;i++)
{
if(arr[i] == i)
{
arr[i] =i-1 ;
for(int j =2*i ; j<= n ; j+= i )
{
arr[j] = (arr[j]*(i-1))/i ;
}
}
}
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
///////////////////////////////////////////////////////////////////////////////////////////
public static ArrayList<Integer> allFactors(int n)
{
ArrayList<Integer> list = new ArrayList<>() ;
for(int i = 1; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n)
{
list.add(i) ;
}
else{
list.add(i) ;
list.add(n/i) ;
}
}
}
return list ;
}
public static ArrayList<Long> allFactors(long n)
{
ArrayList<Long> list = new ArrayList<>() ;
for(long i = 1L; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n)
{
list.add(i) ;
}
else{
list.add(i) ;
list.add(n/i) ;
}
}
}
return list ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
static final int MAXN = 1000001;
static int spf[] = new int[MAXN];
static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
static ArrayList<Integer> getPrimeFactorization(int x)
{
ArrayList<Integer> ret = new ArrayList<Integer>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
public static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
public static long knapsack(int[] weight,long value[],int maxWeight){
int n= value.length ;
//dp[i] stores the profit with KnapSack capacity "i"
long []dp = new long[maxWeight+1];
//initially profit with 0 to W KnapSack capacity is 0
Arrays.fill(dp, 0);
// iterate through all items
for(int i=0; i < n; i++)
//traverse dp array from right to left
for(int j = maxWeight; j >= weight[i]; j--)
dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]);
/*above line finds out maximum of dp[j](excluding ith element value)
and val[i] + dp[j-wt[i]] (including ith element value and the
profit with "KnapSack capacity - ith element weight") */
return dp[maxWeight];
}
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// to return max sum of any subarray in given array
public static long kadanesAlgorithm(long[] arr)
{
if(arr.length == 0)return 0 ;
long[] dp = new long[arr.length] ;
dp[0] = arr[0] ;
long max = dp[0] ;
for(int i = 1; i < arr.length ; i++)
{
if(dp[i-1] > 0)
{
dp[i] = dp[i-1] + arr[i] ;
}
else{
dp[i] = arr[i] ;
}
if(dp[i] > max)max = dp[i] ;
}
return max ;
}
/////////////////////////////////////////////////////////////////////////////////////////////
public static long kadanesAlgorithm(int[] arr)
{
if(arr.length == 0)return 0 ;
long[] dp = new long[arr.length] ;
dp[0] = arr[0] ;
long max = dp[0] ;
for(int i = 1; i < arr.length ; i++)
{
if(dp[i-1] > 0)
{
dp[i] = dp[i-1] + arr[i] ;
}
else{
dp[i] = arr[i] ;
}
if(dp[i] > max)max = dp[i] ;
}
return max ;
}
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING
// JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive
//Function for swapping the characters at position I with character at position j
public static String swapString(String a, int i, int j) {
char[] b =a.toCharArray();
char ch;
ch = b[i];
b[i] = b[j];
b[j] = ch;
return String.valueOf(b);
}
//Function for generating different permutations of the string
public static void generatePermutation(String str, int start, int end)
{
//Prints the permutations
if (start == end-1)
System.out.println(str);
else
{
for (int i = start; i < end; i++)
{
//Swapping the string by fixing a character
str = swapString(str,start,i);
//Recursively calling function generatePermutation() for rest of the characters
generatePermutation(str,start+1,end);
//Backtracking and swapping the characters again.
str = swapString(str,start,i);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
public static long factMod(long n, long mod) {
if (n <= 1) return 1;
long ans = 1;
for (int i = 1; i <= n; i++) {
ans = (ans * i) % mod;
}
return ans;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
public static long power(int a ,int b)
{
//time comp : o(logn)
long x = (long)(a) ;
long n = (long)(b) ;
if(n==0)return 1 ;
if(n==1)return x;
long ans =1L ;
while(n>0)
{
if(n % 2 ==1)
{
ans = ans *x ;
}
n = n/2L ;
x = x*x ;
}
return ans ;
}
public static long power(long a ,long b)
{
//time comp : o(logn)
long x = (a) ;
long n = (b) ;
if(n==0)return 1L ;
if(n==1)return x;
long ans =1L ;
while(n>0)
{
if(n % 2 ==1)
{
ans = ans *x ;
}
n = n/2L ;
x = x*x ;
}
return ans ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
public static long powerMod(long x, long n, long mod) {
//time comp : o(logn)
if(n==0)return 1L ;
if(n==1)return x;
long ans = 1;
while (n > 0) {
if (n % 2 == 1) ans = (ans * x) % mod;
x = (x * x) % mod;
n /= 2;
}
return ans;
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/*
lowerBound - finds largest element equal or less than value paased
upperBound - finds smallest element equal or more than value passed
if not present return -1;
*/
public static long lowerBound(long[] arr,long k)
{
long ans=-1;
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=(start+end)/2;
if(arr[mid]<=k)
{
ans=arr[mid];
start=mid+1;
}
else
{
end=mid-1;
}
}
return ans;
}
public static int lowerBound(int[] arr,int k)
{
int ans=-1;
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=(start+end)/2;
if(arr[mid]<=k)
{
ans=arr[mid];
start=mid+1;
}
else
{
end=mid-1;
}
}
return ans;
}
public static long upperBound(long[] arr,long k)
{
long ans=-1;
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=(start+end)/2;
if(arr[mid]>=k)
{
ans=arr[mid];
end=mid-1;
}
else
{
start=mid+1;
}
}
return ans;
}
public static int upperBound(int[] arr,int k)
{
int ans=-1;
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=(start+end)/2;
if(arr[mid]>=k)
{
ans=arr[mid];
end=mid-1;
}
else
{
start=mid+1;
}
}
return ans;
}
//////////////////////////////////////////////////////////////////////////////////////////
public static void printArray(int[] arr , int si ,int ei)
{
for(int i = si ; i <= ei ; i++)
{
out.print(arr[i] +" ") ;
}
}
public static void printArrayln(int[] arr , int si ,int ei)
{
for(int i = si ; i <= ei ; i++)
{
out.print(arr[i] +" ") ;
}
out.println() ;
}
public static void printLArray(long[] arr , int si , int ei)
{
for(int i = si ; i <= ei ; i++)
{
out.print(arr[i] +" ") ;
}
}
public static void printLArrayln(long[] arr , int si , int ei)
{
for(int i = si ; i <= ei ; i++)
{
out.print(arr[i] +" ") ;
}
out.println() ;
}
public static void printtwodArray(int[][] ans)
{
for(int i = 0; i< ans.length ; i++)
{
for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" ");
out.println() ;
}
out.println() ;
}
static long modPow(long a, long x, long p) {
//calculates a^x mod p in logarithmic time.
a = a % p ;
if(a == 0)return 0L ;
long res = 1L;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x =x/2;
}
return res;
}
static long modInverse(long a, long p) {
//calculates the modular multiplicative of a mod p.
//(assuming p is prime).
return modPow(a, p-2, p);
}
static long[] factorial = new long[1000001] ;
static void modfac(long mod)
{
factorial[0]=1L ; factorial[1]=1L ;
for(int i = 2; i<= 1000000 ;i++)
{
factorial[i] = factorial[i-1] *(long)(i) ;
factorial[i] = factorial[i] % mod ;
}
}
static long modBinomial(long n, long r, long p) {
// calculates C(n,r) mod p (assuming p is prime).
if(n < r) return 0L ;
long num = factorial[(int)(n)] ;
long den = (factorial[(int)(r)]*factorial[(int)(n-r)]) % p ;
long ans = num*(modInverse(den,p)) ;
ans = ans % p ;
return ans ;
}
static void update(int val , long[] bit ,int n)
{
for( ; val <= n ; val += (val &(-val)) )
{
bit[val]++ ;
}
}
static long query(int val , long[] bit , int n)
{
long ans = 0L;
for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val];
return ans ;
}
static int countSetBits(long n)
{
int count = 0;
while (n > 0) {
n = (n) & (n - 1L);
count++;
}
return count;
}
static int abs(int x)
{
if(x < 0)x = -1*x ;
return x ;
}
static long abs(long x)
{
if(x < 0)x = -1L*x ;
return x ;
}
////////////////////////////////////////////////////////////////////////////////////////////////
static void p(int val)
{
out.print(val) ;
}
static void p()
{
out.print(" ") ;
}
static void pln(int val)
{
out.println(val) ;
}
static void pln()
{
out.println() ;
}
static void p(long val)
{
out.print(val) ;
}
static void pln(long val)
{
out.println(val) ;
}
static void yes()
{
out.println("YES") ;
}
static void no()
{
out.println("NO") ;
}
////////////////////////////////////////////////////////////////////////////////////////////
// calculate total no of nos greater than or equal to key in sorted array arr
static int bs(int[] arr, int s ,int e ,int key)
{
if( s> e)return 0 ;
int mid = (s+e)/2 ;
if(arr[mid] <key)
{
return bs(arr ,mid+1,e , key) ;
}
else{
return bs(arr ,s ,mid-1, key) + e-mid+1;
}
}
// static ArrayList<Integer>[] adj ;
// static int mod= 1000000007 ;
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
public static void solve()
{
FastReader scn = new FastReader() ;
//Scanner scn = new Scanner(System.in);
//int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ;
// product of first 11 prime nos is greater than 10 ^ 12;
//sieve() ;
//ArrayList<Integer> arr[] = new ArrayList[n] ;
ArrayList<Integer> list = new ArrayList<>() ;
ArrayList<Long> lista = new ArrayList<>() ;
ArrayList<Long> listb = new ArrayList<>() ;
// ArrayList<Integer> lista = new ArrayList<>() ;
// ArrayList<Integer> listb = new ArrayList<>() ;
//ArrayList<String> lists = new ArrayList<>() ;
HashMap<Integer,Integer> map = new HashMap<>() ;
//HashMap<Long,Long> map = new HashMap<>() ;
HashMap<Integer,Integer> mapx = new HashMap<>() ;
HashMap<Integer,Integer> mapy = new HashMap<>() ;
//HashMap<String,Integer> maps = new HashMap<>() ;
//HashMap<Integer,Boolean> mapb = new HashMap<>() ;
//HashMap<Point,Integer> point = new HashMap<>() ;
Set<Integer> set = new HashSet<>() ;
Set<Integer> setx = new HashSet<>() ;
Set<Integer> sety = new HashSet<>() ;
StringBuilder sb =new StringBuilder("") ;
//Collections.sort(list);
//if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ;
//else map.put(arr[i],1) ;
// if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ;
// else map.put(temp,1) ;
//int bit =Integer.bitCount(n);
// gives total no of set bits in n;
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override
// public int compare(Pair a, Pair b) {
// if (a.first != b.first) {
// return a.first - b.first; // for increasing order of first
// }
// return a.second - b.second ; //if first is same then sort on second basis
// }
// });
int testcase = 1;
testcase = scn.nextInt() ;
for(int testcases =1 ; testcases <= testcase ;testcases++)
{
//if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ;
//if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ;
int n= scn.nextInt() ;int m = scn.nextInt() ;
Queue<Integer> adj[] = new LinkedList[n+1] ;
for(int i = 0; i<= n; i++)
{
adj[i] = new LinkedList<Integer>();
}
int[] a= new int[n] ;int[] b= new int[n] ;int[] c= new int[m] ;
for(int i=0; i < n;i++)
{
a[i]= scn.nextInt();
}
for(int i=0; i < n;i++)
{
b[i]= scn.nextInt();
}
for(int i=0; i < m;i++)
{
c[i]= scn.nextInt();
}
int f= 0 ;int[]ans = new int[m] ;
for(int i=0; i < n;i++)
{
if((a[i]!= b[i]) && (b[i] ==c[m-1]) )
{
ans[m-1] = i ;f=1 ;break ;
}
}
for(int i=0; i < n&& f== 0 ;i++)
{
if((a[i] ==c[m-1]) && (b[i]==c[m-1]) )
{
ans[m-1] = i ;f=1 ;break ;
}
}
if(f== 0)
{
out.println("NO") ;
}
else{
for(int i=0; i < n;i++)
{
if((b[i] != a[i])&& (i != ans[m-1]))
{
adj[b[i]].add(i) ;
}
}
for(int i = 0; i <= m-2 ; i++){
int col = c[i] ;
if(adj[col].size() >0 )
{
int fr = adj[col].poll() ; ans[i] =fr ;
}
else{
ans[i] = ans[m-1] ;
}
a[ans[i]] = col ;
}
f= 1;
a[ans[m-1]] = c[m-1] ;
//printArrayln(a,0,n-1) ;printArrayln(b,0,n-1) ;
for(int i = 0 ; i < n; i++)
{
if(a[i] != b[i])
{
f= 0 ;break ;
}
}
if(f==0)out.println("NO") ;
else{
out.println("YES") ;
for(int i = 0;i < m ;i++ )
{
out.print((ans[i]+1) +" ") ;
}
out.println();
}
}
//out.println(ans) ;
//out.println(ans+" "+in) ;
//out.println("Case #" + testcases + ": " + ans ) ;
//out.println("@") ;
set.clear() ;
sb.delete(0 , sb.length()) ;
list.clear() ;lista.clear() ;listb.clear() ;
map.clear() ;
mapx.clear() ;
mapy.clear() ;
setx.clear() ;sety.clear() ;
} // test case end loop
out.flush() ;
} // solve fn ends
public static void main (String[] args) throws java.lang.Exception
{
solve() ;
}
}
class Pair
{
int first ;
int second ;
public Pair(int x, int y)
{
this.first = x ;this.second = y ;
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true ;
if(obj == null)return false ;
if(this.getClass() != obj.getClass())
{
return false ;
}
Pair other = (Pair)(obj) ;
if(this.first != other.first)return false ;
if(this.second != other.second)return false ;
return true ;
}
@Override
public int hashCode()
{
return this.first^this.second ;
}
@Override
public String toString() {
String ans = "" ;
ans += this.first ;
ans += " ";
ans += this.second ;
return ans ;
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | dbf724dbec3e12214c073cd24ec1d04d | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF_8_1
{
public static void main(String[] args)
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 1; i <= T; i++)
{
int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int n = arr[0];
int m = arr[1];
int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] b = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] c = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
solve(n,m,a,b,c);
}
} catch (Exception e) {
System.out.println(e);
}
}
private static void solve(int n,int m,int[] a,int[] b,int[] c)throws Exception
{
OutputStream out = new BufferedOutputStream ( System.out );
int req[] = new int[n+1];
int orig[] = new int[n+1];
int fin[] = new int[n+1];
int has[] = new int[n+1];
int pos[] = new int[n+1];
HashMap<Integer,TreeSet<Integer>> indices= new HashMap<>();
//Original array
for(int i=0;i<n;i++){
orig[a[i]]++;
}
//Final array
for(int i=0;i<n;i++){
fin[b[i]]++;
}
for(int i=0;i<n;i++){
if(pos[b[i]] == 0){
pos[b[i]] = i+1;
}
}
//required
for(int i=0;i<n;i++){
if(a[i] != b[i]){
req[b[i]]++;
}
}
//Available painters
for(int i=0;i<m;i++){
has[c[i]]++;
}
//number of colours to paint greater than painters with that skill
for(int i=0;i<n+1;i++){
if(req[i] > has[i]){
out.write(("NO\n").getBytes());out.flush(); return;}
}
for(int i=0;i<n;i++){
if(a[i] != b[i] && indices.containsKey(b[i])){
indices.get(b[i]).add(i+1);
}
else if(a[i] != b[i] ){
indices.put(b[i],new TreeSet<>(Arrays.asList(i+1)));
}
}
int cnt = 0;
for(int i=0;i<m;i++) {
if (req[c[i]] > 0 || fin[c[i]] > 0) {
cnt = 0;
}
else{
cnt++;
}
}
if(cnt == 0){
System.out.println("YES");
}
else{
System.out.println("NO");return;
}
for(int i=0;i<m;i++){
if(req[c[i]] > 0){
if(indices.get(c[i]).size() > 1)
{
for(int j=1;j<=cnt;j++){
out.write((indices.get(c[i]).first() + " ").getBytes());
}
out.write((indices.get(c[i]).pollFirst() + " ").getBytes());
}
else{
for(int j=1;j<=cnt+1;j++)
out.write((indices.get(c[i]).first() + " ").getBytes());
}
cnt=0;
}
else if(fin[c[i]] > 0 ){
for(int j=1;j<=cnt+1;j++) {
out.write((pos[c[i]] + " ").getBytes());
}
cnt = 0;
}
else{
cnt++;
}
}
out.write(("\n").getBytes());
out.flush();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | f10545da506959a508957611a692a5ce | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF_8_1
{
public static void main(String[] args)throws Exception
{
//try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 1; i <= T; i++)
{
//int N = Integer.parseInt(br.readLine());
int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int n = arr[0];
int m = arr[1];
int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] b = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] c = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
solve(n,m,a,b,c);
}
/*} catch (Exception e) {
System.out.println(e);
}*/
}
private static void solve(int n,int m,int[] a,int[] b,int[] c)throws Exception
{
OutputStream out = new BufferedOutputStream ( System.out );
OutputStream out1 = new BufferedOutputStream ( System.out );
int req[] = new int[n+1];
int orig[] = new int[n+1];
int fin[] = new int[n+1];
int has[] = new int[n+1];
int pos[] = new int[n+1];
HashMap<Integer,TreeSet<Integer>> indices= new HashMap<>();
//Original array
for(int i=0;i<n;i++){
orig[a[i]]++;
}
//Final array
for(int i=0;i<n;i++){
fin[b[i]]++;
}
for(int i=0;i<n;i++){
if(pos[b[i]] == 0){
pos[b[i]] = i+1;
}
}
//required
for(int i=0;i<n;i++){
if(a[i] != b[i]){
req[b[i]]++;
}
}
//last index of possible correction
int fin_index = 0;
for(int i=0;i<m;i++){
if(fin[c[i]] > 0){fin_index = i;}
}
//Available painters
for(int i=0;i<m;i++){
has[c[i]]++;
}
//number of colours to paint greater than painters with that skill
for(int i=0;i<n+1;i++){
if(req[i] > has[i]){
out.write(("NO\n").getBytes());out.flush(); return;}
}
for(int i=0;i<n;i++){
if(a[i] != b[i] && indices.containsKey(b[i])){
indices.get(b[i]).add(i+1);
}
else if(a[i] != b[i] ){
//System.out.println("Requirement : "+b[i]);
indices.put(b[i],new TreeSet<>(Arrays.asList(i+1)));
}
}
int cnt = 0;
for(int i=0;i<m;i++) {
if (req[c[i]] > 0) {
cnt = 0;
}
else if(fin[c[i]] > 0 ){
cnt=0;
}
else{
cnt++;
}
}
if(cnt == 0){
System.out.println("YES");
}
else{
System.out.println("NO");return;
}
for(int i=0;i<m;i++){
if(req[c[i]] > 0){
if(indices.get(c[i]).size() > 1)
{
//System.out.println("NOW : "+c[i]);
for(int j=1;j<=cnt;j++){
out.write((indices.get(c[i]).first() + " ").getBytes());
}
out.write((indices.get(c[i]).pollFirst() + " ").getBytes());
//System.out.println(indices.get(c[i]).pollFirst());
}
else{
//System.out.println("Hello ");
for(int j=1;j<=cnt+1;j++)
out.write((indices.get(c[i]).first() + " ").getBytes());
}
cnt=0;
}
else if(fin[c[i]] > 0 ){
for(int j=1;j<=cnt+1;j++) {
out.write((pos[c[i]] + " ").getBytes());
}
cnt = 0;
}
else{
cnt++;
}
}
//System.out.println("cnt : "+cnt);
/*if(cnt == 0){
/*out1.write(("YES\n").getBytes());
out1.flush();
System.out.println("YES");
if(n == 100000 && m==100000){System.out.println("HELLO");}*/
out.write(("\n").getBytes());
out.flush();
/*}
else{
out1.write(("NO\n").getBytes());
out1.flush();
}*/
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | cbc0c0b410c2dca1b2b539b631d84be3 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import com.sun.source.util.Trees;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static long MOD = (long) (1e9 + 7);
//static int MOD = 998244353;
static long MOD2 = MOD * MOD;
static FastReader sc = new FastReader();
static int pInf = Integer.MAX_VALUE;
static int nInf = Integer.MIN_VALUE;
static long ded = (long)(1e17)+9;
public static void main(String[] args) throws Exception {
int test = 1;
test = sc.nextInt();
for (int i = 1; i <= test; i++){
//out.print("Case #"+i+": ");
solve();
}
out.flush();
out.close();
}
static void solve(){
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
HashMap<Integer, TreeSet<Integer>> M = new HashMap<>();
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
if(M.containsKey(a[i])){
M.get(a[i]).add(i+1);
}else{
TreeSet<Integer> P = new TreeSet<>();
P.add(i+1);
M.put(a[i],P);
}
}
int count = 0;
HashMap<Integer,TreeSet<Integer>> A = new HashMap<>();
for(int i = 0; i < n; i++){
b[i] = sc.nextInt();
if(b[i]!=a[i]){
count++;
if(A.containsKey(b[i])){
A.get(b[i]).add(i+1);
}else{
TreeSet<Integer> T = new TreeSet<>();
T.add(i+1);
A.put(b[i],T);
}
}
}
for(int i = 0; i < m; i++){
c[i] = sc.nextInt();
}
int[] ans = new int[m];
Arrays.fill(ans,-1);
for(int i = 0; i < m; i++){
if(A.containsKey(c[i])&&!A.get(c[i]).isEmpty()){
count--;
ans[i] = A.get(c[i]).pollFirst();
int idx = ans[i];
M.get(a[idx-1]).remove(idx);
if(M.get(a[idx-1]).isEmpty()){
M.remove(a[idx-1]);
}
if(M.containsKey(c[i])){
M.get(c[i]).add(idx);
}else{
TreeSet<Integer> tt = new TreeSet<>();
tt.add(idx);
M.put(c[i],tt);
}
a[ans[i]-1] = c[i];
if(A.get(c[i]).isEmpty()){
A.remove(c[i]);
}
}else if(M.containsKey(c[i])){
ans[i] = M.get(c[i]).first();
}
}
for(int i = m-1; i >= 0; i--){
if(ans[i]==-1){
ans[i] = ans[m-1];
}
}
if(ans[m-1]==-1||count>0){
out.println("NO");
return;
}
for(int i = 0; i < n; i++){
if(a[i]!=b[i]){
out.println("NO");
return;
}
}
out.println("YES");
for(int i = 0; i < m; i++){
out.print(ans[i]+" ");
}
out.println();
}
static class Pair implements Comparable<Pair> {
int x;int y;int idx;
public Pair(int x, int y,int idx) {
this.x = x;
this.y = y;
this.idx = idx;
}
@Override
public int compareTo(Pair o) {
if(this.x==o.x){
return o.y-this.y;
}
return this.x-o.x;
}
}
static long nC2(long n) {
return add((n * (n + 1)) / 2, 0);
}
public static long mul(long a, long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
public static long add(long a, long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
public static long c2(long n) {
if ((n & 1) == 0) {
return mul(n / 2, n - 1);
} else {
return mul(n, (n - 1) / 2);
}
}
//Shuffle Sort
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n); int temp= a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
//Brian Kernighans Algorithm
static long countSetBits(long n) {
if (n == 0) return 0;
return 1 + countSetBits(n & (n - 1));
}
//Euclidean Algorithm
static long gcd(long A, long B) {
if (B == 0) return A;
return gcd(B, A % B);
}
//Modular Exponentiation
static long fastExpo(long x, long n) {
if (n == 0) return 1;
if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD;
return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD;
}
//AKS Algorithm
static boolean isPrime(long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i <= Math.sqrt(n); i += 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
public static long modinv(long x) {
return modpow(x, MOD - 2);
}
public static long modpow(long a, long b) {
if (b == 0) {
return 1;
}
long x = modpow(a, b / 2);
x = (x * x) % MOD;
if (b % 2 == 1) {
return (x * a) % MOD;
}
return x;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 8cf125d1b592cc956e65a07fca053898 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.PrintStream;
public class Solution {
public static boolean Local(){
try{
return System.getenv("LOCAL_SYS")!=null;
}catch(Exception e){
return false;
}
}
public static boolean LOCAL;
static class FastScanner {
BufferedReader br;
StringTokenizer st ;
FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
FastScanner(String file) {
try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
st = new StringTokenizer("");
}catch(FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
}
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
String readLine() throws IOException{
return br.readLine();
}
}
static class Pair<T,X> {
T first;
X second;
Pair(T first,X second){
this.first = first;
this.second = second;
}
@Override
public int hashCode(){
return Objects.hash(first,second);
}
@Override
public boolean equals(Object obj){
return obj.hashCode() == this.hashCode();
}
}
static PrintStream debug = null;
static long mod = (long)(Math.pow(10,9) + 7);
public static void main(String[] args) throws Exception {
FastScanner s = new FastScanner();
LOCAL = Local();
if(LOCAL){
s = new FastScanner("src/input.txt");
PrintStream o = new PrintStream("src/sampleout.txt");
debug = new PrintStream("src/debug.txt");
System.setOut(o);
}
long mod = 1000000007;
int tcr = s.nextInt();
StringBuilder sb = new StringBuilder();
for(int tc=0;tc<tcr;tc++){
int n = s.nextInt();
int m = s.nextInt();
int color[] = new int[n];
int to_color[] = new int[n];
int paint[] = new int[m];
int req[] = new int[n];
boolean poss = true;
Map<Integer,Integer> present = new HashMap<>();
for(int i=0;i<n;i++){
color[i] = s.nextInt();
color[i]--;
}
Set<Integer> needed = new HashSet<>();
int can_paint = -1;
Map<Integer,Queue<Integer>> map = new HashMap<>();
for(int i=0;i<n;i++){map.put(i,new LinkedList<>());}
for(int i=0;i<n;i++){
to_color[i] = s.nextInt();
to_color[i]--;
if(to_color[i] != color[i]){
map.get(to_color[i]).add(i);
}else{
present.put(color[i],i);
}
needed.add(to_color[i]);
}
for(int i=0;i<m;i++){
paint[i] = s.nextInt();
paint[i]--;
}
if(!needed.contains(paint[m-1])){
sb.append("NO\n");
continue;
}
can_paint = map.get(paint[m-1]).size()==0?present.get(paint[m-1]):map.get(paint[m-1]).poll();
// println("here==;"+can_paint);
int ans[] = new int[m];
for(int i=0;i<m-1;i++){
if(needed.contains(paint[i])){
if(map.get(paint[i]).size() == 0){
ans[i] = can_paint;
color[ans[i]] = paint[i];
}else{
ans[i] = map.get(paint[i]).poll();
color[ans[i]] = paint[i];
}
}else{
ans[i] = can_paint;
color[ans[i]] = paint[i];
}
}
ans[m-1] = can_paint;
color[can_paint] = paint[m-1];
// dbg(debug,color);
// dbg(debug,to_color);
for(int i=0;i<n;i++){
if(color[i] != to_color[i]){poss = false;break;}
}
if(!poss){
sb.append("NO\n");
}else{
sb.append("YES\n");
for(int i=0;i<m;i++){
sb.append((ans[i] + 1)+" ");
}
sb.append('\n');
}
}
print(sb.toString());
}
public static long solve(long arr[],int index,long dp[]){
if(dp[index] != -1l){
return dp[index];
}
long next_index = index + arr[index];
long ans = arr[index];
if(next_index < arr.length){
return dp[index] = ans + solve(arr,(int)(index + arr[index]),dp);
}
return dp[index] = ans;
}
public static boolean inRange(int r1,int r2,int val){
return ((val >= r1) && (val <= r2));
}
static int len(long num){
return Long.toString(num).length();
}
static long mulmod(long a, long b,long mod)
{
long ans = 0l;
while(b > 0){
long curr = (b & 1l);
if(curr == 1l){
ans = ((ans % mod) + a) % mod;
}
a = (a + a) % mod;
b = b >> 1;
}
return ans;
}
public static void dbg(PrintStream ps,Object... o) throws Exception{
if(ps == null){
return;
}
Debug.dbg(ps,o);
}
public static long modpow(long num,long pow,long mod){
long val = num;
long ans = 1l;
while(pow > 0l){
long bit = pow & 1l;
if(bit == 1){
ans = (ans * (val%mod))%mod;
}
val = (val * val) % mod;
pow = pow >> 1;
}
return ans;
}
public static char get(int n){
return (char)('a' + n);
}
public static long[] sort(long arr[]){
List<Long> list = new ArrayList<>();
for(long n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
public static int[] sort(int arr[]){
List<Integer> list = new ArrayList<>();
for(int n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
// return the (index + 1)
// where index is the pos of just smaller element
// i.e count of elemets strictly less than num
public static int justSmaller(long arr[],long num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
public static int justSmaller(int arr[],int num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
//return (index of just greater element)
//count of elements smaller than or equal to num
public static int justGreater(long arr[],long num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static int justGreater(int arr[],int num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static void println(Object obj){
System.out.println(obj.toString());
}
public static void print(Object obj){
System.out.print(obj.toString());
}
public static int gcd(int a,int b){
if(b == 0){return a;}
return gcd(b,a%b);
}
public static long gcd(long a,long b){
if(b == 0l){
return a;
}
return gcd(b,a%b);
}
public static int find(int parent[],int v){
if(parent[v] == v){
return v;
}
return parent[v] = find(parent, parent[v]);
}
public static List<Integer> sieve(){
List<Integer> prime = new ArrayList<>();
int arr[] = new int[100001];
Arrays.fill(arr,1);
arr[1] = 0;
arr[2] = 1;
for(int i=2;i<=100000;i++){
if(arr[i] == 1){
prime.add(i);
for(long j = (i*1l*i);j<100001;j+=i){
arr[(int)j] = 0;
}
}
}
return prime;
}
static boolean isPower(long n,long a){
long log = (long)(Math.log(n)/Math.log(a));
long power = (long)Math.pow(a,log);
if(power == n){return true;}
return false;
}
private static int mergeAndCount(int[] arr, int l,int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static int mergeSortAndCount(int[] arr, int l,int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
int count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static class Debug{
//change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
private static <T> String ts(T t) {
if(t==null) {
return "null";
}
try {
return ts((Iterable) t);
}catch(ClassCastException e) {
if(t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}
try {
return ts((Object[]) t);
}catch(ClassCastException e1) {
return t.toString();
}
}
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: arr) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: iter) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
public static void dbg(PrintStream ps,Object... o) throws Exception {
if(LOCAL) {
System.setErr(ps);
System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [");
for(int i = 0; i<o.length; i++) {
if(i!=0) {
System.err.print(", ");
}
System.err.print(ts(o[i]));
}
System.err.println("]");
}
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 2cf38c422d3c64f637105c670b0927b1 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class FencePainting {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));//new FileReader("cowdance.in")
PrintWriter out = new PrintWriter(System.out);//new FileWriter("cowdance.out")
StringTokenizer st = new StringTokenizer(read.readLine());
int t = Integer.parseInt(st.nextToken());
for (int ie = 0; ie < t; ie++) {
st = new StringTokenizer(read.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
ArrayList<ArrayList<Integer>> req = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<=n;i++) {
req.add(new ArrayList<Integer>());
}
int [] b = new int [n];
int [] arr = new int [m];
int [] a = new int [n];
int [] p = new int [m];
st = new StringTokenizer(read.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(read.readLine());
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(st.nextToken());
if(a[i] != b[i]){
req.get(b[i]).add(i);
}
}
st = new StringTokenizer(read.readLine());
for (int i = 0; i < m; i++) {
p[i] = Integer.parseInt(st.nextToken());
}
int lastR = -1;
if(req.get((int) p[m-1]).size()>0) {
lastR = req.get((int) p[m-1]).get(req.get((int) p[m-1]).size()-1);
req.get((int) p[m-1]).remove(req.get((int) p[m-1]).size()-1);
}else {
for (int i = 0; i < n; i++) {
if (b[i] == p[m-1]) {
lastR = i;
break;
}
}
}
arr[m-1] = lastR;
boolean work = true;
if (lastR == -1) {
work = false;
}
for(int i=0;i<m-1;i++) {
if(req.get(p[i]).size() == 0) {
arr[i]=lastR;
}else {
arr[i] = req.get(p[i]).get(req.get(p[i]).size()-1);
req.get(p[i]).remove(req.get(p[i]).size()-1);
}
}
for(int i=1;i<=n;i++) {
if(req.get(i).size()>0) {
work = false;
}
}
if (work) {
out.println("YES");
for (int i = 0; i < m; i++) {
out.print((arr[i]+1)+" ");
}
out.println();
}else {
out.println("NO");
}
}
out.close();
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | c3b2ca67420a1f1f6811ea5a06fa7bd6 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class VC699D2P3{
public static void main(String[] args){
FS sc = new FS();
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = sc.nextArray(n);
int[] b = sc.nextArray(n);
int[] c = sc.nextArray(m);
HashMap<Integer, ArrayList<Integer>> colors = new HashMap<>();
for(int i=0; i<n; i++){
if(b[i]!=a[i]){
if(!colors.containsKey(b[i]))
colors.put(b[i], new ArrayList<>());
colors.get(b[i]).add(i+1);
}
}
// finding the last painter color
// if it is useful for repainting
// we use it
// else we find an index which can be repainted on any
// un useful color painter occurrence
int neededCol = c[m-1];
int index = -1;
if(colors.containsKey(neededCol)){
index = colors.get(neededCol).get(0);
colors.get(neededCol).remove(0);
}
else{
for(int i=0; i<n; i++){
if(b[i]==neededCol){
index = i+1;
break;
}
}
}
if(index==-1){
pw.println("NO");
continue;
}
// here "index" of such plank is obtained
// which is a useful color plank
StringBuilder sb = new StringBuilder();
for(int i=0; i<m; i++){
if(i==m-1)
sb.append(index);
else if(!colors.containsKey(c[i]) || colors.get(c[i]).size()==0){
sb.append(index).append(" ");
}
else{
sb.append(colors.get(c[i]).get(0)).append(" ");
colors.get(c[i]).remove(0);
}
}
boolean imposs = false;
int prev = -1;
// painters not meeting the required no. of planks
for(Map.Entry<Integer, ArrayList<Integer>> entry : colors.entrySet()){
if(entry.getValue().size()>=1){
imposs = true;
break;
}
}
if(imposs) pw.println("NO");
else{
pw.println("YES");
pw.println(sb.toString());
}
}
pw.flush();
pw.close();
}
static class FS{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while(!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
} catch(Exception ignored){
}
}
return st.nextToken();
}
int[] nextArray(int n){
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n){
long[] a = new long[n];
for(int i = 0; i < n; i++){
a[i] = nextLong();
}
return a;
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | b40054feb8559a21c903bae71447968b | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | //package fencepainting;
import java.util.*;
import java.io.*;
public class fencepainting {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
StringTokenizer st = new StringTokenizer(fin.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
ArrayList<HashSet<Integer>> colorsNeeded = new ArrayList<HashSet<Integer>>(); //stores the colors that are needed, and their locations
ArrayList<HashSet<Integer>> targetColors = new ArrayList<HashSet<Integer>>(); //just a band aid
for(int i = 0; i < n; i++) {
colorsNeeded.add(new HashSet<Integer>());
targetColors.add(new HashSet<Integer>());
}
int[] curFence = new int[n];
int[] targetFence = new int[n];
int[] ans = new int[m];
st = new StringTokenizer(fin.readLine());
for(int i = 0; i < n; i++) {
curFence[i] = Integer.parseInt(st.nextToken()) - 1;
}
st = new StringTokenizer(fin.readLine());
for(int i = 0; i < n; i++) {
targetFence[i] = Integer.parseInt(st.nextToken()) - 1;
if(curFence[i] != targetFence[i]) {
colorsNeeded.get(targetFence[i]).add(i);
}
targetColors.get(targetFence[i]).add(i);
}
int[] painters = new int[m];
int finalLoc = 0; //saves the place the final painter is going to paint
st = new StringTokenizer(fin.readLine());
for(int i = 0; i < m; i++) {
painters[i] = Integer.parseInt(st.nextToken()) - 1;
}
boolean isValid = true;
if(targetColors.get(painters[m - 1]).size() == 0) {
//System.out.println("IS NOT VALID " + painters[m - 1]);
isValid = false;
}
else {
if(colorsNeeded.get(painters[m - 1]).size() != 0) {
finalLoc = colorsNeeded.get(painters[m - 1]).iterator().next();
colorsNeeded.get(painters[m - 1]).remove(finalLoc);
ans[m - 1] = finalLoc + 1;
}
else {
finalLoc = targetColors.get(painters[m - 1]).iterator().next();
ans[m - 1] = finalLoc + 1;
}
}
for(int i = 0; i < m - 1; i++) {
if(colorsNeeded.get(painters[i]).size() > 0) {
int next = colorsNeeded.get(painters[i]).iterator().next();
colorsNeeded.get(painters[i]).remove(next);
ans[i] = next + 1;
}
else {
ans[i] = finalLoc + 1;
}
}
//System.out.println(colorsNeeded);
for(int i = 0; i < n; i++) {
if(colorsNeeded.get(i).size() != 0) {
isValid = false;
break;
}
}
if(isValid) {
fout.append("YES\n");
for(int i = 0; i < m; i++) {
fout.append(ans[i]).append(" ");
}
fout.append("\n");
}
else {
fout.append("NO\n");
}
}
System.out.print(fout);
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 431f87cd080a3f1fad89b3a2891e5656 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader s = new FastReader();
int tc = s.nextInt();
for(int t = 0;t < tc;t++) {
int n = s.nextInt() , m = s.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0;i < n;i++) a[i] = s.nextInt();
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
for(int i = 0;i <= n;i++) {
arr.add(new ArrayList<>());
}
for(int i = 0;i < n;i++) {
b[i] = s.nextInt();
if(b[i] != a[i]) {
arr.get(b[i]).add(i);
}
}
int[] c = new int[m];
for(int i = 0;i < m;i++) c[i] = s.nextInt();
int index = -1;
if(arr.get(c[m - 1]).size() > 0) {
index = arr.get(c[m - 1]).get(arr.get(c[m - 1]).size() - 1);
arr.get(c[m - 1]).remove(arr.get(c[m - 1]).size() - 1);
}
else {
for(int i = 0;i < n;i++) {
if(b[i] == c[m - 1]) {
index = i;
break;
}
}
}
if(index == -1) {
System.out.println("NO");
continue;
}
int[] ans = new int[m];
ans[m - 1] = index;
for(int i = 0;i < m - 1;i++) {
if(arr.get(c[i]).size() == 0) {
ans[i] = index;
}
else {
ans[i] = arr.get(c[i]).get(arr.get(c[i]).size() - 1);
arr.get(c[i]).remove(arr.get(c[i]).size() - 1);
}
}
boolean possible = true;
for(int i = 0;i < arr.size();i++) {
if(arr.get(i).size() > 0) {
possible = false;
break;
}
}
if(!possible) {
System.out.println("NO");
continue;
}
System.out.println("YES");
for(int i : ans) {
System.out.print((i + 1) + " ");
}
System.out.println();
}
}
}
class Pair {
int val;
int index;
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | eb37c7f3dfceb296db20c0997ea8d3f1 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
public class C {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int cases = scn.nextInt();
while (cases-- > 0) {
int n = scn.nextInt();
int m = scn.nextInt();
int[] initial = new int[n];
int[] desired = new int[n];
int[] painters = new int[m];
int[] result = new int[m];
for (int i = 0; i < n; i++) initial[i] = scn.nextInt();
for (int i = 0; i < n; i++) desired[i] = scn.nextInt();
for (int i = 0; i < m; i++) painters[i] = scn.nextInt();
Map<Integer, Deque<Integer>> toChange = new HashMap<>();
int lastPainted = -1;
int lastColor = painters[m - 1];
for (int i = 0; i < n; i++) {
if (initial[i] != desired[i]) {
Deque<Integer> planks = new ArrayDeque<>();
if (!toChange.containsKey(desired[i]))
toChange.put(desired[i], planks);
planks = toChange.get(desired[i]);
planks.offer(i);
}
}
if (toChange.containsKey(lastColor)) {
lastPainted = toChange.get(lastColor).poll();
}
for (int i = 0; i < n && lastPainted == -1; i++) {
if (desired[i] == painters[m - 1]) {
lastPainted = i;
}
}
result[m - 1] = lastPainted + 1;
boolean ans = lastPainted != -1;
for (int i = 0; i < m - 1; i++) {
int color = painters[i];
Deque<Integer> planks = toChange.get(color);
if (planks != null && planks.size() > 0) {
result[i] = planks.poll() + 1;
} else {
result[i] = lastPainted + 1;
}
}
for (Deque<Integer> planks : toChange.values())
if (!planks.isEmpty()) {
ans = false;
break;
}
if (ans) {
System.out.println("YES");
for (int i : result) System.out.print(i + " ");
System.out.println();
} else System.out.println("NO");
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 733b6f813a03b73e14e07cd1a1a302fb | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class First {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n, m;
n = in.nextInt();
m = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
int[] ans = new int[m];
Map<Integer, LinkedList<Integer>> needed = new HashMap<>();
Map<Integer,Integer> finalPaint = new HashMap<>();
ArrayList<Integer> index = new ArrayList<>();
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < n; i++) {
b[i] = in.nextInt();
if(a[i]!=b[i]){
if(needed.getOrDefault(b[i],null)==null)
needed.put(b[i],new LinkedList<>());
needed.get(b[i]).add(i);
}
finalPaint.put(b[i],i);
}
for (int i = 0; i < m; i++) {
c[i] = in.nextInt();
if(finalPaint.getOrDefault(c[i],-1)!=-1){
index.add(i);
}
}
Arrays.fill(ans,-1);
int d= 0;
for (int i = 0; i < m; i++) {
if (ans[i] != -1) {
continue;
}
if(needed.getOrDefault(c[i],null)!=null && needed.get(c[i]).size()!=0){
ans[i] = needed.get(c[i]).removeFirst();
}
else{
if(needed.getOrDefault(c[i],null)==null && finalPaint.getOrDefault(c[i],-1)==-1){
while(d<index.size() && index.get(d)<i){
d++;
}
if(d>=index.size()){
out.println("NO");
return;
}
if(ans[index.get(d)]!=-1){
ans[i] = ans[index.get(d)];
}
else {
if (needed.getOrDefault(c[index.get(d)], null) != null && needed.get(c[index.get(d)]).size() != 0) {
ans[i] = needed.get(c[index.get(d)]).removeFirst();
} else {
ans[i] = finalPaint.get(c[index.get(d)]);
}
ans[index.get(d)] = ans[i];
}
}
else{
ans[i] = finalPaint.get(c[i]);
}
}
}
for (Integer i: needed.keySet()) {
if(needed.get(i).size()!=0){
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < m; i++) {
out.print(ans[i]+1 + " ");
}
out.println();
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a,b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return this.a - o.a;
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(answer1 o) {
return this.a - o.a;
}
}
static class arrayListClass {
ArrayList<Integer> arrayList2 ;
public arrayListClass(ArrayList<Integer> arrayList) {
this.arrayList2 = arrayList;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 54b0d825b22c6af1cea9fd89b2385de6 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class experiments
{
static int ans=0;
public static void main(String args[]) throws IOException
{
FastScanner fs = new FastScanner();
int T = fs.nextInt();
outer : while (T-- > 0) {
int n = fs.nextInt(),m = fs.nextInt();
int a[] = fs.arrayIn(n);
int b[] = fs.arrayIn(n);
int c[] = fs.arrayIn(m);
int pos[] = new int[m];
Arrays.fill(pos, -1);
int count =0;
for(int i=0; i<n; i++) {
if(b[i] == a[i]){ b[i]=-1;}
}
out : for(int i=0;i<n;i++) {
if(b[i] >0) {
for(int j=0; j<m;j++) {
if(c[j]==b[i]) {c[j]=-1; pos[j] = i+1; a[i] = b[i]; continue out;}
}
System.out.println("NO");
continue outer;
}
}
for(int i=0; i<n; i++) if(c[m-1] == a[i]) {c[m-1] = -1; pos[m-1]=i+1; break;}
if(c[m-1] != -1) System.out.println("NO");
else {
for(int i=0; i<m; i++){
if(pos[i] == -1) pos[i] = pos[m-1];
}
System.out.println("YES");
for(int i:pos) System.out.print(i+" ");
System.out.println();
}
}
}
static final Random random = new Random();
static void ruffleSort(int arr[])
{
int n = arr.length;
for(int i=0; i<n; i++)
{
int j = random.nextInt(n),temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static class Pairs implements Comparable<Pairs>
{
int value,index;
Pairs(int value, int index)
{
this.value = value;
this.index = index;
}
public int compareTo(Pairs p)
{
return Integer.compare(value,p.value);
}
}
}
class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer("");
String next() throws IOException
{
while(!str.hasMoreTokens())
str = new StringTokenizer(br.readLine());
return str.nextToken();
}
char nextChar() throws IOException {
return next().charAt(0);
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
float nextfloat() throws IOException
{
return Float.parseFloat(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
byte nextByte() throws IOException
{
return Byte.parseByte(next());
}
int [] arrayIn(int n) throws IOException
{
int arr[] = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = nextInt();
}
return arr;
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 488384db549545a1010dc55450cd4823 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
/*
** || ॐ श्री गणेशाय नमः ||
** @𝙖𝙪𝙩𝙝𝙤𝙧 𝙅𝙞𝙜𝙖𝙧_𝙉𝙖𝙞𝙣𝙪𝙟𝙞
** 𝙎𝙑𝙉𝙄𝙏-𝙎𝙐𝙍𝘼𝙏
*/
public static void main(String args[]){
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t=1;
t = in.nextInt();
for(int i=1;i<=t;i++)
{
solver.solve(in,i);
}
}
static class TASK {
static int mod = 1000000000+7;
void solve(InputReader in, int testNumber) {
int n=in.nextInt();
int m = in.nextInt();
int a[] = new int[n+1];
int b[] = new int[n+1];
int c[] = new int[n+1];
int coman[] = new int[n+1];
int initial[] = new int[n];
int last[] = new int[n];
int painter[] = new int[m];
int lastt=0;
// PriorityQueue<Integer> pq1[] = new PriorityQueue[n+1];
PriorityQueue<Integer> pq2[] = new PriorityQueue[n+1];
for(int i=0;i<n;i++)
{
initial[i]=in.nextInt();
a[initial[i]]++;
// pq1[i+1]=new PriorityQueue<>();
pq2[i+1]=new PriorityQueue<>();
}
for(int i=0;i<n;i++)
{
last[i]=in.nextInt();
if(initial[i]==last[i])
coman[last[i]]++;
else
{
pq2[last[i]].add(i+1);
}
b[last[i]]++;
}
for(int i=0;i<m;i++)
{
painter[i]=in.nextInt();
c[painter[i]]++;
}
for(int i=1;i<=n;i++)
{
if(last[i-1]==painter[m-1])
lastt=i;
}
int ans[] = new int[m];
if(b[painter[m-1]]>0)
{
ans[m-1]=lastt;
if(!pq2[painter[m-1]].isEmpty())
{
ans[m-1]=pq2[painter[m-1]].poll();
}
}
else
{
System.out.println("NO");
return;
}
for(int i=m-2;i>=0;i--)
{
if(pq2[painter[i]].isEmpty())
{
ans[i]=ans[m-1];
}
else
{
ans[i]=pq2[painter[i]].poll();
}
}
for(int i=1;i<=n;i++)
{
if(!pq2[i].isEmpty())
{
System.out.println("NO");
// System.out.println(i);
return;
}
}
System.out.println("YES");
for(int i=0;i<m;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
static class pair{
int x;
int y;
pair(int x,int y) {
this.x = x;
this.y = y;
}
}
static class Maths {
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c
== -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 722411710c21d7637df52322805ebdc0 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | //package com.codeforces.Practise;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
public class FencePainting {
public static void main(String[] args) throws IOException {
Reader scan = new Reader();
FastPrinter bw = new FastPrinter();
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
int m = scan.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] p = new int[m];
for (int i = 0; i < n; i++)a[i]=scan.nextInt();
for (int i = 0; i < n; i++)b[i]=scan.nextInt();
for (int i = 0; i < m; i++) p[i]=scan.nextInt();
ArrayList<ArrayList<Integer>> li = new ArrayList<>();
for (int i = 0; i <= n; i++) {
li.add(new ArrayList<>());
}
int[] indofEle = new int[n + 1];
Arrays.fill(indofEle, -1);
int ele=-1;
int count = 0;
int[] changes = new int[n + 1];
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
li.get(b[i]).add(i);
if(b[i]==p[m-1]){
ele=(i+1);
}
changes[b[i]]++;
count++;
}
indofEle[b[i]] = i;
}
for (int i = 0; i < m; i++) {
changes[p[i]]--;
}
boolean flag = false;
loop:for (int i = 0; i <=n; i++) {
if (changes[i] > 0) {
flag = true;
break loop;
}
}
if (flag || indofEle[p[m - 1]] == -1) {
bw.println("NO");
} else {
bw.println("YES");
for (int k = 0; k < m; k++) {
ArrayList<Integer> li2 = li.get(p[k]);
if (li2.size() == 0) {
if(ele==-1){
bw.print((indofEle[p[m - 1]] + 1) + " ");
}
else{
bw.print(ele+" ");
}
} else {
bw.print((li2.get(0) + 1) + " ");
li2.remove(0);
}
}
bw.println();
}
bw.flush();
}
bw.close();
}
static class Reader {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
}
public static void safeSort(int[] array) {
Integer[] temp = new Integer[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(long[] array) {
Long[] temp = new Long[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(double[] array) {
Double[] temp = new Double[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | cad8c02316824327cf62e1ec376334db | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public final class Solution{
public static void main (String[] args) throws Exception
{
BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
n++;
m++;
int[] a= new int [n];
int[] b= new int [n];
int[] c= new int [m];
for(int i = 1; i<n ;i++){
a[i] =sc.nextInt();
}
Set<Integer> st= new HashSet<>();
for(int i = 1; i<n ;i++){
b[i] =sc.nextInt();
st.add(b[i]);
}
for(int i = 1; i<m ;i++){
c[i] =sc.nextInt();
}
int topaint=0;
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<>();
for(int i=1;i<n;i++){
if(a[i]!=b[i]){
if(hm.containsKey(b[i])){
ArrayList<Integer> l= hm.get(b[i]);
l.add(i);
hm.put(b[i], l);
}else{
ArrayList<Integer> l= new ArrayList<>();
l.add(i);
hm.put(b[i], l);
}
}
}
int [] ans = new int [m];
ArrayList<Integer> ext= new ArrayList<>();
for(int i=1;i<m-1;i++){
if(hm.containsKey(c[i])){
ArrayList<Integer> l= hm.get(c[i]);
if(l.size()>0) {
a[l.get(0)]= c[i];
ans[i] = l.get(0);
l.remove(0);
}else{
ext.add(i);
}
}else{
ext.add(i);
}
}
if(hm.containsKey(c[m-1])){
ArrayList<Integer> l= hm.get(c[m-1]);
if(l.size()>0) {
a[l.get(0)]= c[m-1];
ans[m-1]= l.get(0);
for(int j=0;j<ext.size(); j++){
ans[ext.get(j)]=l.get(0);
}
l.remove(0);
}else{
out : for(int i=0;i<n;i++){
if(c[m-1]==b[i]){
for(int j=0;j<ext.size(); j++){
ans[ext.get(j)]=i;
}
ans[m-1]=i;
a[i]=b[i];
break out;
}
}
}
}else{
out : for(int i=0;i<n;i++){
if(c[m-1]==b[i]){
for(int j=0;j<ext.size(); j++){
ans[ext.get(j)]=i;
}
ans[m-1]=i;
a[i]=b[i];
break out;
}
}
}
boolean flag = true;
for(int i=0;i<n;i++){
if(a[i] != b[i]){
flag= false;
break;
}
}
// for(int i=1;i<m;i++){
// op.write(ans[i]+" ");
// }
if(flag){
boolean f=false;
for(int i=1;i<m;i++){
if(ans[i]==0){
f=true;
}
}
if(f){
// op.write("debug in f");
op.write("NO\n");
continue;
}
op.write("YES\n");
for(int i=1;i<m;i++){
op.write(ans[i]+" ");
}
}else{
// op.write("debug in else");
op.write("NO");
}
op.write("\n");
}
op.flush();
}}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
// BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
// 1 1 1 0 -1 -1 -1 1 1 1 0 -1 -1 1 1 1 0 -1 1 1 1 0 1 1 1 1 1 1 | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 86cdd11e17cbd79efbc3f84dc0d530ce | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
public class FencePainting {
static class pair {
long first;
long second;
public pair(long first, long second) {
this.first = first;
this.second = second;
}
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
int n = 0;
int m = 0;
while (t-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
int[] a = sc.readArray(n);
int[] b = sc.readArray(n);
int[] c = sc.readArray(m);
System.out.println(new Solver().solve(n, m, a, b, c));
}
}
static class Solver {
public int search(int color, int[] a, int[] b, int n) {
int best = -1;
for (int i = n - 1; i >= 0; i--) {
if (b[i] != a[i] && color == b[i]) {
return i;
} else if (b[i] == a[i] && color == b[i]) {
best = i;
}
}
return best;
}
public String solve(int n, int m, int[] a, int[] b, int[] c) {
StringBuilder sb = new StringBuilder();
sb.append("YES");
sb.append("\n");
int w = search(c[m - 1], a, b, n);
if (w == -1) {
return "NO";
}
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
count++;
if (map.containsKey(b[i])) {
ArrayList<Integer> arrayList = map.get(b[i]);
arrayList.add(i);
} else {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(i);
map.put(b[i], arrayList);
}
}
}
if (c.length < count) {
return "NO";
}
for (int i = 0; i < m; i++) {
int color = c[i];
if (map.containsKey(color)) {
ArrayList<Integer> arr = map.get(color);
int k = arr.get(0);
arr.remove(0);
if (arr.size() == 0) {
map.remove(color);
}
a[k] = color;
sb.append(k + 1);
sb.append(" ");
} else {
sb.append(w + 1);
sb.append(" ");
}
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i])
return "NO";
}
return sb.toString();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 52fb6234fbad10f3490ea8d25c428bc2 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class C1481 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int m = in.nextInt();
int[] start = new int[n];
for (int i = 0; i < n; i++) {
start[i] = in.nextInt() - 1;
}
ArrayList<Integer>[] colorsNeeded = new ArrayList[n];
for (int i = 0; i < colorsNeeded.length; i++) {
colorsNeeded[i] = new ArrayList<Integer>();
}
int[] desiredHas = new int[n];
Arrays.fill(desiredHas, -1);
for (int i = 0; i < n; i++) {
int currentDesire = in.nextInt() - 1;
desiredHas[currentDesire] = i;
if (start[i] != currentDesire) {
colorsNeeded[currentDesire].add(i);
}
}
int paintOverStart = 0;
int[] paintersPlanks = new int[m];
for (int i = 0; i < m; i++) {
int currentPainterColor = in.nextInt() - 1;
if (colorsNeeded[currentPainterColor].size() > 0) {
int index = colorsNeeded[currentPainterColor].remove(colorsNeeded[currentPainterColor].size() - 1);
for (int j = paintOverStart; j <= i; j++) {
paintersPlanks[j] = index;
}
paintOverStart = i + 1;
} else if (desiredHas[currentPainterColor] == -1) {
if (paintOverStart == i)
paintOverStart = i;
} else {
int index = desiredHas[currentPainterColor];
for (int j = paintOverStart; j <= i; j++) {
paintersPlanks[j] = index;
}
paintOverStart = i + 1;
}
}
for (int i = 0; i < n; i++) {
if (colorsNeeded[i].size() != 0) {
paintOverStart = -1;
}
}
if (paintOverStart == m) {
System.out.println("YES");
for (int i = 0; i < m; i++) {
System.out.print((paintersPlanks[i] + 1) + " ");
}
} else {
System.out.println("NO");
}
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | cd3163a0bc756c698a199da3d0945931 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author sarthakmanna
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CFencePainting solver = new CFencePainting();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CFencePainting {
final static Helper hp = new Helper();
public void solve(int testNumber, FastReader in, PrintWriter out) {
int i, j, k;
int N = in.nextInt(), M = in.nextInt();
int[] init = in.getIntArray(N);
int[] req = in.getIntArray(N);
int[] painter = in.getIntArray(M);
ArrayDeque<Integer>[] colors = new ArrayDeque[N + 1], mismatches = new ArrayDeque[N + 1];
for (i = 0; i <= N; ++i) {
colors[i] = new ArrayDeque<>();
mismatches[i] = new ArrayDeque<>();
}
for (i = 0; i < N; ++i) {
if (init[i] != req[i]) {
mismatches[req[i]].add(i);
}
colors[req[i]].add(i);
}
int[] assignment = new int[M];
Arrays.fill(assignment, -7);
int savior = -7;
for (i = M - 1; i >= 0; --i) {
if (!mismatches[painter[i]].isEmpty()) {
int idx = mismatches[painter[i]].poll();
assignment[i] = idx;
if (savior < 0) savior = idx;
} else if (savior >= 0) {
assignment[i] = savior;
}
}
for (ArrayDeque<Integer> itr : mismatches)
if (!itr.isEmpty()) {
out.println("NO");
return;
}
if (assignment[M - 1] < 0 && !colors[painter[M - 1]].isEmpty()) {
savior = colors[painter[M - 1]].poll();
for (i = M - 1; i >= 0; --i) if (assignment[i] < 0) assignment[i] = savior;
}
if (hp.min(assignment) < 0) {
out.println("NO");
} else {
out.println("YES");
for (int itr : assignment) out.print(++itr + " ");
out.println();
//System.err.println(Arrays.toString(assignment));
for (i = 0; i < M; ++i) init[assignment[i]] = painter[i];
if (!Arrays.equals(init, req)) {
System.exit(7 / 0);
}
}
}
}
static class Helper {
public final long MOD;
public final int MAXN;
final Random rnd;
public Helper() {
MOD = 1000_000_007;
MAXN = 1000_006;
rnd = new Random();
}
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public int min(int... ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.min(ret, itr);
return ret;
}
}
static class FastReader {
static final int BUFSIZE = 1 << 20;
static byte[] buf;
static int index;
static int total;
static InputStream in;
public FastReader(InputStream is) {
try {
in = is;
buf = new byte[BUFSIZE];
} catch (Exception e) {
}
}
private int scan() {
try {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
} catch (Exception | Error e) {
System.err.println(e.getMessage());
return 13 / 0;
}
}
public String next() {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan())
sb.append((char) c);
return sb.toString();
}
public int nextInt() {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public int[] getIntArray(int size) {
int[] ar = new int[size];
for (int i = 0; i < size; ++i) ar[i] = nextInt();
return ar;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | f1795089604f521dc97626fb59ccd327 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static class Painter{
int arrives, color;
public Painter(int arrives, int color){
this.arrives = arrives;
this.color = color;
}
@Override
public String toString(){
return "Arrival idx = " + this.arrives + " Color = " + this.color;
}
}
public static void process(int test_number)throws IOException
{
int n = ni(), m = ni(), a[] = new int[n], b[] = new int[n], colorIdxMap[] = new int[n];
Arrays.fill(colorIdxMap, -1);
HashSet<Integer> finalColors = new HashSet<Integer>();
Queue<Painter> req[] = new Queue[n];
ArrayList<Painter> notReq = new ArrayList<>();
for(int i = 0; i < n; i++){
req[i] = new LinkedList<>();
}
for(int i = 0; i < n; i++){
a[i] = ni();
}
for(int i = 0; i < n; i++){
b[i] = ni();
colorIdxMap[b[i] - 1] = i;
finalColors.add(b[i] - 1);
}
int mask = -1;
for(int i = 0; i < m; i++){
int c = ni();
Painter ob = new Painter(i, c - 1);
if(finalColors.contains(c - 1)){
req[c - 1].add(ob);
}
else
notReq.add(ob);
}
boolean flag = true;
int res[] = new int[m];
for(int i = 0; i < n && flag; i++){
if(a[i] != b[i]){
Painter ob = req[b[i] - 1].poll();
if(ob == null){
flag = false;
break;
}
res[ob.arrives] = (i + 1);
a[i] = ob.color + 1;
mask = Math.max(mask, ob.arrives);
}
}
for(int i = 0; i < n; i++)
flag = flag && (a[i] == b[i]);
if(!flag){
p("NO\n");
} else{
for(int i = 0; i < n; i++){
if(req[i].size() != 0){
for(Painter ob : req[i]){
res[ob.arrives] = colorIdxMap[ob.color] + 1;
mask = Math.max(mask, ob.arrives);
}
}
}
if(notReq.size() != 0 && mask < notReq.get(notReq.size() - 1).arrives){
p("NO\n");
return ;
}
for(Painter ob : notReq){
res[ob.arrives] = res[mask];
}
p("YES\n");
for(int x : res)
p(x + " ");
p("\n");
}
}
static final long mod = (long)1e9+7l;
static boolean DEBUG = true;
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc = new FastReader();
long s = System.currentTimeMillis();
int t = 1;
t = ni();
for(int i = 1; i <= t; i++)
process(i);
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
}
static void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); };
static void pn(Object o){ out.println(o); }
static void p(Object o){ out.print(o); }
static int ni()throws IOException{ return Integer.parseInt(sc.next()); }
static long nl()throws IOException{ return Long.parseLong(sc.next()); }
static double nd()throws IOException{ return Double.parseDouble(sc.next()); }
static String nln()throws IOException{ return sc.nextLine(); }
static long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); }
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); }
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 23299202d1802d68cd9fd2098d85f478 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.LinkedList;
public class planet {
static class color{
int b,i;
color(int n,int i){
this.b=n;
this.i=i+1;
}
}
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
please:for(;t>0;t--) {
int count=0,i;
String nm[]=br.readLine().split(" ");
int n=Integer.parseInt(nm[0]);
int m=Integer.parseInt(nm[1]);
int a[]=new int[n];
int b[]=new int[n];
int c[]=new int[m];
int ans[]=new int[m];
String s1[]=br.readLine().split(" ");
String s2[]=br.readLine().split(" ");
String s3[]=br.readLine().split(" ");
LinkedList<color> ll=new LinkedList<color>();
for( i=0;i<n;i++) {
a[i]=Integer.parseInt(s1[i]);
b[i]=Integer.parseInt(s2[i]);
if(a[i]!=b[i]) {
ll.add(new color(b[i],i));
}
}
for( i=0;i<m;i++) {
c[i]=Integer.parseInt(s3[i]);
ans[i]=-1;
}
boolean isfound=true;
me:while(ll.size()!=0 && isfound) {
isfound=false;
color rcol=ll.getFirst();
for(i=0;i<m;i++) {
if(c[i]==rcol.b && ans[i]==-1) {
ans[i]=rcol.i;
isfound=true;
ll.removeFirst();
count++;
continue me;
}
}
}
e:if(isfound==false) {
System.out.println("NO");
}
else {
int plank=1;
if(count!=m) {
if(ans[m-1]!=-1) {
plank=ans[m-1];
}
else {
boolean flag=false;
for(i=0;i<n;i++) {
if(b[i]==c[m-1]) {
plank=i+1;
flag=true;
break;
}
}
if(!flag) {
System.out.println("NO");
continue please;
}
}
}
System.out.println("YES");
for(i=0;i<m;i++) {
if(ans[i]!=-1) {
System.out.print(ans[i]+" ");
}
else {
System.out.print(plank+" ");
}
}
System.out.println();
}
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 5b2fe785b151f49096f22ff8f02c6447 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.StringTokenizer;
public class newMain {
static int mod;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t=sc.nextInt();
test:while(t-->0) {
int n=sc.nextInt(),m=sc.nextInt();
int[] a=sc.nextIntArr(n);
int[] b=sc.nextIntArr(n);
int[] c=sc.nextIntArr(m);
HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();
int skipID=-1;
boolean canSkip=false;
for(int i=0;i<n;i++) {
if(b[i]==c[m-1]&&a[i]!=b[i]) {
skipID=i+1;
canSkip=true;
}
if(a[i]!=b[i]) {
ArrayList<Integer> ar=map.getOrDefault(b[i],new ArrayList<Integer>());
ar.add(i+1);
map.put(b[i], ar);
}
}
if(skipID==-1) {
for(int i=0;i<n;i++)
if(b[i]==c[m-1])
{
skipID=i+1;
canSkip=true;
}
}
int[] ans=new int[m];
Arrays.fill(ans, -1);
for(int i=m-1;i>=0;i--) {
ArrayList<Integer> ar=map.getOrDefault(c[i], new ArrayList<>());
if(ar.size()==0) {
ans[i]=skipID;
}
else {
ans[i]=ar.get(ar.size()-1);
ar.remove((int)ar.size()-1);
}
map.put(c[i],ar);
}
boolean yes=true;
for(Entry<Integer,ArrayList<Integer>> e: map.entrySet()) {
if(e.getValue().size()>0)yes=false;
}
for(int num:ans)
if(num==-1)yes=false;
if(!canSkip)yes=false;
if(!yes) {
pw.println("NO");
}else {
for(int i=0;i<m;i++) {
a[ans[i]-1]=c[i];
}
for(int i=0;i<n;i++)
if(a[i]!=b[i])
yes=false;
if(!yes) {
pw.println("NO");
continue test;
}
pw.println("YES");
for(int num:ans)
pw.print(num+" ");
pw.println();
}
}
pw.flush();
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static int log(int n, int base) {
int ans = 0;
while (n + 1 > base) {
ans++;
n /= base;
}
return ans;
}
static int pow(int b, long e) {
int ans = 1;
while (e > 0) {
if ((e & 1) == 1)
ans = (int) ((ans * 1l * b));
e >>= 1;
{
}
b = (int) ((b * 1l * b));
}
return ans;
}
static long powmod(long b, long e, int mod) {
long ans = 1;
b %= mod;
while (e > 0) {
if ((e & 1) == 1)
ans = (int) ((ans * 1l * b) % mod);
e >>= 1;
b = (int) ((b * 1l * b) % mod);
}
return ans;
}
public static long add(long a, long b) {
return (a + b) % mod;
}
public static long sub(long a, long b) {
return (a - b + mod) % mod;
}
public static long mul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static class longPair implements Comparable<longPair> {
long x, y;
public longPair(long a, long b) {
x = a;
y = b;
}
public int compareTo(longPair p) {
return (p.x == x) ? ((p.y == y) ? 0 : (y > p.y) ? 1 : -1) : (x > p.x) ? 1 : -1;
}
}
static class Pair implements Comparable<Pair> {
int x;
int y;
public Pair(int a, int b) {
this.x = a;
y = b;
}
public int compareTo(Pair o) {
return (x == o.x) ? ((y > o.y) ? 1 : (y == o.y) ? 0 : -1) : ((x > o.x) ? 1 : -1);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public long[] nextLongArr(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine(), " ,");
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++) {
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
if (sb.length() == 18) {
res += Long.parseLong(sb.toString()) / f;
sb = new StringBuilder("0");
}
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
public static void shuffle(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 062055f2a762c4df69c9b023e3bfd295 | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | /*
ID: xfrostb1
LANG: JAVA
TASK: c
*/
import java.io.*;
import java.util.*;
public class c {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader f = new BufferedReader(in);
int N = Integer.parseInt(f.readLine());
for(int i = 0; i < N; i++) {
// long M = Long.parseLong(f.readLine());
StringTokenizer s = new StringTokenizer(f.readLine());
int M = Integer.parseInt(s.nextToken());
int K = Integer.parseInt(s.nextToken());
int[] a = new int[M];
int[] contains = new int[M+1];
s = new StringTokenizer(f.readLine());
for(int j = 0; j < M; j++){
contains[j+1] = -1;
}
for(int j = 0; j < M; j++){
a[j] = Integer.parseInt(s.nextToken());
}
int[] b = new int[M];
s = new StringTokenizer(f.readLine());
HashMap<Integer,ArrayList<Integer>> hash = new HashMap<>();
for(int j = 0; j < M; j++){
hash.put(j+1,new ArrayList<>());
}
for(int j = 0; j < M; j++){
b[j] = Integer.parseInt(s.nextToken());
if(a[j] != b[j]){
hash.get(b[j]).add(j);
}else{
contains[a[j]] = j;
}
}
int[] c = new int[K];
s = new StringTokenizer(f.readLine());
for(int j = 0; j < K; j++){
c[j] = Integer.parseInt(s.nextToken());
}
int[] ret = new int[K];
int index = -1;
boolean failed = false;
for(int j = K-1; j >= 0; j--){
if(hash.get(c[j]).size() == 0){
if(contains[c[j]]==-1){
if(index == -1){
System.out.println("NO");
failed = true;
break;
}else{
ret[j] = index;
}
}else{
ret[j] = contains[c[j]];
if(index == -1){
index = contains[c[j]];
}
}
}else{
ret[j] = hash.get(c[j]).get(0);
index = ret[j];
hash.get(c[j]).remove(0);
}
}
if(!failed) {
for (int j = 0; j < M; j++) {
if (hash.get(j + 1).size() != 0) {
System.out.println("NO");
failed = true;
break;
}
}
}
if(!failed){
System.out.println("YES");
for(int j = 0; j < K; j++){
System.out.print(ret[j]+1);
if(j < K-1){
System.out.print(" ");
}else{
System.out.println();
}
}
}
}
}
static class Input {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() throws IOException{
while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return next();
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | 915ac55ca26f5da30b324a6adf2c85ef | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0){
sb=new StringBuilder();
solve();
System.out.print(sb.toString());
}
}
public static void solve(){
int n=sc.nextInt();
// int f=-1;
int m=sc.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[m];
// int fl=-1;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
b[i]=sc.nextInt();
}
for(int i=0;i<m;i++){
c[i]=sc.nextInt();
}
HashMap<Integer,ArrayList<Integer>> hm = new HashMap<>();
for(int i=0;i<n;i++){
if(a[i]!=b[i]){
if(!hm.containsKey(b[i])){
hm.put(b[i],new ArrayList<>());
}
hm.get(b[i]).add(i);
}
}
int last = -1;
if(hm.containsKey(c[m-1])){
ArrayList<Integer> al= hm.get(c[m-1]);
last = al.remove(al.size()-1);
if(hm.get(c[m-1]).size()==0){
hm.remove(c[m-1]);
}
}else{
for(int i=0;i<n;i++){
if(b[i]==c[m-1]){
last=i;
break;
}
}
}
if(last==-1){
sb.append("NO\n");
// System.out.print(sb.toString());
// System.out.println("NO");
return;
// f=-2;
}
// if(f==-1){
int[] ans = new int[m];
ans[m-1] = last;
for(int i=0;i<m-1;i++){
if(hm.containsKey(c[i])){
ArrayList<Integer> al= hm.get(c[i]);
int idx = al.remove(al.size()-1);
// = hm.get(c[i]).remove();
ans[i]=idx;
if(hm.get(c[i]).size()==0){
hm.remove(c[i]);
}
}else{
ans[i]=last;
}
}
if(hm.size()!=0){
sb.append("NO\n");
return;
}
// if(fl==-1){
sb.append("YES\n");
for(int i=0;i<m;i++){
sb.append(ans[i]+1).append(" ");
}
sb.append("\n");
}
} | Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output | |
PASSED | b25fc2579fc6fefe5a0e5ba523f8441f | train_107.jsonl | 1612535700 | 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 $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.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 $$$c_j$$$. 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. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static void main(String[] args) throws IOException {
int t = i();
sb = new StringBuilder();
while (t-- > 0) {
solve();
}
out.println(sb.toString());
out.close();
}
static int mod = 1000000007;
static StringBuilder sb;
private static void solve() {
int n = i();
int m = i();
int[] a = readArray(n);
int[] b = readArray(n);
int[] c = readArray(m);
HashMap<Integer, ArrayDeque<Integer>> paint = new HashMap<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (!paint.containsKey(b[i])) {
paint.put(b[i], new ArrayDeque<>());
}
paint.get(b[i]).add(i);
}
}
int last = -1;
if (paint.containsKey(c[m - 1])) {
last = paint.get(c[m - 1]).remove();
if (paint.get(c[m - 1]).size() == 0) {
paint.remove(c[m - 1]);
}
} else {
for (int i = 0; i < n; i++) {
if (b[i] == c[m - 1]) {
last = i;
break;
}
}
}
if (last == -1) {
sb.append("NO\n");
return;
}
int[] ans = new int[m];
ans[m - 1] = last;
for (int i = 0; i < m - 1; i++) {
if (paint.containsKey(c[i])) {
ans[i] = paint.get(c[i]).remove();
if (paint.get(c[i]).size() == 0) {
paint.remove(c[i]);
}
} else {
ans[i] = last;
}
}
if (paint.size() != 0) {
sb.append("NO\n");
return;
}
sb.append("YES\n");
for (int j = 0; j < m; j++) {
sb.append(ans[j] + 1).append(" ");
}
sb.append("\n");
}
// Fenwick Tree
static class fenwickTree {
int n;
long[] farr;
fenwickTree(int n, long[] a) {
this.n = n;
farr = new long[n + 1];
for (int i = 0; i < n; i++) {
update(i + 1, a[i]);
}
}
void update(int i, long val) {
while (i < farr.length) {
farr[i] += val;
i += (i & -i);
}
}
long sum(int i) {
long sum = 0L;
while (i > 0) {
sum += farr[i];
i -= (i & -i);
}
return sum;
}
}
// ****CLASS PAIR ************************************************
// static class Pair implements Comparable<Pair> {
// int sp;
// int ep;
//
// Pair(int sp, int ep) {
// this.sp = sp;
// this.ep = ep;
// }
//
// public int compareTo(Pair o) {
// return this.sp - o.sp;
// }
//
// }
static class Pair implements Comparable<Pair> {
char ch;
ArrayList<Integer> idxs;
Pair(char ch, ArrayList<Integer> idxs) {
this.ch = ch;
this.idxs = idxs;
}
public int compareTo(Pair o) {
return o.idxs.size() - this.idxs.size();
}
}
static class Edge implements Comparable<Edge> {
int u;
int v;
int wt;
Edge(int u, int v, int wt) {
this.u = u;
this.v = v;
this.wt = wt;
}
public int compareTo(Edge o) {
return this.wt - o.wt;
}
}
// *************Disjoint set
// union*********//
static class dsu {
int[] par;
int[] rank;
dsu(int n) {
par = new int[n + 1];
rank = new int[n + 1];
for (int i = 0; i <= n; i++) {
par[i] = i;
rank[i] = 1;
}
}
int find(int a) {
if (par[a] == a) {
return a;
}
int x = find(par[a]);
par[a] = x;
return x;
}
boolean union(int a, int b) {
int pa = find(a);
int pb = find(b);
if (pa == pb) {
return false;
}
if (rank[pa] > rank[pb]) {
par[pb] = pa;
} else if (rank[pb] > rank[pa]) {
par[pa] = pb;
} else {
par[pa] = pb;
rank[pb]++;
}
return true;
}
}
// **************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long[] fact = new long[(int) 1e6 + 10];
fact[0] = fact[1] = 1;
for (int i = 2; i < fact.length; i++) {
fact[i] = ((long) (i % mod) * (long) (fact[i - 1] % mod)) % mod;
}
long res = fact[n] % mod;
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
return res;
}
static ArrayList<Integer> divisors(int n) {
ArrayList<Integer> al = new ArrayList<>();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (n == i * i) {
al.add(i);
} else {
al.add(i);
al.add(n / i);
}
}
}
Collections.sort(al);
return al;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
private static boolean pow2(int i) {
return (i & (i - 1)) == 0;
}
private static void sort(int[] arr) {
int n = arr.length;
ArrayList<Integer> res = new ArrayList<>();
for (int a : arr) {
res.add(a);
}
Collections.sort(res);
for (int i = 0; i < n; i++) {
arr[i] = res.get(i);
}
}
private static void sort(long[] arr) {
int n = arr.length;
ArrayList<Long> res = new ArrayList<>();
for (long a : arr) {
res.add(a);
}
Collections.sort(res);
for (int i = 0; i < n; i++) {
arr[i] = res.get(i);
}
}
private static void revSort(int[] arr) {
int n = arr.length;
ArrayList<Integer> res = new ArrayList<>();
for (int a : arr) {
res.add(a);
}
Collections.sort(res, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = res.get(i);
}
}
private static ArrayList<Integer> sieveOfEratosthenes(int n) {
boolean[] arr = new boolean[n + 1];
arr[0] = arr[1] = true;
for (int i = 2; i * i <= n; i++) {
if (!arr[i]) {
for (int j = i * i; j <= n; j += i) {
arr[j] = true;
}
}
}
ArrayList<Integer> primes = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (!arr[i]) {
primes.add(i);
}
}
return primes;
}
private static ArrayList<Integer> segmentedSieve(int s, int e) {
int len = (int) (Math.sqrt(e) + 1);
ArrayList<Integer> primes = sieveOfEratosthenes(len);
boolean[] arr2 = new boolean[e - s + 1];
if (s == 1) {
arr2[0] = true;
}
ArrayList<Integer> res = new ArrayList<>();
for (int p : primes) {
int st = (int) (p * (Math.ceil((double) s / p)));
if (st < s) {
st += p;
}
if (st == p) {
st += p;
}
for (int i = st; i <= e; i += p) {
arr2[i - s] = true;
}
}
for (int i = 0; i < arr2.length; i++) {
if (!arr2[i]) {
res.add(i + s);
}
}
return res;
}
private static long pow(int n, int k) {
long res = 1;
long num = (long) n;
while (k > 0) {
if ((k & 1) == 1) {
res = (res * num) % mod;
}
k /= 2;
num = num * num % mod;
}
return res;
}
private static int countDigits(int n) {
int c = 0;
while (n != 0) {
c++;
n /= 10;
}
return c;
}
private static boolean palindrome(String str) {
int s = 0;
int e = str.length() - 1;
while (s < e) {
if (str.charAt(s) != str.charAt(e)) {
return false;
}
s++;
e--;
}
return true;
}
private static void reverse(int[] arr, int s, int e) {
while (s < e) {
int temp = arr[s];
arr[s++] = arr[e];
arr[e--] = temp;
}
}
private static int getMax(int[] arr, int s, int e) {
int maxi = s;
for (int i = s + 1; i <= e; i++) {
if (arr[i] > arr[maxi]) {
maxi = i;
}
}
return maxi;
}
private static int getMin(int[] arr, int s, int e) {
int mini = s;
for (int i = s + 1; i <= e; i++) {
if (arr[i] < arr[mini]) {
mini = i;
}
}
return mini;
}
// ******LOWEST COMMON MULTIPLE************************
private static long lcm(long n1, long n2) {
return (n1 * n2) / gcd(n1, n2);
}
private static int lcm(int n1, int n2) {
return (n1 * n2) / gcd(n1, n2);
}
// ***********GCD*****************
private static long gcd(long num1, long num2) {
if (num2 == 0) {
return num1;
}
return gcd(num2, num1 % num2);
}
private static int gcd(int num1, int num2) {
if (num2 == 0) {
return num1;
}
return gcd(num2, num1 % num2);
}
// **************PRIME FACTORIZE **********************************//
static TreeMap<Long, Long> prime(long n) {
TreeMap<Long, Long> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
long nt = 0L;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i * 1L, nt);
}
}
if (n != 1)
h.put(n, 1L);
return h;
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
//*****************INPUT PATTERN******************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArray(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
}
| Java | ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"] | 2 seconds | ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"greedy"
] | a350430c707bb18a146df9f80e114f45 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases. | 1,600 | For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ 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). | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.