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
6db179011f34207eb82d154cf8bffcf4
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]).pollLast(); 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]).pollLast(); 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
PASSED
caba7b40b479f871f2ce3547d5056426
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.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class hacker49 { 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 static void main(String[] args) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); int m=s.nextInt(); int[] a=new int[n+1]; int[] b=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=s.nextInt(); } for(int i=1;i<=n;i++) { b[i]=s.nextInt(); } int[] d=new int[m+1]; HashMap<Integer,Stack<Integer>> e1=new HashMap<>(); // HashMap<Integer,Integer> e2=new HashMap<>(); int[] c=new int[n+1]; for(int i=1;i<=n;i++) { if(b[i]!=a[i]) { if(!e1.containsKey(b[i])) { e1.put(b[i], new Stack<>()); e1.get(b[i]).add(i); }else { e1.get(b[i]).add(i); } } // else { // }else { c[b[i]]=i; // } // } } for(int i=1;i<=m;i++) { d[i]=s.nextInt(); } Stack<Integer> f=new Stack<>(); boolean p=true; //ArrayList<Integer> e=new ArrayList<>(); int[] x=new int[m+1]; out:for(int i=1;i<=m;i++) { if(e1.containsKey(d[i])) { int g=e1.get(d[i]).pop(); x[i]=g; a[g]=d[i]; if(e1.get(d[i]).isEmpty()) { e1.remove(d[i]); } while(!f.isEmpty()) { int r=f.pop(); x[r]=g; } }else if(c[d[i]]!=0) { while(!f.isEmpty()) { int r=f.pop(); x[r]=c[d[i]]; } x[i]=c[d[i]]; } else { f.add(i); } } if(!f.isEmpty()) { out.println("NO"); //while(!f.isEmpty()) { // System.out.println(f.pop()); //} }else { // boolean p=true; // out:for(int i=1;i<=n;i++) { if(a[i]!=b[i]) { p=false; break out; } } if(p) { out.println("YES"); for(int i=1;i<=m;i++) { out.print(x[i]+" "); } out.println(); }else { out.println("NO"); } } t--; } out.close(); } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { // int count=0; long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } static ArrayList<Integer>[] f1=new ArrayList[200001]; static ArrayList<Integer>[] f2=new ArrayList[401]; static int vis1[]=new int[200001]; static int vis2[]=new int[401]; static int len1[]=new int[200001]; static int len2[]=new int[401]; static void bfs1(int node) { vis1[node]=1; PriorityQueue<pair> q=new PriorityQueue<>(); q.add(new pair(node,0)); int h=0; while(!q.isEmpty()) { pair f=q.poll(); for(int i=0;i<f1[f.a].size();i++) { if(vis1[f1[f.a].get(i)]==0) { q.add(new pair(f1[f.a].get(i),f.b+1)); vis1[f1[f.a].get(i)]=1; len1[f1[f.a].get(i)]=f.b+1; } } } } static void bfs2(int node) { vis2[node]=1; PriorityQueue<pair> q=new PriorityQueue<>(); q.add(new pair(node,0)); int h=0; while(!q.isEmpty()) { pair f=q.poll(); for(int i=0;i<f2[f.a].size();i++) { if(vis2[f2[f.a].get(i)]==0) { q.add(new pair(f2[f.a].get(i),f.b+1)); vis2[f2[f.a].get(i)]=1; len2[f2[f.a].get(i)]=f.b+1; } } } } public static int[] Create(int[] a,int n) { int[] b=new int[n+1]; for(int i=1;i<=n;i++) { int j=i; int h=a[i]; while(i<=n) { b[i]+=h; i=get_next(i); } i=j; } return b; } public static int get_next(int a) { return a+(a&-a); } public static int get_parent(int a) { return a-(a&-a); } public static int query_1(int[] b,int index) { int sum=0; if(index<=0) { return 0; } while(index>0) { sum+=b[index]; index=get_parent(index); } return sum; } public static int query(int[] b,int n,int l,int r) { int sum=0; sum+=query_1(b,r); sum-=query_1(b,l-1); return sum; } public static void update(int[] a,int[] b,int n,int index,int val) { int diff=val-a[index]; a[index]+=diff; while(index<=n) { b[index]+=diff; index=get_next(index); } } // public static void Create(int[] a,pair[] segtree,int low,int high,int pos) { // if(low>high) { // return; // } // if(low==high) { // segtree[pos].b.add(a[low]); // return ; // } // int mid=(low+high)/2; // Create(a,segtree,low,mid,2*pos); // Create(a,segtree,mid+1,high,2*pos+1); // segtree[pos].b.addAll(segtree[2*pos].b); // segtree[pos].b.addAll(segtree[2*pos+1].b); // } // public static void update(pair[] segtree,int low,int high,int index,int pos,int val,int prev) { // if(index>high || index<low) { // return ; // } // if(low==high && low==index) { // segtree[pos].b.remove(prev); // segtree[pos].b.add(val); // return; // } // int mid=(high+low)/2; // update(segtree,low,mid,index,2*pos,val,prev); // update(segtree,mid+1,high,index,2*pos+1,val,prev); // segtree[pos].b.clear(); // segtree[pos].b.addAll(segtree[2*pos].b); // segtree[pos].b.addAll(segtree[2*pos+1].b); // // } // public static pair query(pair[] segtree,int low,int high,int qlow,int qhigh ,int pos) { // if(low>=qlow && high<=qhigh) { // return segtree[pos]; // } // if(qhigh<low || qlow>high) { // return new pair(); // } // int mid=(low+high)/2; // pair a1=query(segtree,low,mid,qlow,qhigh,2*pos); // pair a2=query(segtree,mid+1,high,qlow,qhigh,2*pos+1); // pair a3=new pair(); // a3.b.addAll(a1.b); // a3.b.addAll(a2.b); // return a3; // // } // // public static int nextPowerOf2(int n) // { // n--; // n |= n >> 1; // n |= n >> 2; // n |= n >> 4; // n |= n >> 8; // n |= n >> 16; // n++; // return n; // } // //// static class pair implements Comparable<pair>{ //// private int a; //// private long b; ////// private long c; //// pair(int a,long b){ //// this.a=a; //// this.b=b; ////// this.c=c; //// } //// public int compareTo(pair o) { ////// if(this.a!=o.a) { ////// return Long.compare(this.a, o.a); ////// }else { //// return Long.compare(o.b,this.b); ////// } //// } //// } static class pair implements Comparable<pair>{ private int a; private int b; pair(int a,int b){ // this.b=new HashSet<>(); this.a=a; this.b=b; } public int compareTo(pair o) { return Integer.compare(this.b, o.b); } } public static int lower_bound(ArrayList<Long> a ,int n,long x) { int l=0; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a.get(mid)<=x) { l=mid; }else { r=mid; } } return l; } public static int[] is_prime=new int[1000001]; public static ArrayList<Long> primes=new ArrayList<>(); public static void sieve() { long maxN=1000000; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]==1) { // primes.add((int) i); for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]=0; } } } for(long i=0;i<=maxN;i++) { if(is_prime[(int) i]==1) { primes.add(i); } } } // public static pair[] merge_sort(pair[] A, int start, int end) { // if (end > start) { // int mid = (end + start) / 2; // pair[] v = merge_sort(A, start, mid); // pair[] o = merge_sort(A, mid + 1, end); // return (merge(v, o)); // } else { // pair[] y = new pair[1]; // y[0] = A[start]; // return y; // } // } // public static pair[] merge(pair a[], pair b[]) { // pair[] temp = new pair[a.length + b.length]; // int m = a.length; // int n = b.length; // int i = 0; // int j = 0; // int c = 0; // while (i < m && j < n) { // if (a[i].b >= b[j].b) { // temp[c++] = a[i++]; // // } else { // temp[c++] = b[j++]; // } // } // while (i < m) { // temp[c++] = a[i++]; // } // while (j < n) { // temp[c++] = b[j++]; // } // return temp; // } public static long im(long a) { return binary_exponentiation_1(a,mod-2)%mod; } public static long binary_exponentiation_1(long a,long n) { long res=1; while(n>0) { if(n%2!=0) { res=((res)%(1000000007) * (a)%(1000000007))%(1000000007); n--; }else { a=((a)%(1000000007) *(a)%(1000000007))%(1000000007); n/=2; } } return (res)%(1000000007); } public static long bn(long a,long n) { long res=1; while(n>0) { if(n%2!=0) { res=res*a; n--; }else { a*=a; n/=2; } } return res; } public static long[] fac=new long[100001]; public static void find_factorial() { fac[0]=1; fac[1]=1; for(int i=2;i<=100000;i++) { fac[i]=(fac[i-1]*i)%(mod); } } static long mod=1000000007; public static long GCD(long a,long b) { if(b==(long)0) { return a; } return GCD(b , a%b); } static long c=0; }
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
073093d8c4aeff80ad7442f3665afb8d
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.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(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 { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), m = in.nextInt(); int[] a = in.nextIntArray(n); int[] b = new int[n]; HashMap<Integer, ArrayList<Integer>> set = new HashMap<>(); for (int i = 0; i < n; i++) { b[i] = in.nextInt(); if (!set.containsKey(b[i])) set.put(b[i], new ArrayList<>()); set.get(b[i]).add(i); } int[] c = new int[m]; for (int i = 0; i < m; i++) { c[i] = in.nextInt(); } HashMap<Integer, HashSet<Integer>> hm = new HashMap<>(); int countdiff = 0; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { if (!hm.containsKey(b[i])) { hm.put(b[i], new HashSet<>()); } hm.get(b[i]).add(i); countdiff++; } } if (countdiff > m) { out.println("NO"); return; } int[] ans = new int[m]; Arrays.fill(ans, -1); int lind = -1; for (int i = 0; i < m; i++) { if (!set.containsKey(c[i])) continue; HashSet<Integer> temp = hm.get(c[i]); if (temp == null) continue; for (int ind : temp) { ans[i] = ind + 1; if (i == m - 1) lind = ind + 1; hm.get(c[i]).remove(ind); break; } } if (!set.containsKey(c[m - 1])) { out.println("NO"); return; } if (lind == -1) { lind = set.get(c[m - 1]).get(0) + 1; } for (int key : hm.keySet()) { if (hm.get(key).size() != 0) { out.println("NO"); return; } } for (int i = 0; i < m; i++) { if (ans[i] == -1) { ans[i] = lind; } } out.println("YES"); out.println(ans); } } static class InputReader { BufferedReader reader; 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[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int nextInt() { return Integer.parseInt(next()); } } 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 print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void println(Object... objects) { print(objects); writer.println(); } public void 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 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
da60368e9f173c85fe26431c115378c9
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.*; @SuppressWarnings("unchecked") public class FencePainting implements Runnable { void solve() throws IOException { int t = read.intNext(); while (t-- > 0) { int n = read.intNext(), m = read.intNext(); int[] inColors = iArr(n), finColors = iArr(n); for (int i = 0; i < n; i++) { inColors[i] = read.intNext(); } Map<Integer, Integer> fin = new HashMap<>(); for (int i = 0; i < n; i++) { finColors[i] = read.intNext(); fin.put(finColors[i], i); } int[] painters = iArr(m); for (int i = 0; i < m; i++) { painters[i] = read.intNext(); } Map<Integer, ArrayDeque<Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (inColors[i] != finColors[i]) { ArrayDeque<Integer> curr = map.getOrDefault(finColors[i], new ArrayDeque()); curr.add(i); map.put(finColors[i], curr); } } int lastPainter = -1; if (map.containsKey(painters[painters.length - 1])) { lastPainter = map.get(painters[painters.length - 1]).pollLast(); if (map.get(painters[painters.length - 1]).size() == 0) { map.remove(painters[painters.length -1 ]); } } else { for (int i = 0; i < n; i++) { if (painters[painters.length - 1] == finColors[i]) { lastPainter = i; break; } } } if (lastPainter == -1) { println("NO"); } else { int[] ans = iArr(m); ans[m - 1] = lastPainter; for (int i = 0; i < m - 1; i++) { if (map.containsKey(painters[i])) { ArrayDeque<Integer> curr = map.get(painters[i]); ans[i] = curr.pollLast(); if (curr.size() == 0) { map.remove(painters[i]); } } else { ans[i] = lastPainter; } } if (map.size() > 0) { println("NO"); } else { println("Yes"); for (int num : ans) { print((num + 1) + " "); } println(" "); } } } } /************************************************************************************************************************************************/ public static void main(String[] args) throws IOException { new Thread(null, new FencePainting(), "1").start(); } static PrintWriter out = new PrintWriter(System.out); static Reader read = new Reader(); static StringBuilder sbr = new StringBuilder(); static int mod = (int) 1e9 + 7; static int dmax = Integer.MAX_VALUE; static long lmax = Long.MAX_VALUE; static int dmin = Integer.MIN_VALUE; static long lmin = Long.MIN_VALUE; @Override public void run() { try { println(" "); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } static class Reader { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Reader() { in = System.in; } public int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public int intNext() throws IOException { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg * integer; } public double doubleNext() throws IOException { double doub = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else throw new InputMismatchException(); } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else throw new InputMismatchException(); } } return doub * neg; } public String read() throws IOException { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) n = scan(); while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } } static void shuffle(int[] aa) { int n = aa.length; Random rand = new Random(); for (int i = 1; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = aa[i]; aa[i] = aa[j]; aa[j] = tmp; } } static void shuffle(int[][] aa) { int n = aa.length; Random rand = new Random(); for (int i = 1; i < n; i++) { int j = rand.nextInt(i + 1); int first = aa[i][0]; int second = aa[i][1]; aa[i][0] = aa[j][0]; aa[i][1] = aa[j][1]; aa[j][0] = first; aa[j][1] = second; } } static final class Comparators { public static final Comparator<int[]> pairIntArr = (x, y) -> x[0] == y[0] ? compare(x[1], y[1]) : compare(x[0], y[0]); private static final int compare(final int x, final int y) { return Integer.compare(x, y); } } static void print(Object object) { out.print(object); } static void println(Object object) { out.println(object); } static int[] iArr(int len) { return new int[len]; } static long[] lArr(int len) { return new long[len]; } static long min(long a, long b) { return Math.min(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(Long a, Long b) { return Math.max(a, b); } static int max(int a, int b) { return Math.max(a, b); } }
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
cc12ba2d82400274379801a517c8c665
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.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codeforces { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(buf.readLine()); StringBuilder sb=new StringBuilder(); for(int i=0;i<t;i++) { String st1[] = (buf.readLine()).split(" "); int n = Integer.parseInt(st1[0]); int m = Integer.parseInt(st1[1]); String st2[]=(buf.readLine()).split(" "); String st3[]=(buf.readLine()).split(" "); int a[]=new int[n]; int b[]=new int[n]; for(int j=0;j<n;j++) { a[j]=Integer.parseInt(st2[j]); b[j]=Integer.parseInt(st3[j]); } String st4[]=(buf.readLine()).split(" "); int c[]=new int[m]; Stack<Integer> cnt[]=new Stack[n+1]; for(int j=0;j<m;j++) { c[j]=Integer.parseInt(st4[j]); } for(int j=0;j<cnt.length;j++) cnt[j]=new Stack<Integer>(); for(int j=0;j<n;j++) { if(a[j]!=b[j]) { cnt[b[j]].push(j+1); } } int flag=0,index=-1; for(int j=0;j<n;j++) { if(b[j]==c[m-1]) { flag=1; index=j+1; break; } } if(flag==1) { int ans[] = new int[m]; Arrays.fill(ans, -1); for (int j = 0; j < m-1; j++) { if (cnt[c[j]].isEmpty() == false) ans[j] = cnt[c[j]].pop(); } if(cnt[c[m-1]].isEmpty()==false) { ans[m-1]=cnt[c[m-1]].pop(); index=ans[m-1]; } for(int j=0;j<m;j++) { if(ans[j]==-1) ans[j]=index; } int f=0; for(int j=0;j<cnt.length;j++) { if(cnt[j].size()!=0) { f=1; break; } } if(f==0) { sb.append("YES"+"\n"); for(int j=0;j<m;j++) sb.append(ans[j]+" "); sb.append("\n"); } else sb.append("NO"+"\n"); } else sb.append("NO"+"\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
9e3bd448c83326c2ccfd904e9fc1bab0
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 File { public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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()); } } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = sc.nextInt(); for (int t = 1; t <= T; t++) { int n = sc.nextInt(); int m = sc.nextInt(); int[] needed = new int[n+1]; HashMap<Integer, LinkedList<Integer>> map = new HashMap<Integer, LinkedList<Integer>>(); int[] initial = new int[n]; for (int i = 0; i < n; i++) { initial[i] = sc.nextInt(); } int[] target = new int[n]; HashSet<Integer> colorSet = new HashSet<Integer>(); for (int i = 0; i < n; i++) { target[i] = sc.nextInt(); colorSet.add(target[i]); if (!map.containsKey(target[i])) { map.put(target[i], new LinkedList<Integer>()); } if (target[i] == initial[i]) { map.get(target[i]).addFirst(i); } else { map.get(target[i]).addLast(i); } } int[] colors = new int[m]; for (int i = 0; i < m; i++) { colors[i] = sc.nextInt(); } int lastColor = colors[m-1]; boolean isPossible = true; int[] res = new int[m]; if (!map.containsKey(lastColor)) { isPossible = false; } else { // This will be the index where all wrong colors go. int replaceIndex = -1; // Try to fill the answer. for (int i = 0; i < m; i++) { int c = colors[i]; if (!map.containsKey(c)) { res[i] = -1; } else { int index = map.get(c).getLast(); res[i] = index + 1; initial[index] = c; if (map.get(c).size() > 1) { map.get(c).removeLast(); } if (c == lastColor) { replaceIndex = index; } } } for (int i = 0; i < m; i++) { if (res[i] == -1) { res[i] = replaceIndex + 1; } } // Check if correct result. for (int i = 0; i < n; i++) { if (initial[i] != target[i]) { isPossible = false; break; } } } if (isPossible) { out.println("YES"); for (int val : res) { out.print(val + " "); } 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
7bb7fa2e8b187a2fac3ac5bc729b3b6a
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 second { private static final Scanner scanner = new Scanner(System.in); public static void main(String [] args) { int t = scanner.nextInt(); for(int i=0; i<t; i++) { int n = scanner.nextInt(); int k = scanner.nextInt(); int[] a = new int[n]; int[] b = new int[n]; int[] p = new int[k]; for (int j = 0; j < n; j++) { a[j] = scanner.nextInt(); } for (int j = 0; j < n; j++) { b[j] = scanner.nextInt(); } for (int j = 0; j < k; j++) { p[j] = scanner.nextInt(); } HashMap<Integer, ArrayList<Integer>> need_paint = new HashMap<Integer, ArrayList<Integer>>(); for (int j = 0; j < n; j++) { if (a[j] != b[j]) { ArrayList<Integer> val = need_paint.getOrDefault(b[j], new ArrayList<Integer>()); val.add(j); need_paint.put(b[j], val); } } int last = -1; if(need_paint.containsKey(p[k-1])) { ArrayList<Integer> val = need_paint.get(p[k-1]); last = val.get(val.size() -1); val.remove(val.size() -1); if(val.size() > 0) { need_paint.put(p[k-1], val); }else { need_paint.remove(p[k-1]); } }else { for(int j=0; j<n; j++) { if(b[j] == p[k-1]) { last = j; break; } } } if(last == -1) { System.out.println("NO"); }else { int [] ans = new int[k]; ans[k-1] = last; for(int j= 0; j<k-1;j++) { if(need_paint.containsKey(p[j])) { ArrayList<Integer> val = need_paint.get(p[j]); int last1 = val.get(val.size() -1); val.remove(val.size() -1); if(val.size() > 0) { need_paint.put(p[j], val); }else { need_paint.remove(p[j]); } ans[j] = last1; }else { ans[j] = last; } } if(need_paint.size() > 0) { System.out.println("NO"); }else { System.out.println("YES"); for (int j = 0; j < ans.length; j++) { System.out.print((ans[j] + 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
37efdeaff4308c3c4ea436c096049641
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
060099800206c7cf490c00d7019605be
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 eround101; import java.util.*; import java.io.*; import java.lang.*; import java.util.StringTokenizer; public class C101 { static HritikScanner sc = new HritikScanner(); static PrintWriter pw = new PrintWriter(System.out,true); final static int MOD = (int) (1e9 + 7); static int[] count = new int[1000001]; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) solve(); } static void solve(){ int n = ni(); int m = ni(); long[] arr = new long[n]; ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for(int i = 0; i < n; i++) { arr[i] = nl(); list.add(new ArrayList<Integer>()); } for(int i = 0; i <= n; i++) { list.add(new ArrayList<Integer>()); } int[] brr = new int[n]; for(int i = 0; i < n; i++) { brr[i] = ni(); } int[] crr = new int[m]; for(int i = 0; i < m; i++) { crr[i] = ni(); } for(int i = 0; i < n; i++) { if(arr[i] != brr[i]) { list.get(brr[i]).add(i); } } int last = -1; if(list.get(crr[m-1]).size() > 0) { last = list.get(crr[m-1]).get(list.get(crr[m-1]).size() - 1); list.get(crr[m-1]).remove(list.get(crr[m-1]).size() - 1); } else { for(int i = 0; i < n; i++) { if(brr[i] == crr[m-1]) { last = i; break; } } } // pl("last " + last); if(last == -1) { pl("NO"); return; } int[] ans = new int[m]; ans[m-1] = last; for(int i = 0; i < m-1; i++) { if(list.get(crr[i]).size() > 0) { ans[i] = list.get(crr[i]).get(list.get(crr[i]).size() - 1); list.get(crr[i]).remove(list.get(crr[i]).size() - 1); } else { ans[i] = last; } } for(int i = 0; i<= n; i++) { if(list.get(i).size() > 0) { pl("NO"); return; } } pl("YES"); for(int i = 0; i< m; i++) { p((ans[i]+1) + " "); } pl(); } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for(Object o : arr) p(o); pl(); } static void pa(int arr[]) { for(int o : arr) p(o); pl(); } static void pa(long arr[]) { for(long o : arr) p(o); pl(); } static void pa(double arr[]) { for(double o : arr) p(o); pl(); } static void pa( char arr[]) { for(char o : arr) p(o); pl(); } static void pa(List list) { for(Object o : list) p(o); pl(); } static void pa(Object[][] arr) { for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(int[][] arr) { for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(long[][] arr) { for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(char[][] arr) { for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(double[][] arr) { for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { 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 Pair implements Comparable<Pair> { int h, w, index; Pair(int h, int w) { this.h = h; this.w = w; } public int compareTo(Pair A) { if (A.h == this.h) { return this.w - A.w; } return this.h - A.h; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } ////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean isPowerOfTwo(long n) { if(n==0) return false; return (long)(Math.ceil((Math.log(n) / Math.log(2)))) == (long)(Math.floor(((Math.log(n) / Math.log(2))))); } public static long getFact(int n){ long ans = 1; while (n > 0){ ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow){ if (pow == 0) return 1; long temp = pow(n, pow/2)%MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) temp *= n; temp %= MOD; return temp; } public static long nCr(int n, int r){ long ans = 1; int temp = n-r; while (n > temp){ ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r)%MOD, MOD-2)%MOD; ans %= MOD; 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
045a7120afd51484812fbb09d397722f
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(); int tt = 1; 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)); } // if (!map.containsKey(brr[i])) 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 (tt == 18) { // System.out.println(n+" "+m); // for (long i : arr) { // System.out.print(i + " "); // } // System.out.println(); // for (long i : brr) { // System.out.print(i + " "); // } // System.out.println(); // for (long i : col) { // System.out.print(i + " "); // } // System.out.println(); // } 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]; } } } } // for(long i : arr) { // System.out.print(i+" "); // } // System.out.println(); // tt += 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
35e4a66545e80746252b4d023ecbdfeb
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.lang.*; public class Rextester{ static void printList(LinkedList[] list){ for(int i=0;i<5;i++){ System.out.print(i+"--"); for(int j=0;j<list[i].size();j++){ System.out.print(list[i].get(j)+" "); } System.out.println(); } } public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuffer sb = new StringBuffer(); while(t-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); StringTokenizer st1 = new StringTokenizer(br.readLine()); StringTokenizer st2 = new StringTokenizer(br.readLine()); StringTokenizer st3 = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[][] planks = new int[n][2]; LinkedList<Integer>[] list = new LinkedList[n+1]; for(int i=0;i<list.length;i++){ list[i]=new LinkedList<Integer>(); } int[] neutral = new int[n+1]; Arrays.fill(neutral,-1); for(int i=0;i<n;i++){ planks[i][0]=Integer.parseInt(st1.nextToken()); planks[i][1]=Integer.parseInt(st2.nextToken()); if(planks[i][0]!=planks[i][1]){ list[planks[i][1]].add(i); } else{ neutral[planks[i][1]]=i; } } //printList(list); int[] painters = new int[m]; for(int i=0;i<m;i++){ painters[i]=Integer.parseInt(st3.nextToken()); } boolean possible = true; int victimPlank = -1; int[] ans = new int[m]; for(int i=m-1;i>=0;i--){ if(list[painters[i]].size()==0){ if(victimPlank == -1 && neutral[painters[i]]==-1){ possible = false; break; } else{ if(neutral[painters[i]]!=-1){ ans[i]=neutral[painters[i]]; victimPlank = neutral[painters[i]]; } else{ ans[i]=victimPlank; } } } else{ Integer z = (Integer)list[painters[i]].pop(); ans[i]=z; victimPlank = z; if(neutral[painters[i]]==-1){ neutral[painters[i]]=z; } } } for(int i=0;i<list.length;i++){ if(list[i].size()!=0){ possible = false; } } if(possible){ sb.append("YES\n"); for(int i=0;i<m;i++){ sb.append(ans[i]+1).append(" "); } sb.append("\n"); } else{ sb.append("NO\n"); } } br.close(); 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
32268dae7a99eb179ff40c5d7ac3454d
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 C { BufferedReader input; BufferedWriter output; StringTokenizer st; // My Solution void solve() throws IOException { int t = getInt(); while (t--!=0){ int n = getInt(); int m = getInt(); int[] initial = getInts(n); int[] target = getInts(n); int[] painter = getInts(m); HashMap<Integer, LinkedList<Integer>> planksToBePainted = new HashMap<>(); HashMap<Integer, Integer> correctlyPainted = new HashMap<>(); for (int i = 0; i < n; i++) { if(initial[i]!=target[i]){ LinkedList<Integer> list; if(planksToBePainted.containsKey(target[i])) list = planksToBePainted.get(target[i]); else list = new LinkedList<>(); list.add(i); planksToBePainted.put(target[i], list); }else correctlyPainted.put(target[i], i); } int lastPainter = painter[m-1]; if(!correctlyPainted.containsKey(lastPainter) && !planksToBePainted.containsKey(lastPainter)) { print("NO"); } else { HashMap<Integer, Integer> paintersFreq = new HashMap<>(); for(int p : painter){ int freq = 0; if(paintersFreq.containsKey(p)) { freq = paintersFreq.get(p); } freq++; paintersFreq.put(p, freq); } boolean ans = true; for(int paint : planksToBePainted.keySet()){ LinkedList<Integer> list = planksToBePainted.get(paint); int freq = paintersFreq.getOrDefault(paint, 0); if(freq<list.size()){ ans = false; print("NO"); break; } } if(ans){ print("YES"); nextLine(); int backupOp = -1; if(planksToBePainted.containsKey(lastPainter)){ LinkedList<Integer> list = planksToBePainted.get(lastPainter); backupOp = list.peekLast()+1; list.removeLast(); if(list.isEmpty()) planksToBePainted.remove(lastPainter); } else { backupOp = correctlyPainted.get(lastPainter)+1; } for (int i = 0; i < m; i++) { if(i==m-1){ print(backupOp); break; } if(planksToBePainted.containsKey(painter[i])){ LinkedList<Integer> list = planksToBePainted.get(painter[i]); int index = list.peekLast(); print((index+1)+" "); list.removeLast(); if(list.isEmpty()) planksToBePainted.remove(painter[i]); }else { print(backupOp+" "); } } } } nextLine(); } } // Some basic functions int min(int...i){ int min = Integer.MAX_VALUE; for (int value : i) min = Math.min(min, value); return min; } int max(int...i){ int max = Integer.MIN_VALUE; for (int value : i) max = Math.max(max, value); return max; } // Printing stuff void print(int... i) throws IOException { for (int value : i) output.write(value + " "); } void print(long... l) throws IOException { for (long value : l) output.write(value + " "); } void print(String... s) throws IOException { for (String value : s) output.write(value); } void nextLine() throws IOException { output.write("\n"); } // Taking Inputs int[][] getIntMat(int n, int m) throws IOException { int[][] mat = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) mat[i][j] = getInt(); return mat; } char[][] getCharMat(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) { String s = input.readLine(); for (int j = 0; j < m; j++) mat[i][j] = s.charAt(j); } return mat; } int getInt() throws IOException { if(st ==null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return Integer.parseInt(st.nextToken()); } int[] getInts(int n) throws IOException { int[] a = new int[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); return a; } long getLong() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return Long.parseLong(st.nextToken()); } long[] getLongs(int n) throws IOException { long[] a = new long[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i = 0; i < n; i++) a[i] = Long.parseLong(st.nextToken()); return a; } // Checks whether the code is running on OnlineJudge or LocalSystem boolean isOnlineJudge() { if (System.getProperty("ONLINE_JUDGE") != null) return true; try { return System.getProperty("LOCAL")==null; } catch (Exception e) { return true; } } // Handling CodeExecution public static void main(String[] args) throws Exception { new C().run(); } void run() throws IOException { // Defining Input Streams if (isOnlineJudge()) { input = new BufferedReader(new InputStreamReader(System.in)); output = new BufferedWriter(new OutputStreamWriter(System.out)); } else { input = new BufferedReader(new FileReader("input.txt")); output = new BufferedWriter(new FileWriter("output.txt")); } // Running Logic solve(); output.flush(); // Run example test cases if (!isOnlineJudge()) { BufferedReader output = new BufferedReader(new FileReader("output.txt")); BufferedReader answer = new BufferedReader(new FileReader("answer.txt")); StringBuilder outFile = new StringBuilder(); StringBuilder ansFile = new StringBuilder(); String temp; while ((temp = output.readLine()) != null) outFile.append(temp.trim()); while ((temp = answer.readLine()) != null) ansFile.append(temp.trim()); if (outFile.toString().equals(ansFile.toString())) System.out.println("Test Cases Passed!!!"); else System.out.println("Failed..."); } } }
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
81dffd614d5ffc33a2fa6f50af731bd6
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 Himanshu **/ import java.util.*; import java.io.*; import java.math.*; public class C1481 { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int t = s.i(); while (t-- > 0) { int n = s.i() , m = s.i(); int [] a = s.arr(n); int [] b = s.arr(n); int [] c = s.arr(m); HashMap<Integer,ArrayList<Integer>> map = new HashMap<>(); for (int i=0;i<n;i++) { if (a[i] != b[i]) { ArrayList<Integer> x ; if (map.containsKey(b[i])) { x = map.get(b[i]); } else x = new ArrayList<>(); x.add(i+1); map.put(b[i],x); } } int temp = -1; for (int i=0;i<n;i++) { if (b[i] == c[m-1]) { temp = i+1; break; } } if (temp == -1) { out.println("NO"); continue; } int [] ans = new int[m]; if (map.containsKey(c[m-1])) { ArrayList<Integer> x = map.get(c[m-1]); if (x.size() == 0) { ans[m-1] = temp; map.remove(c[m-1]); } else { int val = x.get(x.size()-1); ans[m-1] = val; x.remove(x.size()-1); map.remove(c[m-1]); if (x.size() != 0) map.put(c[m-1],x); temp = val; } } else ans[m-1] = temp; for (int i=m-2;i>=0;i--) { if (map.containsKey(c[i])) { ArrayList<Integer> x = map.get(c[i]); if (x.size() == 0) { ans[i] = temp; map.remove(c[i]); } else { int val = x.get(x.size()-1); ans[i] = val; x.remove(x.size()-1); map.remove(c[i]); if (x.size() != 0) map.put(c[i],x); } } else { ans[i] = temp; } } if (map.size() == 0) { out.println("YES"); for (int x : ans) out.print(x + " "); out.println(); } else out.println("NO"); } out.flush(); } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar, 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 s() { 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 long l() { 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 i() { 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 double d() throws IOException { return Double.parseDouble(s()); } 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; } public int[] arr(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = i(); } return ret; } public long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } // static class pairLong implements Comparator<pairLong> { // long first, second; // // pairLong() { // } // // pairLong(long first, long second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pairLong p1, pairLong p2) { // if (p1.first == p2.first) { // if(p1.second > p2.second) return 1; // else return -1; // } // if(p1.first > p2.first) return 1; // else return -1; // } // } static class pair implements Comparator<pair> { int first, second , third; pair() { } pair(int first, int second , int third) { this.first = first; this.second = second; this.third = third; } @Override public int compare(pair p1, pair p2) { if (p1.first == p2.first) return p1.second - p2.second; return p1.first - p2.first; } } }
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
14ce5f4dc4eec94375c3608a1032c1c8
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 int mod=(int)1e9+7; public static void main(String[] args) throws IOException { PrintWriter out=new PrintWriter(System.out); Reader in=new Reader(System.in); int ts=1; ts=in.nextInt(); outer: while(ts-->0) { int n=in.nextInt(); int m=in.nextInt(); int a[]=in.readArray(n); int b[]=in.readArray(n); int c[]=in.readArray(m); ArrayDeque<Integer> [] aq1=new ArrayDeque[n+1]; for(int i=0; i<=n; ++i) aq1[i]=new ArrayDeque<>(); int ti=-1; for(int i=0; i<n; ++i) { if(c[m-1]==b[i]) { ti=i; if(a[i]!=b[i]) { break; } } } if(ti==-1) { out.println("NO"); continue outer; } for(int i=0; i<n; ++i) { if(a[i]!=b[i] && i!=ti) aq1[b[i]].addLast(i); } ArrayList<Integer> al=new ArrayList<>(); for(int i=0; i<m-1; ++i) { if(aq1[c[i]].size()==0) { al.add(ti+1); }else { int cur=aq1[c[i]].removeFirst(); al.add(cur+1); } } for(int i=0; i<aq1.length; ++i) { if(aq1[i].size()!=0) { out.println("NO"); continue outer; } } al.add(ti+1); out.println("YES"); for(int i=0; i<al.size(); ++i) { out.print(al.get(i)+" "); } out.println(); } out.close(); } static void sort(long a[]) { ArrayList<Long> al=new ArrayList<>(); for(long i: a) al.add(i); Collections.sort(al, Comparator.reverseOrder()); for(int i=0; i<a.length; ++i) a[i]=al.get(i); } static class Reader{ BufferedReader br; StringTokenizer to; Reader(InputStream stream){ br=new BufferedReader(new InputStreamReader(stream)); to=new StringTokenizer(""); } String nextLine() { String line=""; try { line=br.readLine(); }catch(IOException e) {}; return line; } String next() { while(!to.hasMoreTokens()) { try { to=new StringTokenizer(br.readLine()); }catch(IOException e) {} } return to.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int [] readArray(int n) { int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=nextInt(); return a; } long [] readLongArray(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 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
612c41555be3b519a8b61f466c8acd0f
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.codeforces; import java.io.PrintWriter; import java.util.*; import java.util.Scanner; public class Solution { //a public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); while (t-->0){ int n=input.nextInt(); int m=input.nextInt(); int a[]=new int[n]; for (int i = 0; i <n ; i++) { a[i]=input.nextInt(); } int b[]=new int[n]; for (int i = 0; i <n ; i++) { b[i]=input.nextInt(); } int c[]=new int[m]; for (int i = 0; i <m ; i++) { c[i]=input.nextInt(); } HashMap<Integer,ArrayList<Integer>> map=new HashMap<>(); int ptr[]=new int[n+1]; int fin[]=new int[n+1]; for (int i = 0; i <n ; i++) { if (a[i]!=b[i]){ if (map.containsKey(b[i])){ map.get(b[i]).add(i+1); }else{ ArrayList<Integer> temp=new ArrayList<>(); temp.add(i+1); map.put(b[i],temp); } }else { fin[b[i]] = i + 1; } } int ans[]=new int[m]; int sup=-1; boolean pos=true; for (int i = m-1; i >=0; i--) { int curr=c[i]; if (map.containsKey(curr)){ //required int j=ptr[curr]; ArrayList<Integer> temp=map.get(curr); if (temp.size()==j){ //all fixed ans[i]=temp.get(0); }else { //fix it sup=temp.get(0); a[temp.get(j)-1]=b[temp.get(j)-1]; ans[i]=temp.get(j); ptr[curr]++; } }else { if (sup==-1){ if (fin[curr]!=0){ //color not required but present in b ans[i]=fin[curr]; sup=fin[curr]; }else { pos=false; break; } }else { ans[i]=sup; } } } for (int i = 0; i <n ; i++) { if (a[i]!=b[i]){ pos=false; break; } } if (!pos){ System.out.println("NO"); }else { 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 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
dee820e1e59849c523d150f877c6725c
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 SolutionC extends Thread { 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; } } private static final FastReader scanner = new FastReader(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null, new SolutionC(), "Main", 1 << 26).start(); } public void run() { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(); } out.close(); } private static void solve() { int n = scanner.nextInt(); int m = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = scanner.nextInt(); } int[] c = new int[m]; int[] colorsHaving = new int[n+1]; for (int i = 0; i < m; i++) { c[i] = scanner.nextInt(); colorsHaving[c[i]]++; } int[] colorsNeeded = new int[n+1]; boolean[] isColorNeeded = new boolean[n+1]; int amountWrong = 0; int[] randomIndex = new int[n+1]; List<Integer>[] wrongPlanksIndex = new List[n+1]; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { colorsNeeded[b[i]]++; amountWrong++; if (wrongPlanksIndex[b[i]] == null) { wrongPlanksIndex[b[i]] = new ArrayList<>(); } wrongPlanksIndex[b[i]].add(i); } isColorNeeded[b[i]] = true; randomIndex[b[i]] = i; } int[] output = new int[m]; List<Integer> indicesDoneWrong = new ArrayList<>(); for (int i = 0; i < m; i++) { if (isColorNeeded[c[i]]) { int index; if (colorsNeeded[c[i]] > 0) { colorsNeeded[c[i]]--; amountWrong--; index = wrongPlanksIndex[c[i]].get(colorsNeeded[c[i]]); } else { index = randomIndex[c[i]]; } output[i] = index; for (int indexWrong: indicesDoneWrong) { output[indexWrong] = index; } indicesDoneWrong.clear(); } else { indicesDoneWrong.add(i); } } if (indicesDoneWrong.size() == 0 && amountWrong == 0) { out.println("YES"); StringBuilder s = new StringBuilder(); for (int i: output) { s.append(i + 1) .append(" "); } out.println(s); } else { out.println("NO"); } } //REMINDERS: //- CHECK FOR INTEGER-OVERFLOW BEFORE SUBMITTING //- CAN U BRUTEFORCE OVER SOMETHING, TO MAKE IT EASIER TO CALCULATE THE SOLUTION }
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
a336de10cc014b0dc3bdd546577a6701
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.util.Arrays.*; import java.util.*; public class C { public Object solve () { int N = sc.nextInt(), M = sc.nextInt(); int [] A = dec(sc.nextInts()), B = dec(sc.nextInts()), C = dec(sc.nextInts()); int [] res = new int [M]; boolean ok = false; int K = C[M-1]; for (int i : rep(N)) if (B[i] == K && A[i] != K) { fill(res, i); A[i] = K; ok = true; break; } if (!ok) for (int i : rep(N)) if (B[i] == K) { fill(res, i); ok = true; break; } if (!ok) return "NO"; @SuppressWarnings("unchecked") Queue<Integer> [] Q = new Queue[N]; for (int i : rep(N)) Q[i] = new LinkedList<>(); for (int i : rep(N)) if (B[i] != A[i]) Q[B[i]].add(i); for (int j : rep(M-1)) { int c = C[j]; if (!Q[c].isEmpty()) { int i = res[j] = Q[c].poll(); A[i] = B[i]; } } for (int i : rep(N)) if (B[i] != A[i]) return "NO"; print("YES"); return inc(res); } private static final int CONTEST_TYPE = 2; private static void init () { } private static int [] dec (int ... A) { for (int i = 0; i < A.length; ++i) --A[i]; return A; } private static int [] inc (int ... A) { for (int i = 0; i < A.length; ++i) ++A[i]; return A; } private static int [] rep (int N) { return rep(0, N); } private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } //////////////////////////////////////////////////////////////////////////////////// OFF private static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static Object print (Object o, Object ... A) { IOUtils.print(o, A); return null; } private static class IOUtils { public static class MyScanner { public String next () { newLine(); return line[index++]; } public int nextInt () { return Integer.parseInt(next()); } public String nextLine () { line = null; return readLine(); } public String [] nextStrings () { return split(nextLine()); } public int[] nextInts () { return nextStream().mapToInt(Integer::parseInt).toArray(); } ////////////////////////////////////////////// private boolean eol () { return index == line.length; } private String readLine () { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine () { if (line == null || eol()) { line = split(readLine()); index = 0; } } private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); } private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim (String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(min(b.length(), delim.length())); } ////////////////////////////////////////////////////////////////////////////////// private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); private static void start () { if (t == 0) t = millis(); } private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) f.accept(java.lang.reflect.Array.get(o, i)); } else if (o instanceof Iterable<?>) ((Iterable<?>)o).forEach(f::accept); else g.accept(o instanceof Double ? formatter.format(o) : o); } private static void append (final StringBuilder b, Object o, final String delim) { append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print (Object o, Object ... A) { String res = build(o, A); if (DEBUG == 2) err(res, '(', time(), ')'); if (res.length() > 0) pw.println(res); if (DEBUG == 1) { pw.flush(); System.out.flush(); } } private static void err (Object o, Object ... A) { System.err.println(build(o, A)); } private static int DEBUG; private static void exit () { String end = "------------------" + System.lineSeparator() + time(); switch(DEBUG) { case 1: print(end); break; case 2: err(end); break; } IOUtils.pw.close(); System.out.flush(); System.exit(0); } private static long t; private static long millis () { return System.currentTimeMillis(); } private static String time () { return "Time: " + (millis() - t) / 1000.0; } private static void run (int N) { try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); } catch (Throwable t) {} for (int n = 1; n <= N; ++n) { Object res = new C().solve(); if (res != null) { @SuppressWarnings("all") Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res; print(o); } } exit(); } } //////////////////////////////////////////////////////////////////////////////////// public static void main (String[] args) { init(); @SuppressWarnings("all") int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt(); IOUtils.run(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
947af6c134eec9afb622738d136617a8
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.jdi.IntegerValue; import com.sun.jdi.connect.Connector; import javax.lang.model.type.IntersectionType; import javax.security.auth.login.AccountNotFoundException; import javax.swing.*; import java.sql.Array; import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static FastReader sc = new FastReader(); static long mod = (int)1e9+7; public static void main (String[] args) throws java.lang.Exception { int t = sc.nextInt(); while(t-->0){ solve(); } } public static void solve() { int n = i(); int m = i(); int[] arr = new int[n]; int[] brr = new int[n]; HashMap<Integer, HashSet<Integer>> has = new HashMap<>(); for(int i = 0;i<n;++i){ arr[i]=i(); if(has.containsKey(arr[i])){ has.get(arr[i]).add(i); }else{ HashSet<Integer> list = new HashSet<>(); list.add(i); has.put(arr[i], list); } } HashMap<Integer, ArrayList<Integer>> require = new HashMap<>(); for(int i = 0;i<n;++i){ brr[i]=i(); if(brr[i]!=arr[i]){ if(require.containsKey(brr[i])){ require.get(brr[i]).add(i); }else{ require.put(brr[i], new ArrayList<>()); require.get(brr[i]).add(i); } } } int[] ans = new int[m]; ArrayList<Integer> redundant = new ArrayList<>(); for(int i = 0;i<m;++i){ int colour = i(); if(require.containsKey(colour)){ ArrayList<Integer> list = require.get(colour); ans[i] = list.get(list.size()-1); if(redundant.size()!=0){ for(int el : redundant){ ans[el] = ans[i]; } redundant = new ArrayList<>(); } int ind = ans[i]; int prevcol = arr[ind]; if(has.containsKey(colour)){ has.get(colour).add(ans[i]); }else{ HashSet<Integer> temp = new HashSet<>(); temp.add(ans[i]); has.put(colour, temp); } arr[ans[i]]=colour; has.get(prevcol).remove(ind); if(has.get(prevcol).size()==0){ has.remove(prevcol); } list.remove(list.size()-1); if(list.size()==0){ require.remove(colour); } }else{ if(has.containsKey(colour)){ ans[i] = has.get(colour).iterator().next(); if(redundant.size()!=0){ for(int el : redundant){ ans[el] = ans[i]; } redundant = new ArrayList<>(); } }else{ redundant.add(i); } } } if(redundant.size()!=0){ out.println("NO");out.flush();return; } for(int i = 0;i<n;++i){ if(arr[i]!=brr[i]){ out.println("NO");out.flush();return; } } out.println("YES"); for(int el : ans){ out.print((el+1) + " "); } out.println(); out.flush(); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Pair implements Comparable<Pair>{ int ht;int ind; Pair(int a, int ch){ this.ht=a; this.ind = ch; } public int compareTo(Pair o) { return this.ht-o.ht; } public String toString(){ return "" ; } } static int csb(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } 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
6bbe3beee4dd7ef9098a38944114a5b5
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.lang.*; import java.util.*; public class Main { public static int mod = (int)1e9 + 7; // **** -----> Disjoint Set Union(DSU) Start ********** public static int findPar(int node, int[] parent) { if (parent[node] == node) return node; return parent[node] = findPar(parent[node], parent); } public static boolean union(int u, int v, int[] rank, int[] parent) { u = findPar(u, parent); v = findPar(v, parent); if(u == v) return false; if (rank[u] < rank[v]) parent[u] = v; else if (rank[u] > rank[v]) parent[v] = u; else { parent[u] = v; rank[v]++; } return true; } // **** DSU Ends *********** //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + " -> " + b; } } public static String toBinary(int n) {return Integer.toBinaryString(n);} public static boolean isPalindrome(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;} public static boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;} public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;} 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 == 0)return a;return gcd(b, a % b);} public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}} public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);} public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);} public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);} public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;} public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];} public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;} public static String reverse(String str) {StringBuilder sb = new StringBuilder(str);sb.reverse();return sb.toString();} public static boolean isPrime(int 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 * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false;return true;} public static int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;} public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;} static int lower_bound(int array[], int low, int high, int key){ int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) high = mid; else low = mid + 1; } if (low < array.length && array[low] < key) low++; return low; } static long fastPower(long a, long b, long mod){ long ans = 1 ; while (b > 0) { if ((b&1) != 0) ans = (ans%mod * a%mod) %mod; b >>= 1 ; a = (a%mod * a%mod)%mod ; } return ans ; } /********************************* Start Here ***********************************/ // int mod = 1000000007; static HashMap<String, Integer> map; static Set<Integer> set1, set2; static boolean[] vis; public static void main(String[] args) throws java.lang.Exception { if (System.getProperty("ONLINE_JUDGE") == null) { PrintStream ps = new PrintStream(new File("output.txt")); System.setOut(ps); } FastScanner sc = new FastScanner("input.txt"); StringBuilder result = new StringBuilder(); // sieveOfEratosthenes(1000000); int T = sc.nextInt(); // int T = 1; for(int test = 1; test <= T; test++){ int n = sc.nextInt(), m = sc.nextInt(); int[] a = sc.readArray(n); int[] b = sc.readArray(n); int[] c = sc.readArray(m); HashMap<Integer,Integer> backup = new HashMap<>(); HashMap<Integer,HashSet<Integer>> notSatisfied = new HashMap<>(); HashSet<Integer> satisfied = new HashSet<>(); for(int i = 0;i<n;i++){ if(a[i]==b[i]){ backup.put(b[i],i); } else{ if(!notSatisfied.containsKey(b[i])){ notSatisfied.put(b[i],new HashSet<>()); } notSatisfied.get(b[i]).add(i); } } int[] ans = new int[m]; boolean ok = true; for(int i = m-1;i>=0;i--){ if(notSatisfied.containsKey(c[i])){ HashSet<Integer> temp = notSatisfied.get(c[i]); int idx = -1; for(int k : temp){ idx = k; break; } ans[i] = idx; temp.remove(idx); if(temp.size()==0) notSatisfied.remove(c[i]); } else{ if(backup.containsKey(c[i])) ans[i] = backup.get(c[i]); else if(satisfied.isEmpty()) { ok = false; break; } else { int idx = -1; for(int k : satisfied){ idx = k; break; } ans[i] = idx; } } satisfied.add(ans[i]); } if(!ok || notSatisfied.size() > 0) result.append("NO"); else { result.append("YES\n"); for(int i = 0; i < m; i++) result.append(ans[i]+1+" "); } result.append("\n"); } System.out.println(result); System.out.close(); } public static long lcm(long a, long b){ if(a>b) return a/gcd(b,a) * b; return b/gcd(a,b) * a; } // static void sieveOfEratosthenes(int n){ // boolean prime[] = new boolean[n + 1]; // for (int i = 0; i <= n; i++) // prime[i] = true; // for (int p = 2; p * p <= n; p++){ // if (prime[p] == true){ // for (int i = p * p; i <= n; i += p) // prime[i] = false; // } // } // for (int i = 2; i <= n; i++){ // if (prime[i] == true) // set.add(i); // } // } // public int[] ncr(){ // int ncr[][] = new int[1001][1001]; // for (int i = 0; i < 1001; i++) { // ncr[i][0] = 1; // } // for (int i = 1; i < 1001; i++) { // for (int j = 1; j <= i; j++) { // if (i == j) { // ncr[i][j] = 1; // continue; // } // ncr[i][j] = (ncr[i - 1][j]+ncr[i - 1][j - 1])%mod; // } // } // } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st == null || !st.hasMoreTokens()) { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken("\n"); } public String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } int nextInt() { return Integer.parseInt(nextToken()); } String next() { return nextToken(); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] read2dArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } long[][] read2dlongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = 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 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
4f8e84b3bbce10c077a602417f5c2044
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 Solution { static long[] fac; static int m = (int)1e9+7; static int c = 1; // static int[] x = {1,-1,0,0}; // static int[] y = {0,0,1,-1}; // static int cycle_node; public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); // Do not delete this // //Uncomment this before using nCrModPFermat // fac = new long[200000 + 1]; // fac[0] = 1; // // for (int i = 1; i <= 200000; i++) // fac[i] = (fac[i - 1] * i % 1000000007); // long[] fact = factorial((int)1e6); int te = Reader.nextInt(); // int te = 1; while(te-->0){ int n = Reader.nextInt(); int m = Reader.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] = Reader.nextInt(); for(int i = 0;i<n;i++) b[i] = Reader.nextInt(); for(int i = 0;i<m;i++) c[i] = Reader.nextInt(); HashMap<Integer,Integer> backup = new HashMap<>(); HashMap<Integer,HashSet<Integer>> notSatisfied = new HashMap<>(); HashSet<Integer> satisfied = new HashSet<>(); for(int i = 0;i<n;i++){ if(a[i]==b[i]){ backup.put(b[i],i); } else{ if(!notSatisfied.containsKey(b[i])){ notSatisfied.put(b[i],new HashSet<>()); } notSatisfied.get(b[i]).add(i); } } int[] ans = new int[m]; boolean ok = true; for(int i = m-1;i>=0;i--){ if(notSatisfied.containsKey(c[i])){ HashSet<Integer> temp = notSatisfied.get(c[i]); int idx = -1; for(int k : temp){ idx = k; break; } ans[i] = idx; temp.remove(idx); // System.out.println(notSatisfied.get(c[i])); if(temp.size()==0){ notSatisfied.remove(c[i]); } // System.out.println(notSatisfied); } else{ if(backup.containsKey(c[i])){ ans[i] = backup.get(c[i]); } else if(satisfied.isEmpty()) { ok = false; break; } else { int idx = -1; for(int k : satisfied){ idx = k; break; } ans[i] = idx; } } satisfied.add(ans[i]); } if(ok && notSatisfied.size()==0){ output.write("YES\n"); for(int i = 0;i<m;i++){ output.write(ans[i]+1+" "); } output.write("\n"); } else output.write("NO\n"); } output.close(); } // Recursive function to return (x ^ n) % m static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } // Function to return the fraction modulo mod static long getFractionModulo(long a, long b) { long c = gcd(a, b); a = a / c; b = b / c; // (b ^ m-2) % m long d = modexp(b, m - 2); // Final answer long ans = ((a % m) * (d % m)) % m; return ans; } public static boolean isP(String n){ StringBuilder s1 = new StringBuilder(n); StringBuilder s2 = new StringBuilder(n); s2.reverse(); for(int i = 0;i<s1.length();i++){ if(s1.charAt(i)!=s2.charAt(i)) return false; } return true; } public static long[] factorial(int n){ long[] factorials = new long[n+1]; factorials[0] = 1; factorials[1] = 1; for(int i = 2;i<=n;i++){ factorials[i] = (factorials[i-1]*i)%1000000007; } return factorials; } public static long numOfBits(long n){ long ans = 0; while(n>0){ n = n & (n-1); ans++; } return ans; } public static long ceilOfFraction(long x, long y){ // ceil using integer division: ceil(x/y) = (x+y-1)/y // using double may go out of range. return (x+y-1)/y; } public static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns n^(-1) mod p public static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. public static long nCrModPFermat(int n, int r, int p) { if (n<r) return 0; if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long ncr(long n, long r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } public 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; (long) i * i <= n; i = i + 6){ if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } public static int powOf2JustSmallerThanN(int n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n ^ (n >> 1); } public static long merge(int[] arr, int[] aux, int low, int mid, int high) { int k = low, i = low, j = mid + 1; long inversionCount = 0; // while there are elements in the left and right runs while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { aux[k++] = arr[i++]; } else { aux[k++] = arr[j++]; inversionCount += (mid - i + 1); // NOTE } } // copy remaining elements while (i <= mid) { aux[k++] = arr[i++]; } /* no need to copy the second half (since the remaining items are already in their correct position in the temporary array) */ // copy back to the original array to reflect sorted order for (i = low; i <= high; i++) { arr[i] = aux[i]; } return inversionCount; } public static long mergesort(int[] arr, int[] aux, int low, int high) { if (high <= low) { // if run size <= 1 return 0; } int mid = (low + ((high - low) >> 1)); long inversionCount = 0; // recursively split runs into two halves until run size <= 1, // then merges them and return up the call chain // split/merge left half inversionCount += mergesort(arr, aux, low, mid); // split/merge right half inversionCount += mergesort(arr, aux, mid + 1, high); // merge the two half runs inversionCount += merge(arr, aux, low, mid, high); return inversionCount; } public static void reverseArray(int[] arr,int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } public 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){ if(a>b) return a/gcd(b,a) * b; return b/gcd(a,b) * a; } public static long largeExponentMod(long x,long y,long mod){ // computing (x^y) % mod x%=mod; long ans = 1; while(y>0){ if((y&1)==1){ ans = (ans*x)%mod; } x = (x*x)%mod; y = y >> 1; } return ans; } public static boolean[] numOfPrimesInRange(long L, long R){ boolean[] isPrime = new boolean[(int) (R-L+1)]; Arrays.fill(isPrime,true); long lim = (long) Math.sqrt(R); for (long i = 2; i <= lim; i++){ for (long j = Math.max(i * i, (L + i - 1) / i * i); j <= R; j += i){ isPrime[(int) (j - L)] = false; } } if (L == 1) isPrime[0] = false; return isPrime; } public static ArrayList<Long> primeFactors(long n){ ArrayList<Long> factorization = new ArrayList<>(); if(n%2==0){ factorization.add(2L); } while(n%2==0){ n/=2; } if(n%3==0){ factorization.add(3L); } while(n%3==0){ n/=3; } if(n%5==0){ factorization.add(5L); } while(n%5==0){ // factorization.add(5L); n/=5; } int[] increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (long d = 7; d * d <= n; d += increments[i++]) { if(n%d==0){ factorization.add(d); } while (n % d == 0) { // factorization.add(d); n /= d; } if (i == 8) i = 0; } if (n > 1) factorization.add(n); return factorization; } } class DSU { int[] size, parent; int n; public DSU(int n){ this.n = n; size = new int[n]; parent = new int[n]; for(int i = 0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int u){ if(parent[u]==u){ return u; } return parent[u] = find(parent[u]); } public void merge(int u, int v){ u = find(u); v = find(v); if(u!=v){ if(size[u]>size[v]){ parent[v] = u; size[u] += size[v]; } else{ parent[u] = v; size[v] += size[u]; } } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class SegTree{ int n; // array size // Max size of tree public int[] tree; public SegTree(int n){ this.n = n; tree = new int[4*n]; } // function to build the tree void update(int pos, int val, int s, int e, int treeIdx){ if(pos < s || pos > e){ return; } if(s == e){ tree[treeIdx] = val; return; } int mid = s + (e - s) / 2; update(pos, val, s, mid, 2 * treeIdx); update(pos, val, mid + 1, e, 2 * treeIdx + 1); tree[treeIdx] = tree[2 * treeIdx] + tree[2 * treeIdx + 1]; } void update(int pos, int val){ update(pos, val, 1, n, 1); } int query(int qs, int qe, int s, int e, int treeIdx){ if(qs <= s && qe >= e){ return tree[treeIdx]; } if(qs > e || qe < s){ return 0; } int mid = s + (e - s) / 2; int subQuery1 = query(qs, qe, s, mid, 2 * treeIdx); int subQuery2 = query(qs, qe, mid + 1, e, 2 * treeIdx + 1); int res = subQuery1 + subQuery2; return res; } int query(int l, int r){ return query(l, r, 1, n, 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
39a5525464610562cdec993f517d280c
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 MyScanner sc= new MyScanner(); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i<n; i++) { a[i] = sc.nextInt(); } int b[] = new int[n]; for(int i =0; i<n; i++) { b[i] = sc.nextInt(); } int p[] = new int[m]; for(int i =0; i<m; i++) { p[i] = sc.nextInt(); } TreeMap<Integer,LinkedList<Integer>> tm = new TreeMap<>(); for(int i =0; i<n; i++) { if(a[i]!=b[i]) { if(!tm.containsKey(b[i])) { tm.put(b[i],new LinkedList<>()); } tm.get(b[i]).add((i+1)); } } if(tm.keySet().isEmpty()) { HashMap<Integer,Integer> hm = new HashMap<>(); for(int i = 0; i<n; i++) { if(!hm.containsKey(b[i])) { hm.put(b[i], (i+1)); } } HashMap<Integer,Integer> res = new HashMap<>(); Stack<Integer> st = new Stack<>(); for(int i = 0; i<m; i++){ int paint = p[i]; if(!hm.containsKey(paint)) { st.add(i); } else { res.put(i,hm.get(paint)); while(!st.isEmpty()) { int ind= st.pop(); res.put(ind, res.get(i)); } } } if(st.isEmpty()) { pw.println("YES"); for(int i = 0; i<m; i++) { pw.print(res.get(i)+" "); } pw.println(); } else { pw.println("NO"); } } else { TreeMap<Integer,Integer> res = new TreeMap<>(); Stack<Integer> st = new Stack<>(); HashMap<Integer,Integer> hm = new HashMap<>(); for(int i = 0; i<n; i++) { if(a[i]==b[i]) { hm.put(a[i],i+1); } } for(int i =0; i<m; i++) { int paint = p[i]; if(tm.containsKey(paint)) { if(!hm.containsKey(paint)) { hm.put(paint, tm.get(paint).peekFirst()); } res.put(i, tm.get(paint).poll()); if(tm.get(paint).isEmpty()) { tm.remove(paint); } while(!st.isEmpty()) { int ind = st.pop(); res.put(ind, res.get(i)); } } else { if(hm.containsKey(paint)) { res.put(i, hm.get(paint)); while(!st.isEmpty()) { int ind = st.pop(); res.put(ind, res.get(i)); } } else { st.add(i); } } } if(st.isEmpty() && tm.keySet().isEmpty()) { pw.println("YES"); for(int i=0; i<m; i++) { pw.print(res.get(i)+" "); } pw.println(); } else { pw.println("NO"); } } } pw.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 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
ab4e1eb547c698526f4ac32f103b7538
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.util.Map.Entry; import java.math.*; public class Simple{ public static class Pair{ int x; long y; public Pair(int x,long y){ this.x = x; this.y = y; } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ 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(); int vis = -1; boolean bool = true; Map<Integer,HashSet<Integer>> map = new HashMap<>(); Map<Integer,Integer> hmap = new HashMap<>(); for(int i=0;i<n;i++){ if(a[i]!=b[i]){ if(map.get(b[i])==null)map.put(b[i], new HashSet<>()); map.get(b[i]).add(i); } else{ if(hmap.get(b[i])==null)hmap.put(b[i], i); } } ArrayList<Integer> ans = new ArrayList<>(); for(int i=m-1;i>=0;i--){ if(map.get(c[i])==null && vis==-1 && hmap.get(c[i])==null){ bool = false; break; } if(map.get(c[i])==null && vis==-1){ ans.add(hmap.get(c[i])); vis = hmap.get(c[i]); } else if(map.get(c[i])==null || map.get(c[i]).size()==0){ ans.add(vis); } else{ for(Integer x : map.get(c[i])){ vis = x; ans.add(x); a[x] = c[i]; map.get(c[i]).remove(x); break; } } } if(!bool){ System.out.println("NO"); } else{ // for(int i=0;i<n;i++){ // System.out.print(a[i]+" "); // } // System.out.println(); // for(int i=0;i<n;i++){ // System.out.print(b[i]+" "); // } // System.out.println(); for(int i=0;i<n;i++){ if(a[i]!=b[i]){ bool = false; break; } } if(bool){ System.out.println("YES"); for(int i=m-1;i>=0;i--){ System.out.print(ans.get(i)+1+" "); } System.out.println(); } else{ System.out.println("NO"); } } } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
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
4181a5de3e2aba702df364a23a1ad035
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 q1 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int tests = Integer.parseInt(br.readLine()); for (int test = 1; test <= tests; test++) { String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int m = Integer.parseInt(parts[1]); int[] a = new int[n]; parts = br.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(parts[i]); } int[] b = new int[n]; parts = br.readLine().split(" "); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(parts[i]); } int[] c = new int[m]; HashMap<Integer, ArrayList<Integer>> paint = new HashMap<>(); parts = br.readLine().split(" "); for (int i = 0; i < m; i++) { c[i] = Integer.parseInt(parts[i]); ArrayList<Integer> arr = paint.getOrDefault(c[i], new ArrayList<>()); arr.add(i); paint.put(c[i], arr); } int[] ans = new int[m]; boolean flag = true; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { if (paint.containsKey(b[i])) { ArrayList<Integer> arr = paint.get(b[i]); int idx = arr.remove(arr.size() - 1); ans[idx] = i + 1; a[i] = b[i]; if (arr.size() == 0) paint.remove(b[i]); } else { flag = false; break; } } } if (!flag) { out.write("NO\n"); continue; } if (ans[m - 1] == 0) { for (int i = 0; i < n; i++) { if (a[i] == c[m - 1]) { ans[m - 1] = i + 1; break; } } } if (ans[m - 1] == 0) { out.write("NO\n"); continue; } for (int i = 0; i < m; i++) { if (ans[i] == 0) ans[i] = ans[m - 1]; } out.write("YES\n"); for (int val : ans) out.write(val + " "); out.write("\n"); } 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
11abf9e9de397c98cb57ca43caf4e838
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 q1 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int tests = Integer.parseInt(br.readLine()); for (int test = 1; test <= tests; test++) { String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int m = Integer.parseInt(parts[1]); int[] a = new int[n]; parts = br.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(parts[i]); } int[] b = new int[n]; parts = br.readLine().split(" "); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(parts[i]); } HashMap<Integer, ArrayList<Integer>> paint = new HashMap<>(); parts = br.readLine().split(" "); for (int i = 0; i < m; i++) { int val = Integer.parseInt(parts[i]); ArrayList<Integer> arr = paint.getOrDefault(val, new ArrayList<>()); arr.add(i); paint.put(val, arr); } int used = 0, painted = 0; int[] ans = new int[m]; boolean flag = true; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { if (paint.containsKey(b[i])) { ArrayList<Integer> arr = paint.get(b[i]); int idx = arr.remove(arr.size() - 1); ans[idx] = i + 1; if (idx > used) { used = idx; painted = i + 1; } a[i] = b[i]; if (arr.size() == 0) paint.remove(b[i]); } else { flag = false; break; } } } if (!flag) { out.write("NO\n"); continue; } for (int i = 0; i < n; i++) { if (paint.containsKey(a[i])) { ArrayList<Integer> arr = paint.get(a[i]); if (arr.get(arr.size() - 1) >= used) { used = arr.get(arr.size() - 1); painted = i + 1; } } } for (int i = 0; i < m; i++) { if (ans[i] == 0) { if (i <= used) { ans[i] = painted; } else { flag = false; break; } } } if (!flag) { out.write("NO\n"); } else { out.write("YES\n"); for (int val : ans) out.write(val + " "); out.write("\n"); } } 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
d5288bbd4a464b743a6f971ed7e86c88
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.*; import java.util.*; public class c { static BufferedReader br; static long mod = 1000000000 + 7; static HashSet<Integer> p = new HashSet<>(); static boolean debug =true; // Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda public static void main(String[] args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); int tc = 1; tc = cinI(); PrintWriter pr = new PrintWriter(System.out); while (tc-- > 0) { int[] nm =readArray(2,0,0); int n =nm[0]; int m =nm[1]; int[] ar=readArray(n,0,0); int[] b=readArray(n,0,0); int[] c=readArray(m,0,0); for(int i=0;i<n;i++){ ar[i]-=1; b[i]-=1; } // for(int i=0;i<n;i++){ // pr.print(b[i]+" "); // } //pr.println("f"); for(int i=0;i<m;i++){ c[i]-=1; } int[] needToBeColored = new int[n]; int[] colored= new int[n]; Stack<Integer>[] ntc = new Stack[n]; Stack<Integer>[] col = new Stack[n]; int[] val = new int[m]; Arrays.fill(val,-1); for(int i=0;i<n;i++){ ntc[i]=new Stack<>(); col[i]=new Stack<>(); } for(int i=0;i<n;i++){ if(b[i]==ar[i]){ colored[b[i]]+=1; col[b[i]].push(i); } else{ needToBeColored[b[i]]+=1; ntc[b[i]].push(i); } } boolean ans=true; //int[] fi = new int[n]; // for(int i=0;i<m;i++){ if(needToBeColored[c[i]]>0){ needToBeColored[c[i]]-=1; colored[c[i]]+=1; int index =ntc[c[i]].pop(); col[c[i]].push(index); val[i]=index; ar[index]=c[i]; ans=true; continue; } if(colored[c[i]]>0){ ans=true; int index= col[c[i]].peek(); val[i]=index; ar[index]=c[i]; continue; } ans=false; } if(!ans){ pr.println("NO"); continue; } for(int i=0;i<m;i++){ if(val[i]==-1){ val[i]=val[m-1]; } } boolean ok=true; for(int i=0;i<n;i++){ if(ar[i]!=b[i]){ //pr.println(b[i]+" "); // pr.println(fi[i]+"xx "+b[i]); ok=false; break; } } if(!ok){ pr.println("NO"); continue; } else{ pr.println("YES"); for(int i=0;i<m;i++){ pr.print((val[i]+1)+" "); } pr.println(); } //pr.println("YES"); } pr.close(); } public static <E> void print(String var ,E e){ if(debug==true){ System.out.println(var +" "+e); } } private static long[] sort(long[] e){ ArrayList<Long> x=new ArrayList<>(); for(long c:e){ x.add(c); } Collections.sort(x); long[] y = new long[e.length]; for(int i=0;i<x.size();i++){ y[i]=x.get(i); } return y; } public static void printDp(long[][] dp) { int n = dp.length; for (int i = 0; i < n; i++) { for (int j = 0; j < dp[0].length; j++) { System.out.print(dp[i][j] + " "); } System.out.println(); } } private static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a % b, b); return gcd(a, b % a); } public static long min(long a, long b) { return Math.min(a, b); } public static int min(int a, int b) { return Math.min(a, b); } public static void sieve() { int[] pf = new int[100000000 + 1]; //0 prime //1 not prime pf[0] = 1; pf[1] = 1; for (int j = 2; j <= 10000; j++) { if (pf[j] == 0) { p.add(j); for (int k = j * j; k < pf.length; k += j) { pf[k] = 1; } } } } public static int[] readArray(int n, int x, int z) throws Exception { int[] arr = new int[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getI(ar[i - x]); } return arr; } public static long[] readArray(int n, int x) throws Exception { long[] arr = new long[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getL(ar[i - x]); } return arr; } public static void arrinit(String[] a, long[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Long.parseLong(a[i]); } } public static HashSet<Integer>[] Graph(int n, int edge, int directed) throws Exception { HashSet<Integer>[] tree = new HashSet[n]; for (int j = 0; j < edge; j++) { String[] uv = cinA(); int u = getI(uv[0]); int v = getI(uv[1]); if (directed == 0) { tree[v].add(u); } tree[u].add(v); } return tree; } public static void arrinit(String[] a, int[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Integer.parseInt(a[i]); } } static double findRoots(int a, int b, int c) { // If a is 0, then equation is not //quadratic, but linear int d = b * b - 4 * a * c; double sqrt_val = Math.sqrt(Math.abs(d)); // System.out.println("Roots are real and different \n"); return Math.max((double) (-b + sqrt_val) / (2 * a), (double) (-b - sqrt_val) / (2 * a)); } public static String cin() throws Exception { return br.readLine(); } public static String[] cinA() throws Exception { return br.readLine().split(" "); } public static String[] cinA(int x) throws Exception { return br.readLine().split(""); } public static String ToString(Long x) { return Long.toBinaryString(x); } public static void cout(String s) { System.out.println(s); } public static Integer cinI() throws Exception { return Integer.parseInt(br.readLine()); } public static int getI(String s) throws Exception { return Integer.parseInt(s); } public static long getL(String s) throws Exception { return Long.parseLong(s); } public static long max(long a, long b) { return Math.max(a, b); } public static int max(int a, int b) { return Math.max(a, b); } public static void coutI(int x) { System.out.println(String.valueOf(x)); } public static void coutI(long x) { System.out.println(String.valueOf(x)); } public static Long cinL() throws Exception { return Long.parseLong(br.readLine()); } public static void arrInit(String[] arr, int[] arr1) throws Exception { for (int i = 0; i < arr.length; i++) { arr1[i] = getI(arr[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
a6068501ed099df7f5be7222b320abb2
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.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; private static final String No = "No"; private static final String Yes = "Yes"; InputStream is; PrintWriter out; String INPUT = ""; private static long MOD = 1000000007; private static final int MAXN = 100000; void solve() { int T = ni(); for (int i = 0; i < T; i++) { solve(i); } } int N; ArrayDeque<Integer> m[]; ArrayDeque<Integer> o[]; void solve(int T) { N = ni(); int K = ni(); int a[] = na(N); int b[] = na(N); m = new ArrayDeque[N + 1]; o = new ArrayDeque[N + 1]; Arrays.setAll(m, i -> new ArrayDeque<Integer>()); Arrays.setAll(o, i -> new ArrayDeque<Integer>()); for (int i = 0; i < N; i++) { if (a[i] != b[i]) m[b[i]].add(i + 1); else o[b[i]].add(i + 1); } // tr(m); // tr(o); int ans[] = new int[K]; int ca[] = na(K); // tr(ca); for (int i = 0; i < K; i++) { int c = ca[i]; if (!m[c].isEmpty()) { ans[i] = m[c].poll(); o[c].addLast(ans[i]); } else if (!o[c].isEmpty()) { ans[i] = o[c].peek(); } else if (i == K - 1) { out.println(NO); return; } else { int lc = ca[K - 1]; if (!m[lc].isEmpty()) ans[i] = m[lc].peek(); else if (!o[lc].isEmpty()) ans[i] = o[lc].peek(); else { out.println(NO); return; } } } for (ArrayDeque<Integer> m0 : m) { if (!m0.isEmpty()) { out.println(NO); return; } } // tr(m); // tr(o); out.println(YES); for (int i : ans) out.print(i + " "); out.println(); } // a^b long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Integer[] na2(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Long[] nl2(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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
33cd1b4caf39ca3992910d84168314c1
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 { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int testCase = fs.nextInt(); outer : for (int tc = 0; tc < testCase; tc++) { int nPlank = fs.nextInt(), nPainter = fs.nextInt(); int[] color = fs.readArray(nPlank); int[] target = fs.readArray(nPlank); int[] painterColor = fs.readArray(nPainter); HashMap<Integer, TreeSet<Integer>> mp = new HashMap<>(); HashMap<Integer, Integer> same = new HashMap<>(); for(int i = 0; i < nPlank; ++i) { if(color[i] == target[i]) { same.put(target[i], i + 1); continue; } if(!mp.containsKey(target[i])) mp.put(target[i], new TreeSet<>()); mp.get(target[i]).add(i + 1); } int prev = -1; List<Integer> ansList = new ArrayList<>(); for(int i = nPainter - 1; i >= 0; --i) { if(!mp.containsKey(painterColor[i])) { if(prev == -1) { if(!same.containsKey(painterColor[i])) { System.out.println("NO"); continue outer; } else { prev = same.get(painterColor[i]); } } } else { TreeSet<Integer> indexes = mp.get(painterColor[i]); if(!indexes.isEmpty()) { prev = indexes.first(); indexes.remove(prev); } } ansList.add(prev); } for(TreeSet<Integer> remain : mp.values()) { if(!remain.isEmpty()) { System.out.println("NO"); continue outer; } } System.out.println("YES"); for(int i = ansList.size() - 1; i >= 0; --i) System.out.print(ansList.get(i) + " "); System.out.println(); } } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for(int i = 0; i < a.length; ++i) { int oi = r.nextInt(n), t = a[i]; a[i] = a[oi]; a[oi] = t; } Arrays.sort(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); } 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()); } } }
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
781f91b34e7b6955515101bad5cc9970
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.BigInteger; import java.util.*; public class C { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int yo = sc.nextInt(); outer: while(yo-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int[] a = sc.readArray(n); int[] b = sc.readArray(n); int[] c = sc.readArray(m); List<Queue<Integer>> needPainter = new ArrayList<>(n+1); for(int i = 0; i < n+1; i++) { needPainter.add(new LinkedList<>()); } for(int i = 0; i < n; i++) { if(a[i] != b[i]) { needPainter.get(b[i]).add(i+1); } } int last = -1; if(!needPainter.get(c[m-1]).isEmpty()) { last = needPainter.get(c[m-1]).peek(); needPainter.get(c[m-1]).poll(); } else { for(int i = 0; i < n; i++) { if(b[i] == c[m-1]) { last = i+1; break; } } } if(last == -1) { pw.println("NO"); continue outer; } int[] ans = new int[m]; ans[m-1] = last; for(int i = 0; i < m-1; i++) { if(needPainter.get(c[i]).size() > 0) { ans[i] = needPainter.get(c[i]).peek(); needPainter.get(c[i]).poll(); } else { ans[i] = last; } } for(int i = 1; i <= n; i++) { if(needPainter.get(i).size() > 0) { pw.println("NO"); continue outer; } } pw.println("YES"); for(int e : ans) { pw.print(e + " "); } pw.println(); } pw.close(); } static class Pair{ int x; int y; public Pair(int x, int y){ this.x = x; this.y = y; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n+1]; for(int i = 2; i <= n; i++) { if(isPrime[i]) continue; for(int j = 2*i; j <= n; j+=i) { isPrime[j] = true; } } return isPrime; } static int mod = 1000000007; static long pow(int a, long b) { if(b == 0) { return 1; } if(b == 1) { return a; } if(b%2 == 0) { long ans = pow(a,b/2); return ans*ans; } else { long ans = pow(a,(b-1)/2); return a * ans * ans; } } 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()); } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
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
52318488495cf07882b72a598beb1077
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.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Codeforces { 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 static void main(String args[]) { FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); StringBuilder sb = new StringBuilder(); //Map<Integer, Integer> map = new HashMap<>(); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; int[] newP = new int[n]; Map<Integer, Integer> al = new HashMap<>(); for(int i = 0; i<n; i++) { arr[i] = sc.nextInt(); } Map<Integer, ArrayList<Integer>> dif = new HashMap<>(); int[] count = new int[n+1]; for(int i = 0; i<n; i++) { newP[i] = sc.nextInt(); al.put(newP[i], i); if(newP[i] != arr[i]) { if(dif.containsKey(newP[i])) { dif.get(newP[i]).add(i); //ArrayList<Integer> all = dif.get(newP[i]); //all.add(i); //dif.put(newP[i], all); } else { ArrayList<Integer> all = new ArrayList<>(); all.add(i); dif.put(newP[i], all); } count[newP[i]]++; } } /*for(int i = 0; i<n; i++) { al.add(sc.nextInt()); if(arr[i] == al.get(i)) { if(!map.containsKey(arr[i]))map.put(arr[i], 0); } else { if(map.containsKey(al.get(i)))map.put(al.get(i), map.get(al.get(i)+1)); else map.put(al.get(i), 1); } }*/ int[] ava = new int[m]; int[] cnt = new int[n+1]; for(int i= 0; i<m; i++) { ava[i] = sc.nextInt(); cnt[ava[i]]++; } boolean bool = true; for(int i = 1; i<=n; i++) { if(cnt[i]<count[i]) { bool = false; break; } } if(!al.containsKey(ava[m-1]))bool = false; int c = 0; if(bool) { sb.append("YES\n"); for(int i = 0; i<m; i++) { if(dif.containsKey(ava[i]) && dif.get(ava[i]).size()!=0) { for(int j = 0; j<c+1; j++)sb.append(dif.get(ava[i]).get(0)+1+" "); dif.get(ava[i]).remove(0); //if(dif.get(ava[i]).size() == 0)dif.remove(ava[i]); c = 0; } else if(al.containsKey(ava[i])) { for(int j = 0; j<c+1; j++)sb.append(al.get(ava[i])+1+" "); c = 0; } else c++; } }else sb.append("NO"); sb.append("\n"); t--; } System.out.println(sb); } ///////// SUM OF EACH DIGIT OF A NUMBER /////////////// public static long digSum(long a) { long sum = 0; while(a>0) { sum += a%10; a /= 10; } return sum; } ///////// TO CHECK NUMBER IS PRIME OR NOT /////////////// public static boolean isPrime(int 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*i<=n; i+=6) { if(n%i == 0 || n%(i+2) == 0)return false; } return true; } ///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER /////////////// public static int nextPrime(int n) { while(true) { n++; if(isPrime(n)) break; } return n; } ///////// GCD /////////////// public static int gcd(int a, int b) { if(b == 0)return a; return gcd(b, a%b); } ///////// LCM /////////////// public static int lcm(int a, int b) { return (a*b)/gcd(a,b); } }
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
857131f9243e0ee85fdc4d2795b86b01
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 FencePainting { // https://codeforces.com/contest/1481/problem/C public static void main(String[] args) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("FencePainting")); int t = Integer.parseInt(in.readLine()); while (t-->0) { StringTokenizer st = new StringTokenizer(in.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]; int[] moves = new int[m]; ArrayList<Integer> extra = new ArrayList<>(); HashMap<Integer, ArrayList<Integer>> fix = new HashMap<>(); // val, indices HashMap<Integer, Integer> good = new HashMap<>(); st = new StringTokenizer(in.readLine()); for (int i=0; i<n; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(in.readLine()); for (int i=0; i<n; i++) { b[i] = Integer.parseInt(st.nextToken()); if (a[i] != b[i]) { if (!fix.containsKey(b[i])) { fix.put(b[i], new ArrayList<>()); } fix.get(b[i]).add(i); } // else { good.put(b[i], i); // } } st = new StringTokenizer(in.readLine()); for (int i=0; i<m; i++) { c[i] = Integer.parseInt(st.nextToken()); } for (int i=0; i<m; i++) { if (fix.containsKey(c[i])) { for (Integer x : extra) { moves[x] = fix.get(c[i]).get(fix.get(c[i]).size()-1); } extra.clear(); moves[i] = fix.get(c[i]).get(fix.get(c[i]).size()-1); // good.put(c[i], fix.get(c[i]).get(fix.get(c[i]).size()-1)); fix.get(c[i]).remove(fix.get(c[i]).size()-1); if (fix.get(c[i]).size() == 0) { fix.remove(c[i]); } } else { if (good.containsKey(c[i])) { for (Integer x : extra) { moves[x] = good.get(c[i]); } extra.clear(); moves[i] = good.get(c[i]); } else { extra.add(i); } } } if (extra.size() > 0 || fix.size() > 0) { System.out.println("NO"); } else { System.out.println("YES"); StringBuilder s = new StringBuilder(); for (int i=0; i<m; i++) { s.append(moves[i]+1); s.append(" "); } System.out.println(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
d9c4b53252aff9c600d09f5851cfbbe1
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 FencePainting { // https://codeforces.com/contest/1481/problem/C public static void main(String[] args) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("FencePainting")); int t = Integer.parseInt(in.readLine()); while (t-->0) { StringTokenizer st = new StringTokenizer(in.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]; int[] moves = new int[m]; ArrayList<Integer> extra = new ArrayList<>(); HashMap<Integer, ArrayList<Integer>> fix = new HashMap<>(); // val, indices HashMap<Integer, Integer> good = new HashMap<>(); st = new StringTokenizer(in.readLine()); for (int i=0; i<n; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(in.readLine()); for (int i=0; i<n; i++) { b[i] = Integer.parseInt(st.nextToken()); if (a[i] != b[i]) { if (!fix.containsKey(b[i])) { fix.put(b[i], new ArrayList<>()); } fix.get(b[i]).add(i); } else { good.put(b[i], i); } } st = new StringTokenizer(in.readLine()); for (int i=0; i<m; i++) { c[i] = Integer.parseInt(st.nextToken()); } for (int i=0; i<m; i++) { if (fix.containsKey(c[i])) { for (Integer x : extra) { moves[x] = fix.get(c[i]).get(fix.get(c[i]).size()-1); } extra.clear(); moves[i] = fix.get(c[i]).get(fix.get(c[i]).size()-1); good.put(c[i], fix.get(c[i]).get(fix.get(c[i]).size()-1)); fix.get(c[i]).remove(fix.get(c[i]).size()-1); if (fix.get(c[i]).size() == 0) { fix.remove(c[i]); } } else { if (good.containsKey(c[i])) { for (Integer x : extra) { moves[x] = good.get(c[i]); } extra.clear(); moves[i] = good.get(c[i]); } else { extra.add(i); } } } if (extra.size() > 0 || fix.size() > 0) { System.out.println("NO"); } else { System.out.println("YES"); StringBuilder s = new StringBuilder(); for (int i=0; i<m; i++) { s.append(moves[i]+1); s.append(" "); } System.out.println(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
821b7645210c944c0b07a76a8d3270e3
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 credit; import javax.naming.InsufficientResourcesException; import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main{ static boolean v[]; static int ans[]; int size[]; static int count=0; static int dsu=0; static int c=0; static int e9=1000000007; int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; long max1=Long.MIN_VALUE; long min1=Long.MAX_VALUE; boolean aBoolean=true; boolean y=false; long m=0; static boolean t1=false; 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 parent[]; int rank[]; int n=0; /* 100000 */ ArrayList<ArrayList<Integer>> arrayLists; boolean v1[]; static boolean t2=false; boolean r=false; int fib[]; int fib1[]; int min3=Integer.MAX_VALUE; public static void main(String[] args) throws IOException { Main g = new Main(); g.go(); } public void go() throws IOException { FastReader scanner=new FastReader(); // Scanner scanner = new Scanner(System.in); // BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); // String s; PrintWriter printWriter = new PrintWriter(System.out); int t=scanner.nextInt(); for (int i = 0; i < t; i++) { int n=scanner.nextInt(); int m=scanner.nextInt(); int ans[]=new int[m]; int ic[]=new int[n]; int dc[]=new int[n]; int pc[]=new int[m]; boolean r=true; ArrayList<Integer> diff[]=new ArrayList[n+1]; ArrayList<Integer> same[]=new ArrayList[n+1]; int used[]=new int[n+1]; for (int j = 0; j <= n; j++) { diff[j]=new ArrayList<Integer>(); } for (int j = 0; j <=n; j++) { same[j]=new ArrayList<Integer>(); } for (int j = 0; j <n; j++) { ic[j]=scanner.nextInt(); } for (int j = 0; j < n; j++) { dc[j]=scanner.nextInt(); if(dc[j]!=ic[j]){ diff[dc[j]].add(j); }else{ same[dc[j]].add(j); } } for (int j = 0; j < m; j++) { pc[j]=scanner.nextInt(); } int s=-1; for (int j =m-1; j >=0 ; j--) { if(used[pc[j]]<diff[pc[j]].size()){ ans[j]=diff[pc[j]].get(used[pc[j]]); same[pc[j]].add(ans[j]); s=ans[j]; used[pc[j]]++; }else if(same[pc[j]].size()>0){ ans[j]=same[pc[j]].get(0); // System.out.println(ans[j]); s=ans[j]; }else if(s!=-1){ ans[j]=s; } else{ printWriter.println("NO"); r=false; break; } } if(r){ for (int j = 1; j <=n; j++) { if(used[j]<diff[j].size()){ r=false; printWriter.println("NO"); break; } } if(r) { printWriter.println("YES"); for (int j = 0; j < m; j++) { printWriter.print((ans[j] + 1) + " "); } printWriter.println(); } } } printWriter.flush(); } static final int mod=1_000_000_007; public long mul(long a, long b) { return a*b; } public long fact(int x) { long ans=1; for (int i=2; i<=x; i++) ans=mul(ans, i); return ans; } public long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } public long modInv(long x) { return fastPow(x, mod-2); } public long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k)))); } public void sieve(int n){ fib=new int[n+1]; fib[1]=-1; fib[0]=-1; for (int i =2; i*i<=n; i++) { if(fib[i]==0) for (int j =i*i; j <=n; j+=i){ fib[j]=-1; // System.out.println("l"); } } } public void parent(int n){ for (int i = 0; i < n; i++) { parent[i]=i; rank[i]=1; size[i]=1; } } public void union(int i,int j){ int root1=find(i); int root2=find(j); // if(root1 != root2) { // parent[root2] = root1; //// sz[a] += sz[b]; // } if(root1==root2){ return; } if(rank[root1]>rank[root2]){ parent[root2]=root1; size[root1]+=size[root2]; } else if(rank[root1]<rank[root2]){ parent[root1]=root2; size[root2]+=size[root1]; } else{ parent[root2]=root1; rank[root1]+=1; size[root1]+=size[root2]; } } public int find(int p){ if(parent[p]!=p){ parent[p]=find(parent[p]); } return parent[p]; // if(parent[p]==-1){ // return -1; // } // else if(parent[p]==p){ // return p; // } // else { // parent[p]=find(parent[p]); // return parent[p]; // } } public double dist(double x1,double y1,double x2,double y2){ double e=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); double e1=Math.sqrt(e); return e1; } public void make(int p){ parent[p]=p; rank[p]=1; } Random rand = new Random(); public void sort(int[] a, int n) { for (int i = 0; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } Arrays.sort(a, 0, n); } public long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } public void dfs(ArrayList<Integer> arrayLists1){ for (int i = 0; i < arrayLists1.size(); i++) { if(v1[arrayLists1.get(i)]==false){ System.out.println(arrayLists1.get(i)); v1[arrayLists1.get(i)]=true; count++; dfs(arrayLists.get(arrayLists1.get(i))); } } } private void dfs2(ArrayList<Integer>[]arrayList,int j,int count){ ans[j]=count; for(int i:arrayList[j]){ if(ans[i]==-1){ dfs2(arrayList,i,count); } } } private void dfs3(ArrayList<Integer>[] arrayList, int j) { v[j]=true; count++; for(int i:arrayList[j]){ if(v[i]==false) { dfs3(arrayList, i); } } } public double fact(double h){ double sum=1; while(h>=1){ sum=(sum%e9)*(h%e9); h--; } return sum%e9; } public long primef(double r){ long c=0; long ans=1; while(r%2==0){ c++; r=r/2; } if(c>0){ ans*=2; } c=0; // System.out.println(ans+" "+r); for (int i = 3; i <=Math.sqrt(r) ;i+=2) { while(r%i==0){ // System.out.println(i); c++; r=r/i; } if(c>0){ ans*=i; } c=0; } if(r>2){ ans*=r; } return ans; } public long divisor(double r){ long c=0; for (int i = 1; i <=Math.sqrt(r); i++) { if(r%i==0){ if(r/i==i){ c++; } else{ c+=2; } } } return c; } } class Pair{ int x; int y; double z; public Pair(int x,int y){ this.x=x; this.y=y; } @Override public int hashCode() { int hash =27; return this.x * hash + this.y; } @Override public boolean equals(Object o1){ if(o1==null||o1.getClass()!=this.getClass()){ return false; } Pair o=(Pair)o1; if(o.x==this.x&&o.y==this.y){ return true; } return false; } } class Sorting implements Comparator<Pair> { public int compare(Pair p1,Pair p2){ if(p1.x==p2.x){ return -1*Double.compare(p1.y,p2.y); } else { return Double.compare(p1.x, p2.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 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
b960f667da8aade365682a50995e3537
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 solve(Scanner in) { int n = in.nextInt(), m = in.nextInt(); int initial[] = new int[n]; int desired[] = new int[n]; int painters[] = new int[m]; for(int i=0; i<n; i++) initial[i] = in.nextInt(); for(int i=0; i<n; i++) desired[i] = in.nextInt(); for(int i=0; i<m; i++) painters[i] = in.nextInt(); // HashMap<Integer, Integer> availableColors = new HashMap<>(); HashMap<Integer, Queue<Integer>> requiredPaints = new HashMap<>(); int safePlank = -1; for(int i=0; i<n; i++){ if(desired[i]==painters[m-1] && (safePlank==-1 || desired[i]!=initial[i])) safePlank=i+1; if(desired[i]!=initial[i]){ if(!requiredPaints.containsKey(desired[i])){ requiredPaints.put(desired[i], new LinkedList<>()); } requiredPaints.get(desired[i]).add(i+1); } } if(safePlank==-1){ System.out.println("NO"); return; } StringBuilder sb = new StringBuilder(); for(int i=0; i<m; i++){ if(!requiredPaints.containsKey(painters[i])){ sb.append(safePlank+" "); }else{ Queue<Integer> li = requiredPaints.get(painters[i]); sb.append(li.poll()+ " "); if(li.size()==0) requiredPaints.remove(painters[i]); } } if(requiredPaints.size()>0){ System.out.println("NO"); }else{ System.out.println("YES"); System.out.println(sb); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-->0){ solve(in); } } }
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
8656ba76287103aaa9a55dcb0623cb49
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 solution { static Scanner scr = new Scanner(System.in); public static void main(String[] args) { int t=scr.nextInt(); while(t-->0) { solve(); } } static void solve() { int n=scr.nextInt(); int m=scr.nextInt(); ArrayList<ArrayList<Integer>>req=new ArrayList<ArrayList<Integer>>(); for(int i=0;i<=n;i++) { req.add(new ArrayList<Integer>()); } int []a=new int[n]; for(int i=0;i<n;i++) { a[i]=scr.nextInt(); } int b[]=new int[n]; int c[]=new int[m]; for(int i=0;i<n;i++) { b[i]=scr.nextInt(); if(a[i]!=b[i]){ req.get(b[i]).add(i); } } for(int i=0;i<m;i++) { c[i]=scr.nextInt(); } int last=-1; if(req.get(c[m-1]).size()>0) { last=req.get(c[m-1]).get(req.get(c[m-1]).size()-1); req.get(c[m-1]).remove(req.get(c[m-1]).size()-1); }else { for(int i=0;i<n;i++) { if(b[i]==c[m-1]) { last=i; break; } } } if(last==-1) { System.out.println("NO"); return; } int ans[]=new int[m]; ans[m-1]=last; for(int i=0;i<m-1;i++) { if(req.get(c[i]).size()==0) { ans[i]=last; }else { ans[i]=req.get(c[i]).get(req.get(c[i]).size()-1); req.get(c[i]).remove(req.get(c[i]).size()-1); } } for(int i=1;i<=n;i++) { if(req.get(i).size()>0) { System.out.println("NO"); return; } } 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 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
4d5c7c6f9c21c8486bd71f3a385b1c09
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
19d1f40f13b7c9b0793407ee8e34b29e
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 Solutionc { public static void main(String arg[]) throws IOException { CustomInputReader sc = new CustomInputReader(); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0) { int in[] = sc.nextIntArr(2); int n = in[0], m = in[1]; int a[] = sc.nextIntArr(n); //initial int b[] = sc.nextIntArr(n); //desired int c[] = sc.nextIntArr(m); //available HashMap<Integer, Integer> changesRequired = new HashMap<>(); HashMap<Integer,Integer> available = new HashMap<>(); for(int i=0;i<n;++i) { if(a[i] != b[i]) { changesRequired.put(b[i], changesRequired.getOrDefault(b[i], 0)+1); } } for(int i=0;i<m;++i) { available.put(c[i], available.getOrDefault(c[i], 0)+1); } boolean isPossible = true; for(int key : changesRequired.keySet()) { int frequencyRequired = changesRequired.get(key); if(frequencyRequired > available.getOrDefault(key, 0)) { isPossible = false; //System.out.println(key + " : " + frequencyRequired); //System.out.println(available); break; } } HashSet<Integer> set = new HashSet<Integer>(); for(int element:b) set.add(element); if(!set.contains(c[m-1])) { //System.out.println(set); //System.out.println("Last element error"); isPossible = false; } if(isPossible) { pw.println("YES"); int ans[] = new int[m]; HashMap<Integer, LinkedList<Integer>> mapIndices = new HashMap<>(); for(int i=0;i<n;++i) { if(a[i] != b[i]) { if(mapIndices.containsKey(b[i])) mapIndices.get(b[i]).add(i); else { LinkedList<Integer> list = new LinkedList<>(); list.add(i); mapIndices.put(b[i], list); } } } for(int i=0;i<n;++i) { if(mapIndices.containsKey(b[i])) mapIndices.get(b[i]).add(i); else { LinkedList<Integer> list = new LinkedList<>(); list.add(i); mapIndices.put(b[i], list); } } int overwrite = mapIndices.get(c[m-1]).peek(); for(int i=m-1;i>=0;--i) { if(!set.contains(c[i])) { ans[i]=overwrite; } else { if(!mapIndices.get(c[i]).isEmpty()) ans[i]=mapIndices.get(c[i]).removeFirst(); else ans[i] = overwrite; } } for(int i=0;i<m;++i) pw.print((ans[i]+1)+" "); pw.println(""); } else pw.println("NO"); } //flush output pw.flush(); pw.close(); } } class CustomInputReader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public int nextInt() throws IOException { return Integer.parseInt(br.readLine()); } public int[] nextIntArr(int n) throws IOException { String in[] = br.readLine().split(" "); int arr[] = new int[n]; for(int i=0;i<n;++i) arr[i]=Integer.parseInt(in[i]); return arr; } public long nextLong() throws IOException { return Long.parseLong(br.readLine()); } public long[] nextLongArr(int n) throws IOException { String in[] = br.readLine().split(" "); long arr[] = new long[n]; for(int i=0;i<n;++i) arr[i]=Long.parseLong(in[i]); return arr; } public String next() throws IOException { return br.readLine(); } }
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
c50592466b736e6d274fa945720af32b
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 class pair implements Comparable<pair> { int f; int s; public pair() { } public pair(int a, int b) { f = a; s = b; } @Override public int compareTo(pair o) { // TODO Auto-generated method stub return o.s - this.s; } } static int mod = (int) 1e9 + 7; static int ar[]; // static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder(); static ArrayList<Integer> gr[]; static void buildGraph(int n, int m) throws IOException { gr = new ArrayList[n]; for (int i = 0; i < n; i++) { gr[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = sc.nextInt(); int v = sc.nextInt(); u--; v--; gr[u].add(v); gr[v].add(u); } } static void sort(long a[], int n) { ArrayList<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) { al.add(a[i]); } Collections.sort(al); for (int i = 0; i < n; i++) { a[i] = al.get(i); } } static int dp[][] = new int[1000][1000]; static void in(int a[], int n) throws IOException { for (int i = 0; i < n; i++) a[i] = sc.nextInt(); } public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int c[]=new int[m]; in(a,n);in(b,n);in(c,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); } } if(hm.size()!=0 ) { ok=false; } if(hs.size()>0 && !thm.containsKey(c[m-1]))ok=false; if(!ok) { out.append("No\n"); continue; } else { out.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++) { out.append((ans.get(i)+1)+" "); } out.append("\n"); } } System.out.println(out); } static Reader sc = new Reader(); static 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') break; 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(); } } }
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
c5f4f952c254a62d10add2a249004c11
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.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class Main{ //static long MOD = 1_000_000_007L; static long MOD = 998_244_353L; //static long MOD = 1_000_000_033L; static long inv2 = (MOD + 1) / 2; static long [] fac; static long [] pow; static long [] inv; static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; static long lMax = 0x3f3f3f3f3f3f3f3fL; static int iMax = 0x3f3f3f3f; static HashMap <Long, Long> memo = new HashMap(); static MyScanner sc = new MyScanner(); //static ArrayList <Integer> primes; static long[] pow2; //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); // Start writing your solution here. ------------------------------------- int nn = 300000; /*fac = new long[nn + 1]; fac[1] = 1; for(int i = 2; i <= nn; i++) fac[i] = fac[i - 1] * 1L * i % MOD;*/ /*pow = new long[nn + 1]; pow[0] = 1L; for(int i = 1; i <= nn; i++) pow[i] = pow[i - 1] * 2L % MOD; pow[1] = 3L;*/ /*pow2 = new long[nn + 1]; pow2[0] = 1L; for(int i = 1; i <= nn; i++) pow2[i] = pow2[i - 1] * 2L % (MOD - 1);*/ /*inv = new long[nn + 1]; inv[1] = 1; for (int i = 2; i <= nn; ++i) inv[i] = (MOD - MOD / i) * inv[(int)(MOD % i)] % MOD;*/ //primes = sieveOfEratosthenes(100001); int t = 1; t = sc.ni(); while(t-- > 0){ int[] resA = solveA(); if(resA.length == 0) out.println("NO"); else { out.println("YES"); for (long i : resA) out.print(i + " "); out.println(); } /* int res = solve(); out.println(res);*/ } out.close(); } static int[] solveA() { /*String s = sc.nextLine(); char[] c = s.toCharArray(); int n = c.length;*/ int n = sc.ni(); int m = sc.ni(); int diffcnt = 0; int[] a = new int[n + 1], b = new int[n + 1], c = new int[m], res = new int[m]; Arrays.fill(res, -1); HashSet <Integer> last = new HashSet(); HashMap <Integer, HashSet> diff = new HashMap(); HashMap <Integer, Integer> change = new HashMap(); for(int i = 1; i <= n; i++) a[i] = sc.ni(); for(int i = 1; i <= n; i++) { b[i] = sc.ni(); last.add(b[i]); } for(int i = 0; i < m; i++) { c[i] = sc.ni(); if(last.contains(c[i])) change.put(c[i], change.getOrDefault(c[i], 0) + 1); } if(!last.contains(c[m - 1])) return new int[]{}; for(int i = 1; i <= n; i++) { if(a[i] != b[i]) { diff.putIfAbsent(b[i], new HashSet()); diff.get(b[i]).add(i); diffcnt++; } } for(int i : diff.keySet()) { if(change.get(i) == null || change.get(i) < diff.get(i).size()) return new int[]{}; } int ind = -1; for (int i = 1; i <= n; i++) { if (b[i] == c[m - 1]) ind = i; } for(int i = 0; i < m; i++) { if(diff.containsKey(c[i]) && diff.get(c[i]).size() > 0) { int tmp = (int)diff.get(c[i]).iterator().next(); res[i] = tmp; diff.get(c[i]).remove(tmp); if(i == m - 1) ind = tmp; } } for(int i = 0; i < m; i++) { if(res[i] == -1) res[i] = ind; } return res; } static int solve() { /*String s = sc.nextLine(); char[] c = s.toCharArray(); int n = c.length;*/ int n = sc.ni(); int k = sc.ni(); char[][] c = new char[n][n]; for(int i = 0; i < n; i++) { String tmp = sc.next(); c[i] = tmp.toCharArray(); } return 0; } // SegmentTree From uwi public class SegmentTreeRMQ { public int M, H, N; public int[] st; public SegmentTreeRMQ(int n) { N = n; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; st = new int[M]; Arrays.fill(st, 0, M, Integer.MAX_VALUE); } public SegmentTreeRMQ(int[] a) { N = a.length; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; st = new int[M]; for(int i = 0;i < N;i++){ st[H+i] = a[i]; } Arrays.fill(st, H+N, M, Integer.MAX_VALUE); for(int i = H-1;i >= 1;i--)propagate(i); } public void update(int pos, int x) { st[H+pos] = x; for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i); } private void propagate(int i) { st[i] = Math.min(st[2*i], st[2*i+1]); } public int minx(int l, int r){ int min = Integer.MAX_VALUE; if(l >= r)return min; while(l != 0){ int f = l&-l; if(l+f > r)break; int v = st[(H+l)/f]; if(v < min)min = v; l += f; } while(l < r){ int f = r&-r; int v = st[(H+r)/f-1]; if(v < min)min = v; r -= f; } return min; } public int min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);} private int min(int l, int r, int cl, int cr, int cur) { if(l <= cl && cr <= r){ return st[cur]; }else{ int mid = cl+cr>>>1; int ret = Integer.MAX_VALUE; if(cl < r && l < mid){ ret = Math.min(ret, min(l, r, cl, mid, 2*cur)); } if(mid < r && l < cr){ ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1)); } return ret; } } } public static double dist(double a, double b){ return Math.sqrt(a * a + b * b); } public static long inv(long a){ return quickPOW(a, MOD - 2); } public class Interval { int start; int end; public Interval(int start, int end) { this.start = start; this.end = end; } } public static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * 2; i <= n; i += p) { prime[i] = false; } } } ArrayList<Integer> primeNumbers = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i]) { primeNumbers.add(i); } } return primeNumbers; } public static int lowerBound(int[] a, int v){ return lowerBound(a, 0, a.length, v); } public static int lowerBound(int[] a, int l, int r, int v) { if(l > r || l < 0 || r > a.length)throw new IllegalArgumentException(); int low = l-1, high = r; while(high-low > 1){ int h = high+low>>>1; if(a[h] >= v){ high = h; }else{ low = h; } } return high; } public static int rlowerBound(int[] a, int v){ return lowerBound(a, 0, a.length, v); } public static int rlowerBound(int[] a, int l, int r, int v) { if(l > r || l < 0 || r > a.length)throw new IllegalArgumentException(); int low = l-1, high = r; while(high-low > 1){ int h = high+low>>>1; if(a[h] <= v){ high = h; }else{ low = h; } } return high; } public static long C(int n, int m) { if(m == 0 || m == n) return 1l; if(m > n || m < 0) return 0l; long res = fac[n] * quickPOW((fac[m] * fac[n - m]) % MOD, MOD - 2) % MOD; return res; } public static long quickPOW(long n, long m) { long ans = 1l; while(m > 0) { if(m % 2 == 1) ans = (ans * n) % MOD; n = (n * n) % MOD; m >>= 1; } return ans; } public static long quickPOW(long n, long m, long mod) { long ans = 1l; while(m > 0) { if(m % 2 == 1) ans = (ans * n) % mod; n = (n * n) % mod; m >>= 1; } return ans; } public static int gcd(int a, int b) { if(a % b == 0) return b; return gcd(b, a % b); } public static long gcd(long a, long b) { if(a % b == 0) return b; return gcd(b, a % b); } static class Randomized { public static void shuffle(int[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void shuffle(long[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(long[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); long tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return RandomWrapper.INSTANCE.nextInt(l, r); } } static class RandomWrapper { private Random random; public static final RandomWrapper INSTANCE = new RandomWrapper(new Random()); public RandomWrapper() { this(new Random()); } public RandomWrapper(Random random) { this.random = random; } public int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } //-----------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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { 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
be8e755976bcbf97b7684b8cac0af039
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.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { 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 j = 0; j < n; j++) { a[j] = sc.nextInt(); } Map<Integer, List<Integer>> bb = new HashMap<>(); for (int j = 0; j < n; j++) { b[j] = sc.nextInt(); bb.computeIfAbsent(b[j], asas -> new ArrayList<>()).add(j); } for (int j = 0; j < m; j++) { c[j] = sc.nextInt(); } boolean possible = true; StringBuilder sb = new StringBuilder(); for (int j = 0; j < m; j++) { int k; List<Integer> doskiForDesiredColor = bb.get(c[j]); if (doskiForDesiredColor == null && j == m - 1) { a[0] = c[j]; break; } else if (doskiForDesiredColor != null) { k = findDoskaToPaint(doskiForDesiredColor, a, c, j); } else { int jj = j + 1; for (; jj < m && bb.get(c[jj]) == null; jj++) { } if (jj >= m) { possible = false; break; } k = findDoskaToPaint(bb.get(c[jj]), a, c, jj); } a[k] = c[j]; sb.append(k + 1).append(' '); } if (!possible || !Arrays.equals(a, b)) { System.out.println("NO"); } else { System.out.println("YES"); System.out.println(sb.toString().trim()); } } } static int findDoskaToPaint(List<Integer> doskiForDesiredColor, int[] a, int[] c, int j) { for (int k = 0; k < doskiForDesiredColor.size(); k++) { int doska = doskiForDesiredColor.get(k); if (a[doska] != c[j]) { return doska; } } return doskiForDesiredColor.get(0); // int k = doskiForDesiredColor.get(0); // while (a[k] == c[j] && doskiForDesiredColor.size() > 1) { // k = doskiForDesiredColor.remove(0); // } // return k; } }
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
f92bfd8b8e66b2bae44011df6e775151
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
//Implemented By Aman Kotiyal Date:-05-Feb-2021 Time:-7:55:11 pm import java.io.*; import java.util.*; public class ques3 { public static void main(String[] args)throws Exception{ new ques3().run();} long mod=1000000000+7; void solve() throws Exception { for(int ii=ni();ii>0;ii--) { int n=ni(); int m=ni(); long a[]=new long[n]; long b[]=new long[n]; long c[]=new long[m]; for (int i = 0; i <n; i++) a[i]=nl(); HashSet<Long> setb=new HashSet<>(); for (int i = 0; i <n; i++) { b[i]=nl(); setb.add(b[i]); } for (int i = 0; i <m; i++) c[i]=nl(); //check boolean ch=false; int index=0; for (int i = 0; i <m; i++) { if(setb.contains(c[i])) { index=i; ch=true; } } if(ch==false) { out.println("NO"); continue; } HashMap<Long,HashSet<Integer>> map= new HashMap<>(); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { HashSet<Integer> tem=map.getOrDefault(b[i], new HashSet<>()); tem.add(i); map.put(b[i], tem); } } ch=false; int ind=0; int ans[]=new int[m]; int flag=0; int i2=0; for (int i = 0; i <n; i++) { if(b[i]==c[index]) { i2=i; break; } } for(int j=m-1;j>=0;j--) { if(map.containsKey(c[j])) { //HashSet<Integer> tem=map.get(c[j]); for(Integer key:map.get(c[j])) { ans[j]=key; ch=true; ind=key; map.get(c[j]).remove(key); if(map.get(c[j]).size()==0) map.remove(c[j]); break; } } else { if(ch==false) { if(j<=index) { ans[j]=i2; continue; } else { flag=1; break; } } else { ans[j]=ind; } } } if(map.size()!=0||flag==1) { out.println("NO"); continue; } out.println("YES"); for (int i = 0; i < ans.length; i++) { out.print((ans[i]+1)+" "); } out.println(); } } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private 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
c184a9de95f360ac8e4f85def5ed3008
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 class FastIO { BufferedReader br; BufferedWriter bw, be; StringTokenizer st; public FastIO() { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); be = new BufferedWriter(new OutputStreamWriter(System.err)); st = new StringTokenizer(""); } private void read() throws IOException { st = new StringTokenizer(br.readLine()); } public String ns() throws IOException { while(!st.hasMoreTokens()) read(); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(ns()); } public long nl() throws IOException { return Long.parseLong(ns()); } public float nf() throws IOException { return Float.parseFloat(ns()); } public double nd() throws IOException { return Double.parseDouble(ns()); } public char nc() throws IOException { return ns().charAt(0); } public int[] nia(int n) throws IOException { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } public long[] nla(int n) throws IOException { long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = nl(); return a; } public char[] nca() throws IOException { return f.ns().toCharArray(); } public void out(String s) throws IOException { bw.write(s); } public void flush() throws IOException { bw.flush(); be.flush(); } public void err(String s) throws IOException { be.write(s); } } static FastIO f; public static void main(String args[]) throws IOException { f = new FastIO(); int t, n, m, a[], b[], c[], p[], i, x[]; HashMap<Integer, Integer> h, w; HashSet<Integer> u; t = f.ni(); while(t-->0) { n = f.ni(); m = f.ni(); a = f.nia(n); b = new int[n]; c = new int[m]; p = new int[n]; x = new int[m]; Arrays.fill(p, -1); h = new HashMap<>(); w = new HashMap<>(); u = new HashSet<>(); for(i = 0; i < n; i++) { b[i] = f.ni(); if(a[i] != b[i]) { if(h.containsKey(b[i])) p[i] = h.get(b[i]); h.put(b[i], i); } else w.put(b[i], i); } // f.err(h + "\n" + w + "\n\n"); for(i = 0; i < m; i++) { c[i] = f.ni(); if(h.containsKey(c[i])) { x[i] = h.get(c[i]); if(p[x[i]] == -1) h.remove(c[i]); else h.put(c[i], p[x[i]]); w.put(c[i], x[i]); for(Integer j : u) x[j] = x[i]; u.clear(); } else if(w.containsKey(c[i])) { x[i] = w.get(c[i]); for(Integer j : u) x[j] = x[i]; u.clear(); } else u.add(i); } if(!u.isEmpty() || !h.isEmpty()) f.out("NO\n"); else { f.out("YES\n"); for(i = 0; i < m; i++) f.out(x[i]+1 + " "); f.out("\n"); } } f.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
c7ae5789454aa93f710c03044a5ea1ea
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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.StringTokenizer; /** * * @author is2ac */ public class FencePainting { public static void main(String[] args) { FastScanner25 fs = new FastScanner25(); PrintWriter pw = new PrintWriter(System.out); int t = fs.ni(); for (int tc = 0; tc < t; tc++) { int n = fs.ni(); int m = fs.ni(); int[] a = new int[n], b = new int[n], c = new int[m]; HashSet<Integer> set = new HashSet(); for (int i = 0; i < a.length; i++) { a[i] = fs.ni(); } for (int i = 0; i < b.length; i++) { b[i] = fs.ni(); set.add(b[i]); } for (int i = 0; i < c.length; i++) { c[i] = fs.ni(); } boolean flag = true; int d = -1; Map<Integer,Queue<Integer>> map = new HashMap(); Map<Integer,Queue<Integer>> ex = new HashMap(); for (int i = 0; i < a.length; i++) { if (a[i]!=b[i]) { if (!map.containsKey(b[i])) { Queue<Integer> q = new LinkedList(); map.put(b[i],q); } map.get(b[i]).add(i); } else { if (!ex.containsKey(b[i])) { Queue<Integer> q = new LinkedList(); ex.put(b[i],q); } ex.get(b[i]).add(i); } } int[] x = new int[m]; int ind = -1; for (int i = c.length-1; i > -1; i--) { if (map.containsKey(c[i])) { x[i] = map.get(c[i]).poll(); if (map.get(c[i]).size()==0) map.remove(c[i]); if (ind==-1) ind = x[i]; } else if (ex.containsKey(c[i])) { x[i] = ex.get(c[i]).poll(); if (ex.get(c[i]).size()==0) ex.remove(c[i]); if (ind==-1) ind = x[i]; } else { if (ind==-1) flag = false; x[i] = ind; } } if (!map.isEmpty()) { flag = false; } if (!flag) { pw.println("NO"); continue; } StringBuilder sb = new StringBuilder(); pw.println("YES"); for (int i = 0; i < x.length; i++) { sb.append((x[i]+1) + " "); } pw.println(sb.toString()); } pw.close(); } } class FastScanner25 { BufferedReader br; StringTokenizer st; public FastScanner25() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni(); return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl(); return ret; } double nd() { 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
200b7f1e7b663de93fd93ddf6fb559a7
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 P1481C { static InputReader in = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = (long) (Math.pow(10, 9)) + 7; static HashMap<Integer, List<Integer>> adjList = new HashMap<>(); static boolean[] visited; public static void main(String[] args) { int test = in.nextInt(); // int test = 1; while(test-->0) { solve(); } pw.close(); } static void solve() { int n = in.nextInt(), m = in.nextInt(); int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = in.nextInt(); int[] b = new int[n + 1]; for (int i = 1; i <= n; i++) b[i] = in.nextInt(); int[] c = new int[m + 1]; for (int i = 1; i <= m; i++) c[i] = in.nextInt(); int found = -1; HashMap<Integer, List<Integer>> map = new HashMap<>(); HashMap<Integer, Integer> same = new HashMap<>(); for (int i = 1; i <= m; i++) { map.putIfAbsent(c[i], new ArrayList<>()); map.get(c[i]).add(i); } HashMap<Integer, List<Integer>> needed = new HashMap<>(); for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { needed.putIfAbsent(b[i], new ArrayList<>()); needed.get(b[i]).add(i); } else { same.put(b[i], i); } } int[] ans = new int[m + 1]; for (int i = m; i >= 1; i--) { if (!needed.containsKey(c[i]) && found == -1) { if (same.containsKey(c[i])) { ans[i] = same.get(c[i]); found = same.get(c[i]); } else { pw.println("NO"); return; } } else if (!needed.containsKey(c[i]) && found >= 0) { ans[i] = found; } else if (needed.containsKey(c[i])) { ans[i] = needed.get(c[i]).get(0); found = needed.get(c[i]).remove(0); if (needed.get(c[i]).size() == 0) { needed.remove(c[i]); } } } if (needed.size() > 0) { pw.println("NO"); return; } pw.println("YES"); for (int i = 1; i <= m; i++) { pw.print(ans[i] + " "); } pw.println(); } static long fast_pow(long a, long b) { if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static void sort(long[] a) //check for long { 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 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 class UF { // Number of connected components private int count; // Store a tree private int[] parent; // Record the "weight" of the tree private int[] size; public UF(int n) { this.count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // The small tree is more balanced under the big tree if (size[rootP] > size[rootQ]) { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } else { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } count--; } public boolean connected(int p, int q) { int rootP = find(p); int rootQ = find(q); return rootP == rootQ; } private int find(int x) { while (parent[x] != x) { // Path compression parent[x] = parent[parent[x]]; x = parent[x]; } return x; } public int count() { return count; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } 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; } public static int[] shuffle(int[] a, Random gen) { for(int i = 0, n = a.length;i < n;i++) { int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } 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 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -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
411e8f8deeee294b45b8049534cd05c7
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 codeforce { static boolean multipleTC = true; final static int Mod = 1000000007; final static int Mod2 = 998244353; final double PI = 3.14159265358979323846; int MAX = 1000000007; long ans = 0; void pre() throws Exception { } long factorial(long n) { if(n <= 1) return 1; long f = 1; for(long i=1;i<=n;i++) f = (f*i)%Mod2; return f%Mod2; } long power(long ele, long m){ long temp = ele; while(temp%m == 0) { temp /= m; } return ele/temp; } void insert(HashMap<Integer, List<Integer>> map, int ele, int idx) { if(!map.containsKey(ele)) { List<Integer> list = new ArrayList<>(); list.add(idx); map.put(ele, list); } else { List<Integer> list = map.get(ele); list.add(idx); map.put(ele, list); } } void solve(int t) throws Exception { int n = ni(); int m = ni(); int a[] = new int[n+1]; int b[] = new int[n+1]; int c[] = new int[m+1]; int arr[] = new int[m+1]; HashMap<Integer, List<Integer>> map1 = new HashMap<>(); HashMap<Integer, List<Integer>> map2 = new HashMap<>(); // map1 ---> need to repaint // map2 ---> already in same color for(int i=1;i<=n;i++) a[i] = ni(); for(int i=1;i<=n;i++) { b[i] = ni(); if(b[i] != a[i]) insert(map1, b[i], i); else insert(map2, b[i], i); } for(int i=1;i<=m;i++) c[i] = ni(); for(int i=m;i>=1;i--) { if(map1.containsKey(c[i])) { List<Integer> list = map1.get(c[i]); map1.remove(c[i]); int idx = list.get(0); list.remove(0); if(list.size() > 0) map1.put(c[i], list); arr[i] = idx; } else if(map2.containsKey(c[i])) { List<Integer> list = map2.get(c[i]); map2.remove(c[i]); int idx = list.get(0); list.remove(0); if(list.size() > 0) map2.put(c[i], list); arr[i] = idx; } else { if(i<m) arr[i] = arr[i+1]; else { pn("NO"); return; } } } for(int i=1;i<=m;i++) a[arr[i]] = c[i]; for(int i=1;i<=n;i++) { if(a[i] != b[i]) { pn("NO"); return; } } pn("YES"); StringBuilder ans = new StringBuilder(); for(int i=1;i<=m;i++) ans.append(arr[i] + " "); pn(ans); } double dist(int x1, int y1, int x2, int y2) { double a = x1 - x2, b = y1 - y2; return Math.sqrt((a * a) + (b * b)); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } void pn(int[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } void pn(long[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } char c() throws Exception{ return in.next().charAt(0); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } public static void main(String[] args) throws Exception { new codeforce().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } 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
ce4d3be4ea392584ce7ffc75576a9210
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
157cefe5ffece9da1815a63b30802d73
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
/* 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Look at painters from latest to earliest, if a painter is not wanted anywhere (color does not have a good position) then assign that painter to a part of the fence that was already painted */ import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int nc = Integer.parseInt(br.readLine()); for(int cc = 0; cc < nc; cc++){ StringTokenizer details = new StringTokenizer(br.readLine()); //n, m int n = Integer.parseInt(details.nextToken()); //number of planks int m = Integer.parseInt(details.nextToken()); //number of painters HashMap<Integer, TreeSet<Integer>> fence = new HashMap<Integer, TreeSet<Integer>>(); //fence.get(i) = all places that can be painted with color i int alreadyPainted = -1; int[] painters = new int[m]; int[] ret = new int[m]; int[] initial = new int[n]; int[] pos = new int[n]; //pos[i] = random position where color i is desired (if color is not needed) Arrays.fill(pos, -1); int[] fin = new int[n]; StringTokenizer init = new StringTokenizer(br.readLine()); //initial fence colors for(int x = 0; x < n; x++) initial[x] = Integer.parseInt(init.nextToken()) - 1; StringTokenizer a = new StringTokenizer(br.readLine()); //desired colors StringTokenizer b = new StringTokenizer(br.readLine()); //painter colors for(int x = 0; x < n; x++){ int p = Integer.parseInt(a.nextToken()) - 1; fin[x] = p; if(initial[x] != p){ if(!fence.containsKey(p)) fence.put(p, new TreeSet<Integer>()); fence.get(p).add(x); } else{ pos[p] = x; } } for(int y = 0; y < m; y++){ painters[y] = Integer.parseInt(b.nextToken()) - 1; } boolean works = true; for(int y = m - 1; y >= 0; y--){ //System.out.println(painters[y] + " current painter, desired is " + fence + " and alreadyPainted is " + alreadyPainted); //System.out.println("all positions of colors is " + Arrays.toString(pos)); if(fence.containsKey(painters[y])){ ret[y] = fence.get(painters[y]).first(); initial[ret[y]] = painters[y]; fence.get(painters[y]).remove(ret[y]); alreadyPainted = ret[y]; if(fence.get(painters[y]).isEmpty()) fence.remove(painters[y]); } else{ //color not wanted - cover it? if(pos[painters[y]] != -1){ ret[y] = pos[painters[y]]; alreadyPainted = ret[y]; } else if(alreadyPainted != -1){ ret[y] = alreadyPainted; //just layer all colors under this painted fencepost OR put colors on top of desired color } else{ works = false; break; } } } for(int x = 0; x < n; x++) if(initial[x] != fin[x]) works = false; //System.out.println(Arrays.toString(initial) + " " + Arrays.toString(fin)); if(!works) System.out.println("NO"); else{ System.out.println("YES"); StringBuilder rt = new StringBuilder(); for(int x = 0; x < m; x++){ rt.append((ret[x] + 1) + " "); } System.out.println(rt); } //System.out.println(); } br.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
6709d194ea24d9658fe52331c35e867c
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 { static { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) {} } void solve() { int n = in.nextInt(); int m = in.nextInt(); int[] a = array(n); int[] b = array(n); int[] p = array(m); int last = p[m - 1]; int mainIndex = -1; for (int i = 0; i < n; i++) { if (b[i] == last) { mainIndex = i; if (b[i] != a[i]) { break; } } } if (mainIndex == -1) { out.append("NO\n"); return; } LinkedList<Integer> temp; HashMap<Integer, LinkedList<Integer>> notEquals = new HashMap<>(); for (int i = 0; i < n; i++) { if (i == mainIndex)continue; if (a[i] != b[i]) { if (notEquals.containsKey(b[i])) { temp = notEquals.get(b[i]); temp.offer(i); } else { temp = new LinkedList<Integer>(); temp.offer(i); notEquals.put(b[i], temp); } } } // HashMap<Integer, LinkedList<Integer>> equals = new HashMap<>(); // for (int i = 0; i < n; i++) { // if (a[i] == b[i]) { // if (equals.containsKey(b[i])) { // temp = equals.get(b[i]); // temp.offer(i); // } else { // temp = new LinkedList<Integer>(); // temp.offer(i); // equals.put(b[i], temp); // } // } // } // HashMap<Integer, LinkedList<Integer>> allA = new HashMap<>(); // for (int i = 0; i < n; i++) { // if (allA.containsKey(a[i])) { // temp = allA.get(a[i]); // temp.offer(i); // } else { // temp = new LinkedList<Integer>(); // temp.offer(i); // allA.put(a[i], temp); // } // } int[] res = new int[m]; for (int i = 0; i < m - 1; i++) { int val = p[i]; if (notEquals.containsKey(val)) { temp = notEquals.get(val); int index = temp.poll(); if (temp.size() == 0)notEquals.remove(val); // Integer toRemove = index; // temp = allA.get(a[index]); // temp.remove(toRemove); // if (temp.size() == 0)allA.remove(a[index]); a[index] = val; res[i] = index + 1; // temp = allA.getOrDefault(allA.get(a[index]), new LinkedList<Integer>()); // temp.add(index); // allA.put(a[index], temp); } // else if (allA.containsKey(val)) { // temp = allA.get(val); // int index = temp.peek(); // a[index] = val; // res[i] = index + 1; // } else { // temp = allA.get(a[mainIndex]); // Integer toRemove = mainIndex; // temp.remove(toRemove); // if (temp.size() == 0)allA.remove(a[mainIndex]); res[i] = mainIndex + 1; a[mainIndex] = val; // temp = allA.getOrDefault(allA.get(val), new LinkedList<Integer>()); // temp.add(mainIndex); // allA.put(val, temp); } } res[m - 1] = mainIndex + 1; a[mainIndex] = b[mainIndex]; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { out.append("NO\n"); return; } } out.append("YES\n"); for (int i = 0; i < m; i++) { out.append(res[i] + " "); } out.append("\n"); } public static void main (String[] args) { // Its Not Over Untill I Win - Syed Mizbahuddin Main sol = new Main(); int t = 1; t = in.nextInt(); while (t-- != 0) { sol.solve(); } System.out.print(out); } <T> void println(T[] s) { System.out.println(Arrays.toString(s)); } <T> void println(T s) { System.out.println(s); } void print(int s) { System.out.print(s); } void println(int s) { System.out.println(s); } void println(int[] a) { println(Arrays.toString(a)); } int[] array(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } return a; } int[] array1(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); } return a; } static FastReader in; static StringBuffer out; final int MAX; final int MIN; int mod ; Main() { in = new FastReader(); out = new StringBuffer(); MAX = Integer.MAX_VALUE; MIN = Integer.MIN_VALUE; mod = (int)1e9 + 7; } } 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
ef215d760e4a71b08c72d75922525d24
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(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 { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] a = in.nextIntArray(n); int[] b = in.nextIntArray(n); int[] c = in.nextIntArray(m); int last = -1; Map<Integer, List<Integer>> need = new HashMap<>(); for (int i = 0; i < n; i++) { if (a[i] != b[i]) { need.computeIfAbsent(b[i], k -> new ArrayList<>()).add(i); } } if (need.containsKey(c[m - 1])) { List<Integer> list = need.get(c[m - 1]); last = list.get(list.size() - 1); } else { for (int i = 0; i < n; i++) { if (b[i] == c[m - 1]) { last = i; break; } } } if (last == -1) { out.println("NO"); return; } List<Integer> res = new ArrayList<>(); for (int i = 0; i < m; i++) { if (need.containsKey(c[i])) { res.add(need.get(c[i]).remove(0)); if (need.get(c[i]).size() == 0) { need.remove(c[i]); } } else { res.add(last); } } if (need.size() == 0) { out.println("YES"); for (Integer i : res) { out.print(i + 1 + " "); } out.println(); } else { out.println("NO"); } } } 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() { writer.println(); } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.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 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 String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } 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
dc73177e5ffa42d912032b118b488493
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
//some updates in import stuff import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; //update this bad boi too public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) throws IOException{ MyScanner sc = new MyScanner(); //changes in this line of code out = new PrintWriter(new BufferedOutputStream(System.out)); // out = new PrintWriter(new BufferedWriter(new FileWriter("output.out"))); //WRITE YOUR CODE IN HERE int test = sc.nextInt(); while(test-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int[] prev = new int[n]; int[] post = new int[n]; for(int i= 0; i < n; i++) prev[i] = sc.nextInt(); for(int i= 0; i < n; i++) post[i] = sc.nextInt(); //simple as that, and now we have the proper input bois int[] paint = new int[m]; for(int i= 0; i < m; i++) paint[i] = sc.nextInt(); //now we have all the input we needed, Map<Integer, ArrayList<Integer>> map = new HashMap<>(); for(int i = 0; i < n; i++){ //position at which stuff ain't equal if(prev[i] != post[i]){ map.putIfAbsent(post[i], new ArrayList<>()); map.get(post[i]).add(i); } } //saari positions mil gyi wrong vaali int[] ans = new int[m]; Arrays.fill(ans, -1); boolean check= false; for(int i = 0; i < m; i++){ int color = paint[i]; if(map.containsKey(color)){ if(i == m-1) check = true; ArrayList<Integer> clist = map.get(color); int index = clist.get(clist.size()-1); clist.remove(clist.size()-1); //simply remove if(map.get(color).size() == 0){ map.remove(color); } ans[i] = index; } } //check for the last element now //now check for the last element for sure now if(map.size() != 0){ out.println("NO"); continue; } //if last element do exist int pos = -1; if(!check){ int last = paint[m-1]; for(int i = 0; i < n; i++){ if(last == post[i]){ pos = i; break; } } if(pos == -1){ out.println("NO"); continue; } }else{ pos = ans[m-1]; } for(int i = 0; i < m; i++){ if(ans[i] == -1){ ans[i] = pos; } } out.println("YES"); for(int i : ans){ out.print((i+1) + " "); } out.println(); } out.close(); } //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //Updation Required //Fenwick Tree (customisable) //Segment Tree (customisable) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod 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; } //without 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 class segmentTree{ public long[] arr; public long[] tree; public long[] lazy; segmentTree(long[] array){ int n = array.length; arr = new long[n]; for(int i= 0; i < n; i++) arr[i] = array[i]; tree = new long[4*n + 1]; lazy = new long[4*n + 1]; } public void build(int[]arr, int s, int e, int[] tree, int index){ if(s == e){ tree[index] = arr[s]; return; } //otherwise divide in two parts and fill both sides simply int mid = (s+e)/2; build(arr, s, mid, tree, 2*index); build(arr, mid+1, e, tree, 2*index+1); //who will build the current position dude tree[index] = Math.min(tree[2*index], tree[2*index+1]); } public int query(int sr, int er, int sc, int ec, int index, int[] tree){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(sc != ec){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap if(sr > ec || sc > er) return Integer.MAX_VALUE; //found the index baby if(sr <= sc && ec <= er) return tree[index]; //finding the index on both sides hehehehhe int mid = (sc + ec)/2; int left = query(sr, er, sc, mid, 2*index, tree); int right = query(sr, er, mid+1, ec, 2*index + 1, tree); return Integer.min(left, right); } //now we will do point update implementation //it should be simple then we expected for sure public void update(int index, int indexr, int increment, int[] tree, int s, int e){ if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] = lazy[index]; lazy[2*index] = lazy[index]; } lazy[index] = 0; } //no overlap if(indexr < s || indexr > e) return; //found the required index if(s == e){ tree[index] += increment; return; } //search for the index on both sides int mid = (s+e)/2; update(2*index, indexr, increment, tree, s, mid); update(2*index+1, indexr, increment, tree, mid+1, e); //now update the current range simply tree[index] = Math.min(tree[2*index+1], tree[2*index]); } public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){ //if not at all in the same range if(e < sr || er < s) return; //complete then also move forward if(s == e){ tree[index] += increment; return; } //otherwise move in both subparts int mid = (s+e)/2; rangeUpdate(tree, 2*index, s, mid, sr, er, increment); rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment); //update current range too na //i always forget this step for some reasons hehehe, idiot tree[index] = Math.min(tree[2*index], tree[2*index + 1]); } public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){ //update lazy values //resolve lazy value before going down if(lazy[index] != 0){ tree[index] += lazy[index]; if(s != e){ lazy[2*index+1] += lazy[index]; lazy[2*index] += lazy[index]; } lazy[index] = 0; } //no overlap case if(sr > e || s > er) return; //complete overlap if(sr <= s && er >= e){ tree[index] += increment; if(s != e){ lazy[2*index+1] += increment; lazy[2*index] += increment; } return; } //otherwise go on both left and right side and do your shit int mid = (s + e)/2; rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment); rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment); tree[index] = Math.min(tree[2*index+1], tree[2*index]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function 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'); } //to sort the array with better method 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); } //sort long 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; Pair(int a , int b){ this.a = a; this.b = b; } @Override public String toString(){ return a + " -> " + b; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } //-----------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)); } // public MyScanner() throws FileNotFoundException { // br = new BufferedReader(new FileReader("input.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
f2cf566ce58cd70754c850ea788b51cc
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.util.stream.IntStream; public class P1481CR { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int noOfTestCases = sc.nextInt(); IntStream.range(0, noOfTestCases).forEach(t -> { int n = sc.nextInt(); int m = sc.nextInt(); int[] current = IntStream.range(0, n).map(i -> sc.nextInt()).toArray(); Map<Integer, Integer> colorToIndexMap = new HashMap<>(); Map<Integer, Stack<Integer>> desiredColorToIndexMap = new HashMap<>(); int[] answerArray = new int[m]; Stack<Integer> colorsToDump = new Stack<>(); IntStream.range(0, n).forEach(i -> { int desiredColor = sc.nextInt(); if (current[i] == desiredColor) { colorToIndexMap.put(desiredColor, i); } else { if (desiredColorToIndexMap.containsKey(desiredColor)) { desiredColorToIndexMap.get(desiredColor).push(i); } else { Stack<Integer> indexList = new Stack<>(); indexList.push(i); desiredColorToIndexMap.put(desiredColor, indexList); } } }); IntStream.range(0, m).forEach(i -> { int painterColor = sc.nextInt(); if (desiredColorToIndexMap.containsKey(painterColor) && desiredColorToIndexMap.get(painterColor).size() > 0) { int index = desiredColorToIndexMap.get(painterColor).pop(); colorToIndexMap.put(painterColor, index); answerArray[i] = index; while (colorsToDump.size() > 0) { answerArray[colorsToDump.pop()] = index; } if (desiredColorToIndexMap.get(painterColor).size() == 0) { desiredColorToIndexMap.remove(painterColor); } } else { if (colorToIndexMap.containsKey(painterColor)) { while (colorsToDump.size() > 0) { answerArray[colorsToDump.pop()] = colorToIndexMap.get(painterColor); } answerArray[i] = colorToIndexMap.get(painterColor); } else { colorsToDump.push(i); } } }); if (colorsToDump.size() > 0 || desiredColorToIndexMap.size() > 0) { System.out.println("NO"); } else { System.out.println("YES"); Arrays.stream(answerArray).forEach(ans -> System.out.print(ans+1 + " ")); System.out.println(); } }); 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 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
8288b632ac6713806ea58ea80d8a09e9
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.util.stream.IntStream; public class P1481CR { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int noOfTestCases = sc.nextInt(); IntStream.range(0, noOfTestCases).forEach(t -> { int n = sc.nextInt(); int m = sc.nextInt(); int[] current = IntStream.range(0, n).map(i -> sc.nextInt()).toArray(); Map<Integer, Integer> colorToIndexMap = new HashMap<>(); Map<Integer, Stack<Integer>> desiredColorToIndexMap = new HashMap<>(); int[] answerArray = new int[m]; Stack<Integer> colorsToDump = new Stack<>(); IntStream.range(0, n).forEach(i -> { int desiredColor = sc.nextInt(); if (current[i] == desiredColor) { colorToIndexMap.put(desiredColor, i); } else { if (desiredColorToIndexMap.containsKey(desiredColor)) { desiredColorToIndexMap.get(desiredColor).push(i); } else { Stack<Integer> indexList = new Stack<>(); indexList.push(i); desiredColorToIndexMap.put(desiredColor, indexList); } } }); IntStream.range(0, m).forEach(i -> { int painterColor = sc.nextInt(); if (desiredColorToIndexMap.containsKey(painterColor) && desiredColorToIndexMap.get(painterColor).size() > 0) { int index = desiredColorToIndexMap.get(painterColor).pop(); colorToIndexMap.put(painterColor, index); answerArray[i] = index; while (colorsToDump.size() > 0) { answerArray[colorsToDump.pop()] = index; } if (desiredColorToIndexMap.get(painterColor).size() == 0) { desiredColorToIndexMap.remove(painterColor); } } else { if (colorToIndexMap.containsKey(painterColor)) { while (colorsToDump.size() > 0) { answerArray[colorsToDump.pop()] = colorToIndexMap.get(painterColor); } answerArray[i] = colorToIndexMap.get(painterColor); } else { colorsToDump.push(i); } } }); if (colorsToDump.size() > 0 || desiredColorToIndexMap.size() > 0) { System.out.println("NO"); } else { System.out.println("YES"); Arrays.stream(answerArray).forEach(ans -> System.out.print(ans+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
c184892c41db17fb16a57ad2b8c653da
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 codeforce.cf699; import java.io.PrintWriter; import java.util.*; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); // int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(), m = in.nextInt(); int[][] arr = new int[n][2]; int[] cs = new int[m]; // initial colors of the fence. for (int i = 0; i < n; i++) { arr[i][0] = in.nextInt(); } // desired colors of the fence. for (int i = 0; i < n; i++) { arr[i][1] = in.nextInt(); } // the colors painters have. for (int i = 0; i < m; i++) { cs[i] = in.nextInt(); } Map<Integer, LinkedList<Integer>> mp = new HashMap<>(); LinkedList<Integer> afterSame = new LinkedList<>(); Map<Integer, LinkedList<Integer>> same = new HashMap<>(); for (int i = 0; i < n; i++) { if (arr[i][0] != arr[i][1]){ if (!mp.containsKey(arr[i][1])) mp.put(arr[i][1], new LinkedList<>()); mp.get(arr[i][1]).add(i); }else{ if (!same.containsKey(arr[i][1])) same.put(arr[i][1], new LinkedList<>()); same.get(arr[i][1]).add(i); } } int[] ans = new int[m]; for (int i = m - 1; i >= 0; i--) { if (mp.containsKey(cs[i])){ int id = mp.get(cs[i]).poll(); ans[i] = id + 1; afterSame.add(id); if (mp.get(cs[i]).size() == 0) mp.remove(cs[i]); }else if (afterSame.size() > 0){ int get = afterSame.peek(); ans[i] = get + 1; }else if (same.containsKey(cs[i])){ int fk = same.get(cs[i]).poll(); ans[i] = fk + 1; afterSame.add(fk); if (same.get(cs[i]).size() == 0) same.remove(cs[i]); }else{ out.println("NO"); return; } } if (mp.size() == 0){ out.println("YES"); for(int x : ans){ out.print(x+" "); } out.println(); }else{ 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
ea546d59793cf461e6d7a5d837c68d1e
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 static java.lang.System.currentTimeMillis; public class Sol{ static class Pair{ int cc,fc, id; public Pair(int id, int cc,int fc){ this.id = id; this.cc = cc; this.fc=fc; } } public static void main(String []args){ InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in=new FastReader(inputStream); PrintWriter out=new PrintWriter(outputStream); //long start = currentTimeMillis(); int t=in.nextInt();; while(t-->0){ int n=in.nextInt();int m=in.nextInt(); /*--------Start---------*/ Pair p[]=new Pair[n+1]; Pair c[]=new Pair[m+1];p[0]=new Pair(0,0,0);c[0]=new Pair(0,0,0); for(int i=1;i<=n;i++){int a=in.nextInt();p[i]=new Pair(i,a,0);} for(int i=1;i<=n;i++){int b=in.nextInt();p[i].fc=b;} for(int i=1;i<=m;i++){int color=in.nextInt();c[i]=new Pair(i,0,color);} int color=c[m].fc; Arrays.sort(p , (o1,o2) -> o1.fc - o2.fc ); Arrays.sort(c , (o1,o2) -> o1.fc - o2.fc ); int k=0; for(int i=1;i<=n&&k==0;i++){ if(p[i].fc==color) {int j=i; while(j<=n){if(p[j].fc==color&&p[j].cc==color)j++;else break;} if(j<=n){if(p[j].fc==color)k=j;else k=j-1;}else{k=j-1;}} } /*out.println(k); for(int i=1;i<=n;i++)out.print(p[i].cc+" "); out.println(); for(int i=1;i<=n;i++)out.print(p[i].fc+" "); out.println(); */ boolean ans=true; if(k==0)ans=false; int res[]=new int[m+1]; int i=1;int j=1; while(ans&&i<=n && j<=m){ if(c[j].id==m)j++;if(i==k)i++; if(j>m || i>n )break; if(p[i].cc==p[i].fc){i++;} else{if(p[i].fc==c[j].fc){res[c[j].id]=p[i].id;i++;j++;} else{res[c[j].id]=p[k].id;j++;} } } while(j<=m&&ans){res[c[j].id]=p[k].id;j++;} for(i=1;i<=m;i++){if(c[i].id==m){res[m]=p[k].id;break;}} Arrays.sort(p,(o1,o2)->o1.id-o2.id); Arrays.sort(c,(o1,o2)->o1.id-o2.id); for(i=1;i<=m;i++){p[res[i]].cc=c[i].fc;} /*for(i=1;i<=m;i++)out.print(res[i]+ " ");*/ for(i=1;i<=n && ans ;i++){if(p[i].cc==p[i].fc){}else{ans=false;}} if(ans&&k>0){out.println("YES");for(i=1;i<=m;i++){out.print(res[i]+" ");} out.println();} else{out.println("NO");} } out.close(); //System.out.println(currentTimeMillis() - start); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } public 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()); } public long nextLong() { return Long.parseLong(next()); } public 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
c68b3c0c6f71a81d70397d884ae139ec
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.math.*; import java.io.*; public class A{ static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static LinkedList<Integer>edges[]; static boolean stdin = true; static String filein = "input"; static String fileout = "output"; static int dx[] = { -1, 0, 1, 0 }; static int dy[] = { 0, 1, 0, -1 }; int dx_8[]={1,1,1,0,0,-1,-1,-1}; int dy_8[]={-1,0,1,-1,1,-1,0,1}; static char sts[]={'U','R','D','L'}; static boolean prime[]; static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static int countSetBits(int n) { int count = 0; while (n > 0) { if((n&1)!=1) count++; //count += n & 1; n >>= 1; } return count; } static void sieve(long n) { prime = new boolean[(int)n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int perm=0,FOR=0; static boolean flag=false; static int len=100000000; static ArrayList<Pair>inters=new ArrayList<Pair>(); static StringBuilder sb; static void swap(int i,int j,StringBuilder st) { char tmp=st.charAt(i); st.setCharAt(i,st.charAt(j)); st.setCharAt(j,tmp); } private static int next(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if(arr[mid]==target) return mid; if (arr[mid] <target) { start = mid + 1; } // Move left side. else { ans = mid; end = mid - 1; } } return ans; } //static boolean vis[][]; static long solve(int h,long n,int cur) { if(h==0) return 0; long half=1L<<(h-1); if(n<=half) { if((cur^1)==0) return 1+solve(h-1,n,0); else return 2*half+solve(h-1,n,0); } else { if((cur^1)==0) return 2*half+solve(h-1,n-half,1); else return 1+solve(h-1,n-half,1); } } public static class comp1 implements Comparator<String>{ public int compare(String o1,String o2){ return o1.length()-o2.length(); } } public static class comp2 implements Comparator<String>{ public int compare(String o1,String o2){ return o1.compareTo(o2); } } static StringBuilder a,b; static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static ArrayList<Integer>v; static ArrayList<Integer>pows; static void block(long x) { v = new ArrayList<Integer>(); pows=new ArrayList<Integer>(); while (x > 0) { v.add((int)x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.size(); i++) { if (v.get(i)==1) { pows.add(i); } } } static long mod=(long)(1e9)+7; static int countWaysUtil(int x, int n, int num) { // Base cases int val = (int) (x - Math.pow(num, n)); if (val == 0) return 1; if (val < 0) return 0; // Consider two possibilities, num is // included and num is not included. return countWaysUtil(val, n, num + 1) + countWaysUtil(x, n, num + 1); } static int countWays(int x, int n) { return countWaysUtil(x, n, 1); } static ArrayList<Integer>arr; static int n,k; static int dp[][]; static int rec(int l,int r,int i,int j) { if(i>j) { if(r-l>k) return (int)1e7+5; return 0; } if(r-l<=k) return 0; int x=(int)1e7+5; if(dp[l][r]!=-1) return dp[l][r]; if(arr.get(i)-l<=k) { return x=Math.min(rec(arr.get(i),r,i+1,j)+1,rec(l,r,i+1,j)); } else if(r-arr.get(j)<=k) { x=Math.min(rec(l,arr.get(j),i,j-1)+1,rec(l,r,i,j-1)); } return dp[l][r]=x; } public static void main(String[] args) throws Exception { //SUCK IT UP AND DO IT ALRIGHT //scan=new FastReader("div7.in"); //out = new PrintWriter("div7.out"); //System.out.println(countSetBits(2015)); //int elem[]={1,2,3,4,5}; //System.out.println("avjsmlfpb".compareTo("avjsmbpfl")); int tt=1; /*for(int i=0;i<=100;i++) if(prime[i]) arr.add(i); System.out.println(arr.size());*/ // check(new StringBuilder("05:11")); // System.out.println(26010000000000L%150); tt=scan.nextInt(); // System.out.println(2^6^4); outer:while(tt-->0) { int n=scan.nextInt(),m=scan.nextInt(); //System.out.println(n+" "+m); ArrayList<Integer>equal[]=new ArrayList[n+1]; ArrayList<Integer>notequal[]=new ArrayList[n+1]; TreeSet<Integer>yes=new TreeSet<Integer>(); TreeSet<Integer>yes2=new TreeSet<Integer>(); for(int i=0;i<n+1;i++) { equal[i]=new ArrayList(); notequal[i]=new ArrayList(); } int a[]=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++) a[i]=scan.nextInt(); for(int i=0;i<n;i++) b[i]=scan.nextInt(); boolean vis[]=new boolean[n+1]; for(int i=0;i<n;i++) { if(a[i]==b[i]){ equal[b[i]].add(i); vis[b[i]]=true; yes2.add(i); } else { yes.add(i); notequal[b[i]].add(i); } } int res[]=new int[m]; boolean is=false; int arr[]=new int[m]; int cnt[]=new int[n+1]; Map<Integer,Integer>map=new HashMap<Integer,Integer>(); for(int i=0;i<m;i++) { arr[i]=scan.nextInt(); if(map.containsKey(arr[i])) map.put(arr[i],map.get(arr[i])+1); else map.put(arr[i],1); } that:for(int i=0;i<m;i++) { boolean no=true; int x=arr[i]; if(is) continue; if(notequal[x].size()>0) { yes.remove(notequal[x].get(0)); a[notequal[x].get(0)]=b[notequal[x].get(0)]; res[i]=notequal[x].get(0); equal[x].add(notequal[x].get(0)); yes2.add(notequal[x].get(0)); notequal[x].remove(0); } else if(equal[x].size()>0) { res[i]=equal[x].get(0); } else { if(yes.size()>0) { res[i]=yes.first(); // is=true; //continue that; } else { no=false; int finkey=-1; for(int key:map.keySet()) { if(k==x) { if(equal[key].size()>0&&map.get(key)-1>0) { finkey=key; break; } else if(notequal[key].size()>0&&map.get(key)-1>0) { finkey=key; break; } } else { if(equal[key].size()>0&&map.get(key)-1>=0) { finkey=key; break; } else if(notequal[key].size()>0&&map.get(key)-1>=0) { finkey=key; break; } } } if(finkey==-1){ if(map.get(x)-1>0) { map.put(x,map.get(x)-1); } else map.remove(x); // out.println(i); is=true; continue that; } if(equal[finkey].size()>0) { res[i]=equal[finkey].get(0); } else { res[i]=notequal[finkey].get(0); } } } //System.out.println(x); if(map.get(x)-1>0) { map.put(x,map.get(x)-1); } else map.remove(x); } if(is) { //out.println("FUCK"); out.println("NO"); continue outer; } for(int i=0;i<n;i++) if(a[i]!=b[i]) { out.println("NO"); continue outer; } out.println("YES"); for(int i=0;i<m;i++) out.print((res[i]+1)+" "); out.println(); } out.close(); //SEE UP } static class special{ char c; int idx; special(char c,int idx) { this.c=c; this.idx=idx; } } static long binexp(long a,long n) { if(n==0) return 1; long res=binexp(a,n/2); if(n%2==1) return res*res*a; else return res*res; } static long fastPow(int base, int pow) { if (pow==0) return 1; long half=fastPow(base, pow/2); if (pow%2==0) return half*half%mod; return half*half%mod*base%mod; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return (base % mod+mod)%mod; long R = (powMod(base, exp/2, mod) % mod+mod)%mod; R *= R; R %= mod; if ((exp & 1) == 1) { return (base * R % mod+mod)%mod; } else return (R %mod+mod)%mod; } static double dis(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); //Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private static void sort2(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } boolean hasNext(){ String line; while(root.hasMoreTokens()) return true; return false; } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } static class Pair implements Comparable<Pair>{ public int x, y; public Pair(int x1, int y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { return (o.y-y); } } static class tuple{ int x,y,z; tuple(int a,int b,int c){ x=a; y=b; z=c; } } static class Edge{ int d,w; Edge(int d,int w) { this.d=d; this.w=w; } } }
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
2d068b97c1b5255c485dd5e32b0a3891
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import static java.lang.Math.*; /* Name of the class has to be "Main" only if the class is public. */ // class Codechef public class CFencePainting { static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) throws java.lang.Exception { int t = in.nextInt(); test: while (t-- > 0) { int n = in.nextInt(); int m = in.nextInt(); int a[] = in.nextIntArray(n); int b[] = in.nextIntArray(n); int c[] = in.nextIntArray(m); int diff=0; int req[] = new int[100001]; for(int i=n-1;i>=0;i--){ if(b[i] != a[i]){ if(req[b[i]]++ == 0)diff++; } } for(int e:c){ if(--req[e] == 0)diff--; } if(diff != 0){ out.printLine("NO"); continue; } int at[] =req; Arrays.fill(at, -1); int preindex[] = new int[n]; for(int i=0;i<n;i++){ int pre = at[b[i]]; preindex[i] = pre; at[b[i]] = i; } int ans[] = new int[m]; int ext = -1; outer: for(int i=m-1;i>=0;i--){ int paint = c[i]; int same = -1; while(at[paint]>=0){ if(paint == b[at[paint]]){ if(a[at[paint]] == paint){ same = max(same, at[paint]); ans[i] = same; }else{ ans[i] = at[paint]; ext = max(ext, ans[i]); at[paint] = preindex[at[paint]]; continue outer; } } at[paint] = preindex[at[paint]]; } ext = max(ext, same); if(same != -1){ continue; } if(ext == -1){ out.printLine("No"); continue test; } ans[i] = ext; } out.printLine("Yes"); for(int e: ans){ out.print(e+1+" "); } out.printLine(); } // in.close(); out.flush(); out.close(); } private static class InputReader implements AutoCloseable { /** * The default size of the InputReader's buffer is 2<sup>16</sup>. */ private static final int DEFAULT_BUFFER_SIZE = 1 << 16; /** * The default stream for the InputReader is standard input. */ private static final InputStream DEFAULT_STREAM = System.in; /** * The maximum number of accurate decimal digits the method * {@link #nextDoubleFast() nextDoubleFast()} can read. Currently this value is * set to 21 because it is the maximum number of digits a double precision float * can have at the moment. */ private static final int MAX_DECIMAL_PRECISION = 21; // 'c' is used to refer to the current character in the stream private int c; // Variables associated with the byte buffer. private final byte[] buf; private final int bufferSize; private int bufIndex; private int numBytesRead; private final InputStream stream; // End Of File (EOF) character private static final byte EOF = -1; // New line character: '\n' private static final byte NEW_LINE = 10; // Space character: ' ' private static final byte SPACE = 32; // Dash character: '-' private static final byte DASH = 45; // Dot character: '.' private static final byte DOT = 46; // A reusable character buffer when reading string data. private char[] charBuffer; // Primitive data type lookup tables used for optimizations private static final byte[] bytes = new byte[58]; private static final int[] ints = new int[58]; private static final char[] chars = new char[128]; static { char ch = ' '; int value = 0; byte _byte = 0; for (int i = 48; i < 58; i++) bytes[i] = _byte++; for (int i = 48; i < 58; i++) ints[i] = value++; for (int i = 32; i < 128; i++) chars[i] = ch++; } // Primitive double lookup table used for optimizations. private static final double[][] doubles = { { 0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d }, { 0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d }, { 0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d }, { 0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d }, { 0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d }, { 0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d }, { 0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d }, { 0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d }, { 0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d }, { 0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d } }; /** * Create an InputReader that reads from standard input. */ public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); } /** * Create an InputReader that reads from standard input. * * @param bufferSize The buffer size for this input reader. */ public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); } /** * Create an InputReader that reads from standard input. * * @param stream Takes an InputStream as a parameter to read from. */ public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); } /** * Create an InputReader that reads from standard input. * * @param stream Takes an {@link java.io.InputStream#InputStream() * InputStream} as a parameter to read from. * @param bufferSize The size of the buffer to use. */ public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException(); buf = new byte[bufferSize]; charBuffer = new char[128]; this.bufferSize = bufferSize; this.stream = stream; } /** * Reads a single character from the input stream. * * @return Returns the byte value of the next character in the buffer and EOF at * the end of the stream. * @throws IOException throws exception if there is no more data to read */ private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException(); if (bufIndex >= numBytesRead) { bufIndex = 0; numBytesRead = stream.read(buf); if (numBytesRead == EOF) return EOF; } return buf[bufIndex++]; } /** * Read values from the input stream until you reach a character with a higher * ASCII value than 'token'. * * @param token The token is a value which we use to stop reading junk out of * the stream. * @return Returns 0 if a value greater than the token was reached or -1 if the * end of the stream was reached. * @throws IOException Throws exception at end of stream. */ private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF; // Seek to the first valid position index do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0; bufIndex++; } // reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) return EOF; bufIndex = 0; } while (true); } /** * Reads a single byte from the input stream. * * @return The next byte in the input stream * @throws IOException Throws exception at end of stream. */ public byte nextByte() throws IOException { return (byte) nextInt(); } /** * Reads a 32 bit signed integer from input stream. * * @return The next integer value in the stream. * @throws IOException Throws exception at end of stream. */ public int nextInt() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException(); int sgn = 1, res = 0; c = buf[bufIndex]; if (c == DASH) { sgn = -1; bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1); res += ints[buf[bufIndex++]]; } else { bufIndex++; return res * sgn; } } // Reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) return res * sgn; bufIndex = 0; } while (true); } /** * Reads a 64 bit signed long from input stream. * * @return The next long value in the stream. * @throws IOException Throws exception at end of stream. */ public long nextLong() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException(); int sgn = 1; long res = 0L; c = buf[bufIndex]; if (c == DASH) { sgn = -1; bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1); res += ints[buf[bufIndex++]]; } else { bufIndex++; return res * sgn; } } // Reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) return res * sgn; bufIndex = 0; } while (true); } /** * Doubles the size of the internal char buffer for strings */ private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1]; for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i]; charBuffer = newBuffer; } /** * Reads a line from the input stream. * * @return Returns a line from the input stream in the form a String not * including the new line character. Returns <code>null</code> when * there are no more lines. * @throws IOException Throws IOException when something terrible happens. */ public String nextLine() throws IOException { try { c = read(); } catch (IOException e) { return null; } if (c == NEW_LINE) return ""; // Empty line if (c == EOF) return null; // EOF int i = 0; charBuffer[i++] = (char) c; do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE) { if (i == charBuffer.length) doubleCharBufferSize(); charBuffer[i++] = (char) buf[bufIndex++]; } else { bufIndex++; return new String(charBuffer, 0, i); } } // Reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) return new String(charBuffer, 0, i); bufIndex = 0; } while (true); } // Reads a string of characters from the input stream. // The delimiter separating a string of characters is set to be: // any ASCII value <= 32 meaning any spaces, new lines, EOF, tabs... public String nextString() throws IOException { if (numBytesRead == EOF) return null; if (readJunk(SPACE) == EOF) return null; for (int i = 0;;) { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize(); charBuffer[i++] = (char) buf[bufIndex++]; } else { bufIndex++; return new String(charBuffer, 0, i); } } // Reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) return new String(charBuffer, 0, i); bufIndex = 0; } } // Returns an exact value a double value from the input stream. public double nextDouble() throws IOException { String doubleVal = nextString(); if (doubleVal == null) throw new IOException(); return Double.valueOf(doubleVal); } // Very quickly reads a double value from the input stream (~3x faster than // nextDouble()). However, // this method may provide a slightly less accurate reading than .nextDouble() // if there are a lot // of digits (~16+). In particular, it will only read double values with at most // 21 digits after // the decimal point and the reading my be as inaccurate as ~5*10^-16 from the // true value. public double nextDoubleFast() throws IOException { c = read(); int sgn = 1; while (c <= SPACE) c = read(); // while c is either: ' ', '\n', EOF if (c == DASH) { sgn = -1; c = read(); } double res = 0.0; // while c is not: ' ', '\n', '.' or -1 while (c > DOT) { res *= 10.0; res += ints[c]; c = read(); } if (c == DOT) { int i = 0; c = read(); // while c is digit and we are less than the maximum decimal precision while (c > SPACE && i < MAX_DECIMAL_PRECISION) { res += doubles[ints[c]][i++]; c = read(); } } return res * sgn; } // Read an array of n byte values public byte[] nextByteArray(int n) throws IOException { byte[] ar = new byte[n]; for (int i = 0; i < n; i++) ar[i] = nextByte(); return ar; } // Read an integer array of size n public int[] nextIntArray(int n) throws IOException { int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = nextInt(); return ar; } // Read a long array of size n public long[] nextLongArray(int n) throws IOException { long[] ar = new long[n]; for (int i = 0; i < n; i++) ar[i] = nextLong(); return ar; } // read an of doubles of size n public double[] nextDoubleArray(int n) throws IOException { double[] ar = new double[n]; for (int i = 0; i < n; i++) ar[i] = nextDouble(); return ar; } // Quickly read an array of doubles public double[] nextDoubleArrayFast(int n) throws IOException { double[] ar = new double[n]; for (int i = 0; i < n; i++) ar[i] = nextDoubleFast(); return ar; } // Read a string array of size n public String[] nextStringArray(int n) throws IOException { String[] ar = new String[n]; for (int i = 0; i < n; i++) { String str = nextString(); if (str == null) throw new IOException(); ar[i] = str; } return ar; } // Read a 1-based byte array of size n+1 public byte[] nextByteArray1(int n) throws IOException { byte[] ar = new byte[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextByte(); return ar; } // Read a 1-based integer array of size n+1 public int[] nextIntArray1(int n) throws IOException { int[] ar = new int[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextInt(); return ar; } // Read a 1-based long array of size n+1 public long[] nextLongArray1(int n) throws IOException { long[] ar = new long[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextLong(); return ar; } // Read a 1-based double array of size n+1 public double[] nextDoubleArray1(int n) throws IOException { double[] ar = new double[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextDouble(); return ar; } // Quickly read a 1-based double array of size n+1 public double[] nextDoubleArrayFast1(int n) throws IOException { double[] ar = new double[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextDoubleFast(); return ar; } // Read a 1-based string array of size n+1 public String[] nextStringArray1(int n) throws IOException { String[] ar = new String[n + 1]; for (int i = 1; i <= n; i++) ar[i] = nextString(); return ar; } // Read a two dimensional matrix of bytes of size rows x cols public byte[][] nextByteMatrix(int rows, int cols) throws IOException { byte[][] matrix = new byte[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextByte(); return matrix; } // Read a two dimensional matrix of ints of size rows x cols public int[][] nextIntMatrix(int rows, int cols) throws IOException { int[][] matrix = new int[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextInt(); return matrix; } // Read a two dimensional matrix of longs of size rows x cols public long[][] nextLongMatrix(int rows, int cols) throws IOException { long[][] matrix = new long[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextLong(); return matrix; } // Read a two dimensional matrix of doubles of size rows x cols public double[][] nextDoubleMatrix(int rows, int cols) throws IOException { double[][] matrix = new double[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextDouble(); return matrix; } // Quickly read a two dimensional matrix of doubles of size rows x cols public double[][] nextDoubleMatrixFast(int rows, int cols) throws IOException { double[][] matrix = new double[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextDoubleFast(); return matrix; } // Read a two dimensional matrix of Strings of size rows x cols public String[][] nextStringMatrix(int rows, int cols) throws IOException { String[][] matrix = new String[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextString(); return matrix; } // Read a 1-based two dimensional matrix of bytes of size rows x cols public byte[][] nextByteMatrix1(int rows, int cols) throws IOException { byte[][] matrix = new byte[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextByte(); return matrix; } // Read a 1-based two dimensional matrix of ints of size rows x cols public int[][] nextIntMatrix1(int rows, int cols) throws IOException { int[][] matrix = new int[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextInt(); return matrix; } // Read a 1-based two dimensional matrix of longs of size rows x cols public long[][] nextLongMatrix1(int rows, int cols) throws IOException { long[][] matrix = new long[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextLong(); return matrix; } // Read a 1-based two dimensional matrix of doubles of size rows x cols public double[][] nextDoubleMatrix1(int rows, int cols) throws IOException { double[][] matrix = new double[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextDouble(); return matrix; } // Quickly read a 1-based two dimensional matrix of doubles of size rows x cols public double[][] nextDoubleMatrixFast1(int rows, int cols) throws IOException { double[][] matrix = new double[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextDoubleFast(); return matrix; } // Read a 1-based two dimensional matrix of Strings of size rows x cols public String[][] nextStringMatrix1(int rows, int cols) throws IOException { String[][] matrix = new String[rows + 1][cols + 1]; for (int i = 1; i <= rows; i++) for (int j = 1; j <= cols; j++) matrix[i][j] = nextString(); return matrix; } // Closes the input stream public void close() throws IOException { stream.close(); } } private static class OutputWriter implements AutoCloseable { 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(); } } }
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
6dd52d730bb77d6cebaddfea2f78287b
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.text.DecimalFormat; import java.util.*; public class Main { static long mod = (long) 1e9 + 7; static long mod1 = 998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int m = in.nextInt(); int[] a = in.readArray(n); int[] b = in.readArray(n); int[] c = in.readArray(m); HashMap<Integer, Stack<Integer>> paint_index = new HashMap<>(); for (int i = 0; i < m; i++) { Stack<Integer> ll; if (paint_index.containsKey(c[i])) { ll = paint_index.get(c[i]); } else { ll = new Stack<>(); } ll.push(i); paint_index.put(c[i], ll); } // for(Map.Entry<Integer,Stack<Integer>> entry : paint_index.entrySet()){ // out.println(entry.getKey()+"----> "+entry.getValue()); // } int[] sol = new int[m]; boolean flag = true; boolean all_equal = true; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { all_equal = false; if (paint_index.containsKey(b[i])) { Stack<Integer> ll = paint_index.get(b[i]); if (ll.size() == 0) { flag = false; break; } else { a[i]=b[i]; // out.println("Hello"); sol[ll.pop()] = i + 1; } } else { flag = false; break; } } } HashMap<Integer, Integer> a_index = new HashMap<>(); for (int i = 0; i < n; i++) { if (!a_index.containsKey(a[i])) a_index.put(a[i], i + 1); } if(sol[m-1]!=0){ for(int i=0;i<m;i++) { if(sol[i]==0) sol[i] = sol[m - 1]; } } else { if(a_index.containsKey(c[m-1])) { int index = a_index.get(c[m - 1]); for (int i = 0; i < m; i++) { if (sol[i] == 0) sol[i] = index; } }else flag=false; } // if (sol[m - 1] == 0) // flag = false; if (flag) { out.println("YES"); for (int i : sol) out.print(i + " "); out.println(); } else out.println("NO"); } out.close(); } 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), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static long gcd(long x, long y) { if (x == 0) return y; if (y == 0) return x; long r = 0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static long modulo(long a, long b, long c) { long x = 1, y = a % c; while (b > 0) { if (b % 2 == 1) x = (x * y) % c; y = (y * y) % c; b = b >> 1; } return x % c; } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } static String printPrecision(double d) { DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask) { int ans = 0; while (mask != 0) { mask &= (mask - 1); ans++; } return ans; } 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()); } public int[] readArray(int n) { 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
e17d933e23f0bf4a5f0c14456992a2f9
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.util.ArrayList; public class FencePainting { public static void main(String[] args) throws Exception { BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(buffer.readLine()); while (t-- > 0) { String [] inp = buffer.readLine().split(" "); int n = Integer.parseInt(inp[0]), m = Integer.parseInt(inp[1]); int [] a = new int[n], b = new int[n], c = new int[m]; inp = buffer.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(inp[i]); } ArrayList<Integer>[]colours = new ArrayList[n+1]; for (int i = 0; i < n + 1; i++) { colours[i] = new ArrayList<>(); } inp = buffer.readLine().split(" "); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(inp[i]); if (a[i] != b[i]) colours[b[i]].add(i); } inp = buffer.readLine().split(" "); for (int i = 0; i < m; i++) { c[i] = Integer.parseInt(inp[i]); } int lastFencePos = -1; int temp = colours[c[m-1]].size(); if (temp > 0){ lastFencePos = colours[c[m-1]].get(temp-1); colours[c[m-1]].remove(temp-1); } else { for (int i = 0; i < n; i++) { if (b[i] == c[m-1]) lastFencePos = i; } } int [] ans = new int[m]; ans[m-1] = lastFencePos; if (lastFencePos == -1) sb.append("NO\n"); else { for (int i = 0; i < m - 1; i++) { if (colours[c[i]].size() > 0){ temp = colours[c[i]].size(); ans[i] = colours[c[i]].get(temp-1); colours[c[i]].remove(temp-1); } else ans[i] = lastFencePos; } boolean check = true; for (ArrayList<Integer> colour : colours) { if (colour.size() > 0) { check = false; break; } } if (!check) sb.append("NO\n"); else { sb.append("YES\n"); for (int fence : ans) { sb.append(fence+1).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 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
2f4234f01ff1fa52b045b96196db4262
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{ public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int T = Integer.parseInt(bf.readLine()); for(int testcases = 0; testcases < T; testcases++){ StringTokenizer stk = new StringTokenizer(bf.readLine()); int N = Integer.parseInt(stk.nextToken()); int M = Integer.parseInt(stk.nextToken()); stk = new StringTokenizer(bf.readLine()); int[] a = new int[N]; for(int i = 0; i < N; i++)a[i] = Integer.parseInt(stk.nextToken()); stk = new StringTokenizer(bf.readLine()); int[] b = new int[N]; for(int i = 0; i < N; i++)b[i] = Integer.parseInt(stk.nextToken()); stk = new StringTokenizer(bf.readLine()); int[] c = new int[M]; for(int i = 0; i < M; i++)c[i] = Integer.parseInt(stk.nextToken()); boolean possible = false; ArrayList<Integer> possibleindex = new ArrayList<>(); int index = 0; for(int i = 0; i < N; i++){ if(b[i]==c[M-1]){ possible = true; possibleindex.add(i); } } if(!possible){ System.out.println("NO"); continue; } index = possibleindex.get(0); for(int i = 0; i < possibleindex.size(); i++){ if(a[possibleindex.get(i)]!=b[possibleindex.get(i)])index = possibleindex.get(i); } HashMap<Integer, ArrayList<Integer>> map = new HashMap<>(); for(int i = 0; i < N; i++){ if(i == index)continue; if(a[i]!=b[i]){ if(!map.containsKey(b[i])){ ArrayList<Integer> arr = new ArrayList<>(); arr.add(i); map.put(b[i], arr); } else map.get(b[i]).add(i); } } ArrayList<Integer> moves = new ArrayList<>(); for(int i = 0; i < M; i++){ int color = c[i]; if(map.containsKey(color)){ moves.add(map.get(color).remove(0)); if(map.get(color).isEmpty())map.remove(color); a[moves.get(moves.size()-1)] = color; } else{ moves.add(index); a[index] = color; } } boolean solved = true; for(int i = 0; i < N; i++){ if(a[i]!=b[i])solved = false; } if(!solved){ System.out.println("NO"); continue; } System.out.println("YES"); for(int i = 0; i < M; i++)System.out.print(moves.get(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
fb8c2cf3dd77a78b97e03a2ab500acb6
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.io.File; import java.util.*; public class Main { public static void main(String[] args) { try{ File file = new File("in.rtf"); Scanner input = null; if(file.exists()) input = new Scanner(file); else input = new Scanner(System.in); //---------------------INPUT------------ int t = input.nextInt(); int n,m; int ai[], bi[], ci[]; Map<Integer, Integer> myMap; Map<Integer, List<Integer>> mem; for(int i =0;i<t;i++){ n = input.nextInt(); m=input.nextInt(); ai = new int[n]; bi = new int[n]; ci = new int[m]; for(int j=0;j<n;j++) ai[j] = input.nextInt(); for(int j=0;j<n;j++) bi[j] = input.nextInt(); myMap = new HashMap<Integer, Integer>(); mem = new HashMap<Integer, List<Integer>>(); for(int j=0;j<m;j++){ int temp = input.nextInt(); ci[j] = temp; if(myMap.containsKey(temp)) myMap.put(temp, myMap.get(temp)+1); else{ myMap.put(temp, 1); List<Integer> tempList = new LinkedList<Integer>(); mem.put(temp, tempList); } } boolean sol = false; for(int j=0;j<n;j++){ if(ai[j]!=bi[j] && (myMap.containsKey(bi[j])==false || myMap.get(bi[j])==0)) { System.out.println("NO"); sol = true; break; } else if(ai[j]!=bi[j]){ ai[j] = bi[j]; myMap.put(bi[j], myMap.get(bi[j])-1); List<Integer> tempList = mem.get(bi[j]); tempList.add(j+1); mem.put(bi[j], tempList); } } /*for(int j=0;j<n;j++) System.out.print(ai[j]+" "); System.out.println(); for(int j=0;j<n;j++) System.out.print(bi[j]+" "); System.out.println(); Set<Integer> keys1 = myMap.keySet(); for(Integer key: keys1){ System.out.print(key+": "+myMap.get(key)); System.out.println(); } System.out.println(); */ if(sol) continue; boolean is_exist = false; int choosen=0; for(int j =0;j<n;j++){ if(ai[j]==ci[ci.length-1]) { is_exist = true; List<Integer> tempList = mem.get(ai[j]); for(int k =0;k<myMap.get(ai[j]);k++) tempList.add(j+1); myMap.put(ai[j], 0); mem.put(ai[j], tempList); choosen = ai[j]; } } if(is_exist==false){ System.out.println("NO"); continue; } for(int j=0;j<m;j++){ if(myMap.get(ci[j])>0){ int temp = myMap.get(ci[j]); List<Integer> tempList = mem.get(ci[j]); for(int k=0;k<temp;k++) tempList.add(mem.get(choosen).get(mem.get(choosen).size()-1)); myMap.put(ci[j], 0); } } //--------------- /*for(int j=0;j<n;j++){ if(myMap.containsKey(bi[j]) && myMap.get(bi[j])>0 && ai[j]==bi[j]){ List<Integer> tempList = mem.get(bi[j]); for(int k=0;k<myMap.get(bi[j]);k++) tempList.add(j+1); myMap.put(bi[j], 0); mem.put(bi[j], tempList); } } Set<Integer> keys = mem.keySet(); for(int j = 0;j<m;j++){ List<Integer> tempList1 = mem.get(ci[j]); if(myMap.get(ci[j])>0){ boolean is_sol = false; for(int k=j+1;k<m;k++) { if(mem.get(ci[k]).size()>0) { tempList1.add(mem.get(ci[k]).get(0)); is_sol = true; break; } } if(is_sol==false){ System.out.println("NO"); sol = true; break; } mem.put(ci[j], tempList1); myMap.put(ci[j], myMap.get(ci[j])-1); } } if(sol) continue;*/ // for(Integer key: keys){ // System.out.print(key+": "); // for(Integer intq: mem.get(key)) // System.out.print(intq+" "); // System.out.println(); // } // System.out.println(); int[] result = new int[m]; for(int j=0;j<m;j++){ LinkedList<Integer> tempList = (LinkedList<Integer>) mem.get(ci[j]); if(myMap.get(ci[j])>0 || tempList.size()==0){ System.out.println("NO"); sol = true; break; } result[j] = tempList.getFirst(); tempList.removeFirst(); } if(sol) continue; System.out.println("YES"); for(int j=0;j<m;j++){ if(j+1==m) System.out.println(result[j]); else System.out.print(result[j]+" "); } } input.close(); } catch (Exception e) { e.printStackTrace(); } } } /* 6 7 9 9 5 13 6 11 5 1 4 2 6 2 5 18 10 3 17 9 6 7 9 9 5 13 6 11 5 2 4 2 6 19 9 18 16 3 17 18 6 20 3 20 8 9 1 12 16 14 14 19 19 18 16 17 15 9 1 18 3 2 7 2 5 */
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
94b99e5ae8f2a9198a0a7c8e5d365de4
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.*; public class Main { 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; } } static class HashMultiSet<T> implements Iterable<T> { private final HashMap<T,Integer> map; private int size; public HashMultiSet(){map=new HashMap<>(); size=0;} public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public Iterator<T> iterator() { return new Iterator<>() { private final Iterator<T> iter = map.keySet().iterator(); private int count = 0; private T curElement; public boolean hasNext(){return iter.hasNext()||count>0;} public T next() { if(count==0) { curElement=iter.next(); count=get(curElement); } count--; return curElement; } }; } } private static long abs(long x){ if(x < 0) x*=-1; return x; } public static int get(HashMap<Integer, Deque<Integer> > a, int key ){ return a.get(key).getLast(); } public static void removeLast (HashMap<Integer,Deque<Integer> > a, int key){ if(a.containsKey(key)){ a.get(key).removeLast(); if(a.get(key).size() == 0) a.remove(key); } } public static void add(HashMap<Integer,Deque<Integer>> a, int key, int val){ if(a.containsKey(key)){ a.get(key).addLast(val); }else{ Deque<Integer> b = new LinkedList<>(); b.addLast(val); a.put(key,b); } } public static void main(String[] args) { MyScanner sc = new MyScanner(); int t = sc.nextInt(); while (t-- != 0){ 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,Deque<Integer>> needed = new HashMap<>(); HashMap <Integer , Deque<Integer>> all = new HashMap<>(); int dummy =0; for(int i =0;i<n;i++) a[i] = sc.nextInt(); for(int i =0;i<n;i++){ b[i] = sc.nextInt(); //all.addOne(b[i]); add(all,b[i],i); if(b[i] != a[i]){ //needed.addOne(b[i]); add(needed,b[i],i); } } for(int i =0;i<m;i++) c[i] = sc.nextInt(); int idx = 0; int ans[] = new int[m]; while (needed.size()!= 0 && idx < m){ int x = c[idx]; if(needed.containsKey(x)){ //needed.removeOne(x); ans[idx] = get(needed,x); removeLast(needed,x); }else{ int y = needed.keySet().iterator().next(); ans[idx]= get(needed,y); } idx++; } if(idx == m && needed.size() ==0){ System.out.println("YES"); for(int i =0;i<m;i++){ System.out.print((ans[i]+1) +" "); } System.out.println(); continue; } if(idx == m && needed.size()!=0 ){ System.out.println("NO"); continue; } boolean fail = false; while (idx != m){ int x = c[idx]; if(all.containsKey(x)){ fail = false; ans[idx] = get(all,x); }else { fail = true; for(int j = idx;j<m;j++){ if(all.containsKey(c[j])){ fail = false; for(int z = idx;z<=j;z++){ ans[z] = get(all, c[j]); } idx = j; break; } } if(fail) break; } idx ++; } if (fail) System.out.println("NO"); else{ 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 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
60e28565f37ac95eac30574d349a232e
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.*; public class Main { 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; } } static class HashMultiSet<T> implements Iterable<T> { private final HashMap<T,Integer> map; private int size; public HashMultiSet(){map=new HashMap<>(); size=0;} public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public Iterator<T> iterator() { return new Iterator<>() { private final Iterator<T> iter = map.keySet().iterator(); private int count = 0; private T curElement; public boolean hasNext(){return iter.hasNext()||count>0;} public T next() { if(count==0) { curElement=iter.next(); count=get(curElement); } count--; return curElement; } }; } } private static long abs(long x){ if(x < 0) x*=-1; return x; } public static void main(String[] args) { MyScanner sc = new MyScanner(); int t = sc.nextInt(); while (t-- != 0){ int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; int c[] = new int[m]; HashMultiSet <Integer> needed = new HashMultiSet<>(); HashMultiSet <Integer> all = new HashMultiSet<>(); HashMap<Integer, Deque<Integer> > neededIdx = new HashMap<>(); HashMap<Integer, Deque<Integer>> allIdx = new HashMap<>(); int dummy =0; for(int i =0;i<n;i++) a[i] = sc.nextInt(); for(int i =0;i<n;i++){ b[i] = sc.nextInt(); all.addOne(b[i]); if(allIdx.containsKey(b[i])) allIdx.get(b[i]).addLast(i); else { Deque<Integer> temp = new LinkedList<>(); temp.addLast(i); allIdx.put(b[i], temp); } if(b[i] != a[i]){ needed.addOne(b[i]); if(neededIdx.containsKey(b[i])) neededIdx.get(b[i]).addLast(i); else { Deque<Integer> temp = new LinkedList<>(); temp.addLast(i); neededIdx.put(b[i], temp); } } } for(int i =0;i<m;i++) c[i] = sc.nextInt(); int idx = 0; int ans[] = new int[m]; while (needed.size()!= 0 && idx < m){ int x = c[idx]; if(needed.contains(x)){ needed.removeOne(x); ans[idx] = neededIdx.get(x).getLast(); neededIdx.get(x).removeLast(); }else{ Iterator<Integer> z = needed.iterator(); int y = z.next(); ans[idx]= neededIdx.get(y).getLast(); } idx++; } if(idx == m && needed.size() ==0){ System.out.println("YES"); for(int i =0;i<m;i++){ System.out.print((ans[i]+1) +" "); } System.out.println(); continue; } if(idx == m && needed.size()!=0 ){ System.out.println("NO"); continue; } boolean fail = false; while (idx != m){ int x = c[idx]; if(all.contains(x)){ fail = false; ans[idx] = allIdx.get(x).getLast(); }else { fail = true; for(int j = idx;j<m;j++){ if(all.contains(c[j])){ fail = false; for(int z = idx;z<=j;z++){ ans[z] = allIdx.get(c[j]).getLast(); } idx = j; break; } } if(fail) break; } idx ++; } if (fail) System.out.println("NO"); else{ 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 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
025bca58a4462b8f2ecc49cac75e5451
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 scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int m = scanner.nextInt(); int[] initial = new int[n]; int[] desired = new int[n]; int[] painter = new int[m]; for (int i = 0; i < n; i++) { initial[i] = scanner.nextInt(); } for (int i = 0; i < n; i++) { desired[i] = scanner.nextInt(); } for (int i = 0; i < m; i++) { painter[i] = scanner.nextInt(); } solve(initial, desired, painter); } scanner.close(); } private static void solve(int[] initial, int[] desired, int[] painter) { int n = initial.length; int m = painter.length; //ensure that the last color is in desired int lastC = painter[m - 1]; boolean found = false; for (int i = 0; i < n && !found; i++) { if (desired[i] == lastC) { found = true; } } if (!found) { System.out.printf("NO\n"); return; } //map from a painters color to a list of indexes that he should paint that color Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); for (int i = 0; i < n; i++) { if (initial[i] != desired[i]) { List<Integer> tmp = map.getOrDefault(desired[i], new ArrayList<Integer>()); tmp.add(i); map.put(desired[i], tmp); } } //if there exists an index int dummyInd = -1; List<Integer> finalGroup = map.getOrDefault(lastC, new ArrayList<Integer>()); if (finalGroup.size() > 0) { dummyInd = finalGroup.remove(finalGroup.size() - 1); } else { for (int i = 0; i < n; i++) { if (desired[i] == lastC) { dummyInd = i; break; } } } List<Integer> ans = new ArrayList<Integer>(); for (int i = 0; i < m - 1; i++) { int color = painter[i]; List<Integer> toPaint = map.getOrDefault(color, new ArrayList<Integer>()); if (toPaint.size() == 0) { ans.add(dummyInd + 1); initial[dummyInd] = color; } else { int paintInd = toPaint.get(0); initial[paintInd] = color; ans.add(paintInd + 1); if (toPaint.size() > 1) { toPaint.remove(0); } } } ans.add(dummyInd + 1); initial[dummyInd] = painter[m - 1]; for (int i = 0; i < n; i++) { if (initial[i] != desired[i]) { System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 0; i < ans.size(); i++) { System.out.printf("%d ", ans.get(i)); } System.out.println(); } } class Tuple { int color; int ind; Tuple(int color, int ind) { this.color = color; this.ind = ind; } }
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
daba885cfa0951a7b366ab6c95620f59
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 { private static PrintWriter out; private static class FS { StringTokenizer st; BufferedReader br; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();} } return st.nextToken(); } public String nextLine() { String s = null; try {s = br.readLine();} catch (IOException e) {e.printStackTrace();} return s; } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} } public static void main(String[] args) { FS sc = new FS(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), m = sc.nextInt(), i = 0; int[] a = new int[n], b = new int[n], c = new int[m], ans = new int[m]; ArrayList<Integer>[] g = (ArrayList<Integer>[]) new ArrayList[n+1]; for (i = 0; i < n; ++i) a[i] = sc.nextInt(); for (i = 0; i < n; ++i) { b[i] = sc.nextInt(); if (a[i] != b[i]) { if (g[b[i]] == null) g[b[i]] = new ArrayList<Integer>(); g[b[i]].add(i); } } for (i = 0; i < m; ++i) c[i] = sc.nextInt(); int x = -1; if (g[c[m-1]] != null) { x = g[c[m-1]].remove(g[c[m-1]].size() - 1); } else for (i = 0; i < n; ++i) if (b[i] == c[m-1]) {x = i; break;} if (x == -1) {out.println("NO"); continue;} for (i = 0; i < m - 1; ++i) { if (g[c[i]] == null || g[c[i]].size() == 0) ans[i] = x; else ans[i] = g[c[i]].remove(g[c[i]].size()-1); } ans[m-1] = x; // out.println(Arrays.toString(ans)); // out.println(Arrays.toString(a)+"\n"+Arrays.toString(b)); for (i = 1; i <= n; ++i) { if (g[i] != null && g[i].size() > 0) {out.println("NO"); break;} } if (i == n + 1) { out.println("YES"); for (int v : ans) out.print((v+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 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
f19b6042ca3b5ede0758bfa694ab898c
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 static java.lang.Math.max; import static java.lang.Math.min; public class CP{ static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE; static FastScanner sc = new FastScanner(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args){ int t = sc.nextint(); while(t-->0) { solve(); } out.close(); } static void solve(){ int n = sc.nextint(), m = sc.nextint(); Stack<Integer>[] v = new Stack[n]; for(int i = 0; i < n; ++i) { v[i] = new Stack<Integer>(); } int[] orig = new int[n]; for(int i = 0; i < n; ++i) { orig[i] = sc.nextint()-1; } int[] fin = new int[n]; for(int i = 0; i < n; ++i) { fin[i] = sc.nextint()-1; if(fin[i] != orig[i]) { v[fin[i]].push(i); } } int[] colors = new int[m]; int[] ans = new int[m]; for(int i = 0; i < m; ++i) { colors[i] = sc.nextint()-1; } int last = -1; if(v[colors[m-1]].size() > 0) { last = v[colors[m-1]].pop(); } else { for(int i = 0; i < n; ++i) { if(fin[i] == colors[m-1]) { last = i; break; } } } if(last == -1) { out.println("NO"); return; } ans[m-1] = last; for(int i = 0; i < m-1; ++i) { if(v[colors[i]].size() > 0) { ans[i] = v[colors[i]].pop(); } else { ans[i] = last; } } for(int i = 0; i < n; ++i) { if(v[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 void sort(int[] a) { Random r = new Random(); int n = a.length; for(int i = 0; i < n; ++i) { int idx = r.nextInt(n); int temp = a[idx]; a[idx] = a[i]; a[i] = temp; } Arrays.sort(a); } static BufferedReader br; static StringTokenizer st; static class FastScanner{ public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readint(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextint(); } return res; } long[] readlong(int n) { long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextlong(); } return res; } 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
a30085ae2d2ffb43f828dffca2c46917
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.HashMap; import java.util.Map; import java.util.StringTokenizer; public class contest { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); for (int t = 0; t < T; t++) { StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); Map<Integer, ArrayDeque<Integer>> mp = new HashMap(); int[] A = new int[N]; st = new StringTokenizer(br.readLine()); for (int j = 0; j < N; j++) { A[j] = Integer.parseInt(st.nextToken()); } int[] B = new int[N]; st = new StringTokenizer(br.readLine()); for (int j = 0; j < N; j++) { B[j] = Integer.parseInt(st.nextToken()); if (B[j] != A[j]) { if (mp.get(B[j]) == null) { mp.put(B[j], new ArrayDeque<>()); } mp.get(B[j]).add(j); } } int[] C = new int[M]; st = new StringTokenizer(br.readLine()); for (int j = 0; j < M; j++) { C[j] = Integer.parseInt(st.nextToken()); } int[] ret = new int[M]; int idx = -1; for (int i = 0; i < N; i++) { if (B[i] == C[M - 1]) { idx = i; break; } } if(mp.containsKey(C[M-1])){ idx = mp.get(C[M-1]).poll(); if(mp.get(C[M-1]).isEmpty()) mp.remove(C[M-1]); } ret[M-1] = idx; if (idx == -1) { pw.println("NO"); continue; } for (int i = M - 2; i >= 0; i--) { if (mp.containsKey(C[i])) { ret[i] = mp.get(C[i]).poll(); if (mp.get(C[i]).isEmpty()) { mp.remove(C[i]); } } else { ret[i] = idx; } } if (!mp.isEmpty()) { pw.println("NO"); } else { pw.println("YES"); for (int i : ret) { pw.print((i + 1) + " "); } pw.println(); } } pw.close(); br.close(); } } /* 1 8 10 5 4 3 4 3 2 4 9 */
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
2591e92a771bbe958cec460c0a29979a
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.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int qq = cin.nextInt(); // int qq = 1; label:for(int rrr = 0 ;rrr<qq;rrr++){ int n = cin.nextInt(); int m = cin.nextInt(); int []a = new int [n]; int []b = new int [n]; int []c = new int [m]; int index = -1;//b中不存在的放的位置 TreeSet<Integer>todo[] = new TreeSet[n]; Arrays.setAll(todo,e->new TreeSet<Integer>()); ArrayList<Integer>ans = new ArrayList<>(); Map<Integer,Integer>pos = new HashMap<>(); for(int i = 0 ; i < n ; i++){ a[i] = cin.nextInt() - 1; } for(int i = 0 ; i < n ; i++){ b[i] = cin.nextInt() - 1; if(a[i] != b[i]){ todo[b[i]].add(i);//需要进行涂色的 } pos.put(b[i],i); } for(int i = 0 ; i < m ; i++){ c[i] = cin.nextInt() - 1; } if(!pos.containsKey(c[m - 1])){//不包含c[m-1] out.println("NO"); continue label; } if(todo[c[m - 1]].size()!=0){ index = todo[c[m - 1]].last(); }else{ index = pos.get(c[m - 1]); } for(int i = 0 ; i < m ; i++) { if (!pos.containsKey(c[i])) {//不被b包含 ans.add(index); } else { Integer todoIndex = todo[c[i]].pollFirst(); if (todoIndex == null) {//已经被填充完了 ans.add(pos.get(c[i])); } else {//未被填充完 ans.add(todoIndex); } } } for(int i = 0 ; i < n; i++){ if(todo[i].size()!=0){ out.println("NO"); continue label; } } out.println("YES"); for(int i = 0 ; i < ans.size() -1; i++){ out.printf("%d ",ans.get(i) + 1); } out.printf("%d ",index + 1); out.println(); } out.close(); } public static long lcm(long a,long b ){ long ans = a / gcd(a,b) * b ; return ans; } public static ArrayList<Integer> dfs(int now ,int []arr,boolean []visited,ArrayList<Integer>temp){ if(visited[now]){ return temp; } visited[now] = true; temp.add(now); return dfs(arr[now],arr,visited,temp); } public static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } static class Node{ int color; public int getColor() { return color; } public void setColor(int color) { this.color = color; } public Node(int color) { this.color = color; } } static class SparseTable { public int[] log; public int[][] table; public int N; public int K; public SparseTable(int N) { this.N = N; log = new int[N+2]; K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N)); table = new int[K+1][N]; init(); } private void init() { log[1] = 0; for(int i = 2; i <= N+1; i++) log[i] = log[i/2]+1; } public void lift(int[] arr) { int n = arr.length; for(int i = 0; i < n; i++) table[0][i] = arr[i]; for(int i = 1; i <= K; i++) for(int j = 0; j + (1 << i) <= n; j++) table[i][j] = Math.max(table[i-1][j], table[i-1][j+(1 << (i - 1))]); } public int query(int L, int R)//[L,R]~[1,n] { //inclusive, 1 indexed if (L > R) { L = L^R; R = L^R; L = L^R; // 交换 a 和 b 的值 } L--; R--; int s = log[R-L+1]; return Math.max(table[s][L], table[s][R-(1 << s)+1]); } } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.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 17
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
9085539096723eaf0d5873a57e5ee77e
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 javax.print.DocFlavor; import java.math.BigInteger; import java.util.*; import java.io.*; public class Vaibhav { static long bit[]; static boolean prime[]; public static long lcm(long x, long y) { return (x * y) / gcd(x, y); } static class Pair {//implements Comparable<Pair> { int peldo; int bachalo; Pair(int peldo, int bachalo) { this.peldo = peldo; this.bachalo = bachalo; } /* //public int compareTo(Pair o){ return this.cqm-o.cqm; }*/ } //BIT public static void update(long bit[], int i, int x) { for (; i < bit.length; i += (i & (-i))) { bit[i] += x; } } public static long sum(int i) { long sum = 0; for (; i > 0; i -= (i & (-i))) { sum += bit[i]; } return sum; } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } 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 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()); } 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[] readlongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static long ans; static long mod = 1_000_000_007; static ArrayList<Integer> al[]; static long dp[][][]; static int n; static int k; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { n = fs.nextInt(); int m = fs.nextInt(); int a[] = fs.readArray(n); int b[] = fs.readArray(n); ArrayList<Integer> al [] = new ArrayList[n+1]; ArrayList<Integer> need [] = new ArrayList[n+1]; for(int i=0;i<=n;i++) {al[i] = new ArrayList<>();need[i] = new ArrayList<>();} for(int i=0;i<n;i++){ if(a[i]!=b[i]){ // al[b[i]].add(i); need[b[i]].add(i); }else{ al[b[i]].add(i); } } int c[] = fs.readArray(m); int ans[] = new int[m]; int prev = -1; boolean f = true; for(int i=m-1;i>=0;i--){ if(need[c[i]].size()>0){ ans[i] = need[c[i]].get(0)+1; prev = ans[i]; need[c[i]].remove(0); }else{ if(al[c[i]].size()>0){ ans[i] = al[c[i]].get(0)+1; prev = ans[i]; }else{ if(prev==-1){ f = false; break; }else{ ans[i] = prev; } } } } if(f){ 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]){ f = false; break; } } if(!f) out.println("NO"); else { out.println("YES"); for(int v:ans) out.print(v+" "); 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 17
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
962fc0ad84ea006bcd1f71cde2a82eef
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for(int i=0;i<t;i++){ solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in,PrintWriter out) { long n = in.nextLong(); long k = in.nextLong(); int[] h = new int[(int)n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); } int ans = -1; //****TLE wala Solution for (int i = 0; i < k; i++) { int flag = 0; for (int j = 0; j < n; j++) { if(j<n-1) { if (h[j] < h[j + 1]) { h[j]++; flag = 1; ans = j + 1; break; } } } if(flag == 0){ ans = -1; break; } } out.println(ans); } } 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());} char nextChar(){return next().charAt(0);} boolean nextBoolean(){return !(nextInt()==0);} // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e){e.printStackTrace();} return str; } } private static int[] mergeSort(int[] array){ if(array.length <= 1){ return array; } int midpoint = array.length / 2; int[] left = new int[midpoint]; int[] right; if(array.length % 2 == 0){ right = new int[midpoint]; }else{ right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } int[] result = new int[array.length]; left = mergeSort(left); right = mergeSort(right); result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right){ int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while(leftPointer < left.length || rightPointer < right.length){ if(leftPointer < left.length && rightPointer < right.length){ if(left[leftPointer] < right[rightPointer]){ result[resultPointer++] = left[leftPointer++]; }else{ result[resultPointer++] = right[rightPointer++]; } }else if(leftPointer < left.length){ result[resultPointer++] = left[leftPointer++]; }else{ result[resultPointer++] = right[rightPointer++]; } } return result; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
433882cdf206de8777f97dd406b5d2e4
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/** * Created by Himanshu **/ import java.util.*; import java.io.*; import java.math.*; public class B1481 { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int t = s.i(); while (t-- > 0) { int n = s.i() , k = s.i(); int [] arr = s.arr(n); if(k > 10000) { out.println(-1); continue; } int ans = n; for (int i=0;i<k;i++) { int j; for (j=0;j<n-1;j++) { if (arr[j] < arr[j+1]) { arr[j]++; break; } } ans = j+1; } if (ans == n) out.println(-1); else out.println(ans); } out.flush(); } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar, 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 s() { 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 long l() { 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 i() { 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 double d() throws IOException { return Double.parseDouble(s()); } 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; } public int[] arr(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = i(); } return ret; } public long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } // static class pairLong implements Comparator<pairLong> { // long first, second; // // pairLong() { // } // // pairLong(long first, long second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pairLong p1, pairLong p2) { // if (p1.first == p2.first) { // if(p1.second > p2.second) return 1; // else return -1; // } // if(p1.first > p2.first) return 1; // else return -1; // } // } // static class pair implements Comparator<pair> { // int first, second; // // pair() { // } // // pair(int first, int second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pair p1, pair p2) { // if (p1.first == p2.first) return p1.second - p2.second; // return p1.first - p2.first; // } // } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a20d8f16e705e5c1b3e0a1e6402b4d9c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Main { private static void solve(int[] arr, int n, int k) { int cnt = 0; while (!isDesc(arr)) { for (int i = 0; i < n - 1; i++) { if (arr[i] < arr[i + 1]) { arr[i]++; cnt++; if (cnt == k) { System.out.println(i + 1); return; } else { break; } } } } System.out.println(-1); } private static boolean isDesc(int[] arr) { for (int i = 1; i < arr.length; i++) { if (arr[i] > arr[i - 1]) { return false; } } return true; } public static void main(String[] args) { int tnum = scanner.nextInt(); for (int t = 0; t < tnum; t++) { int n = scanner.nextInt(); int k = scanner.nextInt(); int[] arr = readArray(n); solve(arr, n, k); } } private static int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } return arr; } private static final Scanner scanner = new Scanner(System.in); }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a88eb19776e7133953b28419c666043e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Main { private static void solve(int[] nums, int n, int k) { int cnt = 0; while (!isDesc(nums)) { for (int i = 0; i < n - 1; i++) { if (nums[i] < nums[i + 1]) { nums[i]++; cnt++; if (cnt >= k) { System.out.println(i + 1); return; } else { break; } } } } System.out.println(-1); } private static boolean isDesc(int[] nums) { for (int i = 1; i < nums.length; i++) { if (nums[i] > nums[i - 1]) { return false; } } return true; } public static void main(String[] args) { int tnum = scanner.nextInt(); for (int t = 0; t < tnum; t++) { int n = scanner.nextInt(); int k = scanner.nextInt(); int[] nums = readArray(n); solve(nums, n, k); } } private static int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } return arr; } private static final Scanner scanner = new Scanner(System.in); }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b8235c1c1a97e6742ee8e2a1e37acf40
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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.LinkedList; import java.util.Map.Entry; import java.util.Queue; import java.util.StringTokenizer; public class COVID { static long mod = 998244353 ; static PrintWriter pw; static ArrayList<Integer>[] adjList; static int INF=(int)1e8; static double EPS=1e-9; 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();int k=sc.nextInt(); int idx=0; int[] arr=sc.nextIntArr(n); if(k>10000) { pw.println("-1"); continue test; }else { while(k>0) { // System.out.println("here"); idx=Math.max(idx-1, 0); while(idx<n-1) { if(arr[idx]<arr[idx+1]) { arr[idx]+=1; break; }else { idx++; if(idx==n-1) { pw.println("-1"); continue test; } } } k-=1; } pw.println(idx==n-1?"-1":(idx+1)); } } pw.flush(); } public static long gcd(long a, long b) { if(b==0)return a; return gcd(b,a%b); } static long pow(long p, long e) { long ans = 1; while (e > 0) { if ((e & 1) == 1) ans = ((ans * 1l * p)); e >>= 1; { } p = ((p * 1l * p)); } 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
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
f7279428ff519cbec0d09d6da1e6219f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class Main { static final int M = 1000000007; static FastReader in = new FastReader(); // static PrintWriter out = new PrintWriter(System.out); // static Scanner in = new Scanner(System.in); // File file = new File("input.txt"); // Scanner in = new Scanner(file); // PrintWriter out = new PrintWriter(new FileWriter("output.txt")); // Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); // use as int[n][2]; // static boolean[] prime = new boolean[1100001]; public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(), k = in.nextInt(); int[] h = new int[n]; int max = Integer.MIN_VALUE; for(int i = 0; i<n; i++) { h[i] = in.nextInt(); max = Math.max(max, h[i]); } boolean flag = false; if(max*(n-1)<k) flag = false; else { int times = k; while(times-- > 0) { for(int i = 0; i<n-1; i++) { if(h[i]<h[i+1]) { k--; h[i]++; if(k==0) { System.out.println(i+1); flag = true; break; } break; } } if(flag) break; } } if(!flag) { System.out.println(-1); } } } static void sieve(int n, boolean[] prime) { for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } static int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } static class Pair{ int f; int s; public Pair(int x, int y){ this.f = x; this.s = y; } } 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
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
16161aa22cb004f05e4e5f4c1612e53a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class cf01 { static Scanner sc = new Scanner(System.in); private static void solve() { int n = sc.nextInt(); int k = sc.nextInt(); int h[]=new int[n]; for (int i=0; i<n; ++i) h[i]=sc.nextInt(); int i=0; boolean fl=false; while (k-->0) { fl=false; for (i=0; i<n-1; ++i) if (h[i]<h[i+1]) { ++h[i]; fl=true; break; } if (fl)continue; else break; } if (fl)System.out.println(++i); else System.out.println(-1); } public static void main (String args[]) { int t=sc.nextInt(); while (t-->0) solve(); sc.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
800824e2962f811497c5c0d58a4a354e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int T=sc.nextInt(); for (int i=1;i<=T;i++) {int ans=-1; int N=sc.nextInt(); int K=sc.nextInt(); int arr[]=new int[N]; for(int h=0;h<N;h++)arr[h]=sc.nextInt(); for(int p=1;p<=K;p++) {ans=-1; for(int j=1;j<N;j++) {if(arr[j-1]<arr[j]) {arr[j-1]++;ans=j;break;}} if(ans==-1)break; } System.out.println(ans); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9b1aeec90a9c02ffbba0c4dc6c066d79
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.CharArrayReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { 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 static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = s.nextInt(); int k = s.nextInt(); int h[] = new int[n]; for(int i=0;i<n;i++) { h[i] = s.nextInt(); } int pos = -1; int i; long sum = 0; while(sum<k) { for (i = 1; i < n; i++) { if (h[i] > h[i - 1]) { sum++; h[i - 1] = h[i - 1] + 1; pos = i - 1; break; } } if(i==n) break; } if(sum<k) System.out.println(-1); else System.out.println(pos+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9e2db5e09bc9cc84c1cea2cd3022c573
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.CharArrayReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { 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 static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); long k = s.nextLong(); long h[] = new long[n]; for(int i=0;i<n;i++) { h[i] = s.nextLong(); } long pos = -1; long sum = 0; while(sum<k) { int i = 1; for (i = 1; i < n; i++) { if (h[i] > h[i - 1]) { sum++; h[i - 1] = h[i - 1] + 1; pos = i - 1; break; } } if(i==n) break; } if(sum<k) System.out.println(-1); else System.out.println(pos+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
5148e419ce70b1212dcc0b57e114f16e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class B { public static void main(String[] args) throws FileNotFoundException { FastScanner fs = new FastScanner(); int T = fs.nextInt(); outer: for (int tt = 0; tt < T; tt++) { int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = fs.readArray(n); if (n == 1) { System.out.println(-1); continue outer; } while (true) { for (int i = 0; i < n - 1; i++) { if (i == n - 2 && arr[i] >= arr[i + 1]) { System.out.println(-1); continue outer; } if (arr[i + 1] > arr[i]) { k--; arr[i]++; if (k == 0) { System.out.println(i + 1); continue outer; } break; } } } } } 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()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
81a4342d1cdc76dfadf14a94b98b187b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; import java.util.function.IntFunction; import java.util.stream.Collectors; public class codeforces { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static int cnt; //-----------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; } } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long fact(long n) { long fact=1; long i=1; while(i<=n) { fact=fact*i; i++; } return fact; } public class ArrayListComparator<T extends Comparable<T>> implements Comparator<ArrayList<T>> { @Override public int compare(ArrayList<T> o1, ArrayList<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } public static int reverseBits(int n) { int rev = 0; // traversing bits of 'n' // from the right while (n > 0) { // bitwise left shift // 'rev' by 1 rev <<= 1; // if current bit is '1' if ((int)(n & 1) == 1) rev ^= 1; // bitwise right shift //'n' by 1 n >>= 1; } // required number return rev; } public static void main(String[] args) { // TODO Auto-generated method stub MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); StringBuilder sb = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int ans=-1; boolean p=true; while(k>0) { p=true; for(int i=0;i<n-1;i++) { if(arr[i+1]>arr[i]) { k--; arr[i]++; ans=i+1; p=false; break; } } if(p==true) { ans=-1; break; } } out.println(ans); } out.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
8aea13f4762566e7c18a98ca96fcb6e0
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package codeforces; //package codeforces; import java.util.*; public class codeforces { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(),k=s.nextInt(); long ans=0,ans2=0; int a[]=new int[n]; int max=0; for(int i=0;i<n;i++) { a[i]=s.nextInt(); max=Math.max(max, a[i]); } for(int i=0;i<n;i++) { ans2+=Math.abs(a[i]-max); } if(k>ans2) { System.out.println(-1); continue; } boolean c=false; while(k!=0) { for(int i=0;i<n-1;i++) { if(a[i]>=a[i+1]) { if(i==n-2) { ans=-1; break; } }else { a[i]++; ans=i+1; break; } } if(!c) k--; } System.out.println(ans); } s.close(); } 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); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b64d626d8f95711a67af2357cf7fa01f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package codeforces; import java.util.*; public class codeforces { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(),k=s.nextInt(); long ans=0; int a[]=new int[n]; //int max=0; for(int i=0;i<n;i++) { a[i]=s.nextInt(); //max=Math.max(a[i],max); } int j=0; for(int i=0;i<k;i++) { for(j=1;j<n;j++) { if(a[j]>a[j-1]) { a[j-1]++; break; } } if(j==n) { break; } } if(j==n) { ans=-1; }else { ans=j; } System.out.println(ans); } s.close(); } 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); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
dbe3946292eb24bbd5d99b90831c9531
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class javaCode { private 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; } } private static FastReader sc = new FastReader(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0){ solve(); } } private static void solve() { int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[n]; a[0] = sc.nextInt(); int mx = 0; for(int i=1; i<n; i++){ a[i] = sc.nextInt(); mx = Math.max(mx, a[i]); } if(n * mx < k){ System.out.println(-1); return; } int ans = n+1; for(int b=0;b<k;b++){ int to = -2; for(int i=0;i<n-1;i++){ if(a[i] < a[i+1]){ to = i; break; } } ans = to + 1; if(to == -2)break; a[to]++; } System.out.println(ans); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b2558a7655ac7de244a55a073c7bdf17
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class Main { static long mod=(long)1e9+7; static long mod1=998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t= in.nextInt(); while(t-->0) { int n=in.nextInt(); int k=in.nextInt(); int[] arr=in.readArray(n); int start=0; int lastIndex=start; while(start!=n-1 && k>0){ if(arr[start]<arr[start+1]) { arr[start]++; k--; lastIndex=start; start=0; } else { if (arr[start] >= arr[start + 1]) start++; } } if(start==n-1 && k!=0) out.println(-1); else out.println(lastIndex+1); } out.close(); } 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), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } public static void debug(Object... o){ System.err.println(Arrays.deepToString(o)); } static String printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } 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()); } public int[] readArray(int n) { int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3a2ff57768da0a1e3617ecfa8aed3578
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; /* */ public class A { static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),k=sc.nextInt(); int a[]=sc.readArray(n); int pos=-1; while(k>0) { boolean po=false; for(int i=0;i+1<n;i++) { if(a[i]<a[i+1]) { a[i]++; k--; pos=i+1; po=true; break; } } if(!po)break; } System.out.println(k>0?-1:pos); } } static int[] reverse(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al,Collections.reverseOrder()); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static void ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); } static void print(long a[]) { for(long e:a) { System.out.print(e+" "); } System.out.println(); } static void print(char a[]) { for(char e:a) { System.out.print(e+" "); } System.out.println(); } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static void print(double a[]) { for(double e:a) { System.out.print(e+" "); } System.out.println(); } 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[] readArray(int n) { int a[]=new int [n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } return a; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d0dd072737b6037daf814a511e4bd884
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class C_NewColony { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int itr=0;itr<t; itr++) { int[] line = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int n = line[0]; int k = line[1]; int[] heights = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); addBoulder(heights,k-1); System.out.println(fallIndex(heights)); } //testing //int[] test = {2,4,5,1,6}; addBoulder(test,122); //System.out.println(Arrays.toString(test)); } public static void addBoulder(int[] heights, int k) { boolean flat = false; while (k>0 && flat==false) { for (int i=0; i<heights.length; i++) { if (i==heights.length-1) { flat = true; break; } if (heights[i]<heights[i+1]) { heights[i]+=1; k--; break; } } } //return heights; } public static boolean fall(int[] heights) { for (int i=0; i<heights.length-1; i++) { if (heights[i]<heights[i+1]) return false; } return true; } public static int fallIndex(int[] heights) { for (int i=0; i<heights.length-1; i++) { if (heights[i]<heights[i+1]) return i+1; } return -1; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
dcf2b8fbd9dffd9ab6bc267a2e02524a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class boulders { public static void main(String []er) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int s,k; while(n-->0) { s=sc.nextInt(); k=sc.nextInt(); int a[]=new int[s]; int r=0,j=1,p=0; for(int i=0;i<s;i++) a[i]=sc.nextInt(); while(j<s) { if(a[j]>a[j-1]) { a[j-1]++; r++; if(r==k) p=j; j=1; } else j++; } if(k>r) System.out.println(-1); else System.out.println(p); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
7865750aae920c708abe383700454771
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class a20j { static PrintWriter out = new PrintWriter((System.out)); static long[] a; public static void main(String args[]) throws IOException { Reader sc = new Reader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long k = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int ans = -1; while (k > 0) { boolean ok = false; for (int tt = 0; tt < n - 1; tt++) { if (arr[tt] < arr[tt + 1]) { ok = true; k--; if (k == 0) { ans = tt + 1; break; } arr[tt]++; break; } } if (!ok || ans != -1) { break; } } if (ans == arr.length) { ans = -1; } out.println(ans); } out.close(); } static boolean check(char[] s, int l, int r) { int[] freq = new int[26]; int len = r - l + 1; int left = len / 2 - 1; int right = (len % 2 == 0) ? (len / 2) : (len / 2 + 1); for (int i = l; i <= left; i++) { freq[s[i] - 'a']++; } for (int i = right; i <= r; i++) { freq[s[i] - 'a']--; } for (int c : freq) { if (c > 0) return false; } return true; } static boolean[] sieve(int n) { ArrayList<Integer> primes = new ArrayList<>(); boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } for (int i = 2; i < n; i++) { if (isPrime[i]) primes.add(i); } return isPrime; } static boolean possible(int i, int j) { return i >= 0 && j >= 0 && i < 3 && j < 3; } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow(a * a, b / 2); } else { return a * pow(a * a, b / 2); } } static class Reader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { 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() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null) { return false; } st = new StringTokenizer(next); return true; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
25fd05d3cef375a82e9be744d2fdac6a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Template { //static int cnt=0; static int max=0; static Scanner sc=new Scanner(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static void main (String[] args) throws java.lang.Exception{ int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); ArrayList<Integer> arr=arrInpInt(n); int pos=-1; for(int j=0;j<=100;j++) { for(int i=0;i<n-1;i++) { if(arr.get(i)<arr.get(i+1)) { if(i>=1) { k=k-(Math.min(arr.get(i-1)+1, arr.get(i+1))-arr.get(i)); arr.set(i, Math.min(arr.get(i-1)+1,arr.get(i+1))); } else { k=k-(arr.get(i+1)-arr.get(i)); arr.set(i, arr.get(i+1)); } //red=true; //System.out.println(k); if(k<=0) { pos=i; break; } i=-1; } if(k<=0) { break; } } if(k<=0) { break; } } if(pos==-1) { System.out.println(-1); } else { System.out.println(pos+1); } }} public static int redi(ArrayList<Integer> arr,int k,int n) { boolean red=false; int pos=-1; System.out.println(arr+"\n"+k); for(int i=0;i<n-1;i++) { if(arr.get(i)<arr.get(i+1)) { k=k-(arr.get(i+1)-arr.get(i)); arr.set(i, arr.get(i+1)); red=true; } //System.out.println(k); if(k<=0) { pos=i; break; } } if(red && pos==-1) { pos=redi(arr,k,n); } return pos; } //int n=sc.nextInt(); /* long n=sc.nextLong(); long req=0; long k=sc.nextLong(); if(n%2!=0) { n=n-1; req++; } req=check(n,req); System.out.println(req); if(req<=k) { System.out.println("YES"); } } public static long check(long n,long req) { long pow=(long) (Math.log(n)/Math.log(2)); if(pow<=1) System.out.println("POW "+pow); n=(long) (n-Math.pow(2,pow)); req++; if(n==0) { return req; } else { req=check(n,req); } return req; } */ public static long fact(long i) { long fact=1; for(int j=2;j<=i;j++) { fact=fact*j; } return fact; } public static void printArrInt(ArrayList<Integer> arr) { for(int i=0;i<arr.size();i++) { System.out.print(arr.get(i)+" "); } } public static int[][] matrixInp(int n,int m) { int[][] arr=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { arr[i][j]=sc.nextInt(); } } return arr; } public static int bSearch(ArrayList<Long> arr,long n) { int start=0; int end=arr.size()-1; int mid=0; while(end>=start) { mid=start+(end-start)/2; if(arr.get(mid)==n) { break; } else if(arr.get(mid)>n) { end=mid-1; } else if(arr.get(mid)<n) { start=mid+1; } } return mid; } public static int reduceFraction(int a, int b) { int aa=a; if(a==0 || b==0) return Math.max(a, b); else { for(int i=2;i<=Math.min(a, b);i++) { if(a%i==0 && b%i==0) { a=a/i; b=b/i; i--; } } } return aa/a; } public static long sumDigits(long x) { String s=String.valueOf(x); //System.out.println(s+" "+sx+" "+x); long sum=0; for(int i=0;i<s.length();i++) { sum+=Long.parseLong(s.substring(i,i+1)); } return sum; } public static int[] primeFactofNumb(int num) { int[] factors=new int[num+1]; for(int j=2;j<=Math.sqrt(num);j++) { if(num%j==0) { factors[j]++; } while(num%j==0) { num=num/j; } } if(num>1) factors[num]++; return factors; } public static long nCr(int n, int k){ long C[][] = new long[n + 1][k + 1]; long i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= (long)Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[(int) i][(int) j] = 1; // Calculate value using // previously stored values else C[(int) i][(int) j] = C[(int) (i - 1)][(int) (j - 1)] + C[(int) (i - 1)][(int) j]; } } return C[n][k]; } static long gcd(long a,long b) { if(b==0) { return a; } else if(a==0) return b; return gcd(b,a%b); } public static int s2i(String s) { return Integer.parseInt(s); } public static int LeastIntFn(int a,int b) { if((b%a)==0) { return (int)b/a; } return (int)(b/a)+1; } public static ArrayList<Long> arrInpLong(long n){ ArrayList<Long> arr=new ArrayList(); for(long i=0;i<n;i++) { arr.add(sc.nextLong()); } return arr; } public static ArrayList<String> arrInpStr(int n){ ArrayList<String> arr=new ArrayList(); for(int i=0;i<n;i++) { arr.add(sc.nextLine()); } return arr; } public static ArrayList<Integer> arrInpInt(int n){ ArrayList<Integer> arr=new ArrayList(); for(int i=0;i<n;i++) { int k=sc.nextInt(); if(k>max) max=k; arr.add(k); } return arr; } public static int min(int a,int b,int c) { int min1=Math.min(a, b); return Math.min(min1,c); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
f93e72447b4d58e9c3fb9ffc0bb5b277
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Day2_Test_NewColony { public static void main(String[] args) { InputReader reader = new InputReader(System.in); int numberOfTestCase = reader.nextInt(); StringBuilder outBuffer = new StringBuilder(); while (numberOfTestCase --> 0) { int n = reader.nextInt(); int k = reader.nextInt(); int[] mountainHeight = new int[n]; for (int i = 0; i < n; i++) { mountainHeight[i] = reader.nextInt(); } int lastPosition = 0; boolean fall = false; while (k > 0 && !fall) { for (int i = 0; i < n; i++) { if (i == n - 1) { lastPosition = -1; fall = true; break; } if (mountainHeight[i] < mountainHeight[i + 1]) { mountainHeight[i]++; k--; lastPosition = i + 1; break; } } } outBuffer.append(lastPosition + " "); } System.out.print(outBuffer); } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6de019f3fd8ce21b6647600317509032
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class Main { 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; } } static FastReader in = new FastReader(); public static void main (String args[]) { long t = in.nextLong(); for (long i = 0; i < t; i++) { solve(); } } static void solve() { int n = in.nextInt(); int k = in.nextInt(); int[] h = new int[n]; boolean f = false; for (int i = 0; i < n; i++) h[i] = in.nextInt(); for (int i = 0; i < k; i++) { f = false; for (int j = 0; j < n - 1; j++) { if (h[j] >= h[j+1]) continue; h[j]++; f = true; if (i == k - 1) System.out.println(j+1); break; } if (f == false) { System.out.println(-1); return; } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
63c408b74a33eec0eb6210142bfe715c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /** * @author Vishal Singh * 05-02-2021 */ public class B { static final boolean multipleTestCase = true; public static void main(String[] args) throws Exception { try { FastReader fr = new FastReader(); int t = multipleTestCase ? fr.nextInt() : 1; while (t-- > 0) { int n = fr.nextInt(); int k = fr.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = fr.nextInt(); } int index = 0; int i = 0; while (k-- > 0) { int curr = h[i]; while (i+1 < n && curr >= h[i+1]){ if (curr < h[i+1]){ break; } i++; curr = h[i]; } if (i+2>n){ index = -1; break; }else { index = i; h[i]++; } i = 0; } index++; if (index == 0){ System.out.println("-1"); }else { System.out.println(index); } } } catch (Exception e) { } finally { } } static int min(int... values) { int res = Integer.MAX_VALUE; for (int x : values) { res = Math.min(x, res); } return res; } static int max(int... values) { int res = Integer.MIN_VALUE; for (int x : values) { res = Math.max(x, res); } return res; } 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
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c04bb2e81d999d901c1ddd78c752ff62
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; //import Disjoint_sets_and_Unions.pair; public class hacker190 implements Runnable{ public static int[] visited1=new int[100001]; public static int[] visited2=new int[100001]; public static int[] visited3=new int[100001]; public static HashSet<Integer> points=new HashSet<>(); public static int[] level=new int[1000001]; public static int[] gf=new int[20000]; public static int[] col=new int[20000]; public static int[] a=new int[1000001]; public static int[] in=new int[1000001]; public static int[] low=new int[1000001]; public static Stack stack =new Stack<>(); public static int[] intstack=new int[100001]; public static ArrayList<Integer>[] forw=new ArrayList[1000001]; public static ArrayList<Integer>[] back=new ArrayList[1000001]; public static ArrayList<Integer>[] trav=new ArrayList[1000001]; public static ArrayList<Integer>[] b=new ArrayList[100001]; static int min_dist=Integer.MAX_VALUE; static int min_gf=Integer.MAX_VALUE; static int timer=1; static long count_1=0; static int end=-1; static int leng=0; static boolean p=true; //public static class pair{ // private int x; // private int y; // pair(int x,int y){ // this.x=x; // this.y=y; // } //} public static ArrayList<Integer> e11=new ArrayList<>(); 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 static boolean isPrime(int n) { for(int i=2;i*i<=n;i++) { if(n%i==0) { return false; } } return true; } public static boolean diff(int a,int b) { int cnt=0; while(a>0) { if(a%10!=b%10) { cnt++; } a/=10; b/=10; } if(cnt==1) { return true; }else { return false; } } public static void main(String[] args) throws Exception { new Thread(null, new hacker190(), "Main", 1<<26).start(); } public static class pair2{ private int x; private int y; private int d; pair2(int x,int y,int d){ this.x=x; this.y=y; this.d=d; } } public void run() { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); // int t=s.nextInt(); // while(t>0) { // int n=s.nextInt(); // int[] a=new int[n+1]; // for(int i=1;i<=n;i++) { // a[i]=s.nextInt(); // dist1[i]=Integer.MAX_VALUE; // } // // q.add(new pair(1,0)); // dist1[1]=0; // int cost=Integer.MAX_VALUE; // while(!q.isEmpty()) { // pair v=q.poll(); // if(v.i==n) { // cost=Math.min(cost, v.c); // dist1[n]=cost; // continue; // } // if(dist1[a[v.i]]>dist1[v.i]) { // dist1[a[v.i]]=dist1[v.i]; // q.addFirst(new pair(a[v.i],v.c)); // } // if((v.i+1<=n) && (dist1[v.i+1]>dist1[v.i]+1)) { // dist1[v.i+1]=dist1[v.i]+1; // q.addLast(new pair(v.i+1,v.c+1)); // } // if((v.i-1>=1) && (dist1[v.i-1]>dist1[v.i]+1)) { // dist1[v.i-1]=dist1[v.i]+1; // q.addLast(new pair(v.i-1,v.c+1)); // } // } // out.println(cost); // t--; // } //int n=s.nextInt(); //int m=s.nextInt(); //char[] a=new char[n+1]; //for(int i=1;i<=n;i++) { // a[i]=s.next().charAt(0); //} //for(int i=1;i<=n-1;i++) { // int x=s.nextInt(); // int y=s.nextInt(); // b[x].add(y); // b[y].add(x); // } //out.close(); //int q=s.nextInt(); //while(q>0) { // int v=s.nextInt(); // String str=s.next(); // q--; //} int t=s.nextInt(); while(t>0) { int n=s.nextInt(); long k=s.nextLong(); int[]a=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=s.nextInt(); } // int curr=1; // int currmax=-1; // int no=1; int pos=-1; int curr=1; for(int i=1;i<=k;i++) { curr=1; while(curr<n && a[curr]>=a[curr+1]) { curr++; } if(curr==n) { pos=1; break; }else { a[curr]++; } } if(pos==1) { out.println(-1); }else { out.println(curr); } t--; } out.close(); } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { // int count=0; long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } static int[] gi=new int[100001]; static int[] to=new int[100001]; static HashSet<Integer> e1=new HashSet<>(); static HashSet<Integer> e2=new HashSet<>(); public static void dfs1(int x,int y,int[][] a,int n,int m) { visited_grid[x][y]=1; count++; for(int i=0;i<4;i++) { if(isValid(x+dx[i],y+dy[i],n,m) &&(a[x+dx[i]][y+dy[i]]==1)) { dfs1(x+dx[i],y+dy[i],a,n,m); } }} public static void dfs(int node) { visited1[node]=1; for(int i=0;i<b[node].size();i++) { if(visited1[(int)b[node].get(i)]==0) { dfs((int)b[node].get(i)); } } } public static boolean isValid(int x,int y,int n,int m) { if( x<0 || x>n-1 || y>m-1 || y<0 ) { return false; }else if(visited_grid[x][y]==1) { return false; } return true; } public static int[] dx= {0,0,-1,1}; public static int[] dy= {1,-1,0,0}; public static int[][] visited_grid=new int[1001][1001]; public static int[][] dist=new int[1001][1001]; public static int[] dist1=new int[100001]; static int count=0; public static class pair{ private int i; private int c; pair(int i,int c) { this.i=i; this.c=c; } } //public static void BFS(int x,int y,int n,int m,int[][] a) { // Queue<pair> q=new LinkedList<>(); // q.add(new pair(x,y)); // visited_grid[x][y]=1; //count++; // while(!q.isEmpty()) { // // } //} public static Deque<pair> q=new LinkedList<>(); //public static void BFS_on_grid(int x,int y,int[][] a,int n,int m) { // visited_grid[x][y]=1; // Queue<pair> queue=new LinkedList<>(); // count++; // queue.add(new pair(x,y)); // while(!queue.isEmpty()) { // pair v=queue.poll(); // int x1=v.x; // int y1=v.y; // for(int i=0;i<4;i++) { // if(isValid(x1+dx[i],y1+dy[i],n,m)) { // if((a[x1+dx[i]][y1+dy[i]]==1) &&(visited_grid[x1+dx[i]][y1+dy[i]]==0)) { // queue.add(new pair(x1+dx[i],y1+dy[i])); // count++; // visited_grid[x1+dx[i]][y1+dy[i]]=1; // } // } // } // //} //}} }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
08d30630ed1fd6fbed2b454c6853d589
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.math.*; //import java.io.*; public class Experiment { static Scanner in=new Scanner(System.in); static int global=0; // static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); static class Pair implements Comparable<Pair>{ String s;char ch;int swap; Pair(String s,char ch,int swap){ this.s=s; this.ch=ch; this.swap=swap; } public int compareTo(Pair o){ if(swap%2==0) return o.ch-this.ch; else return this.ch-o.ch; // Sort return this.first-o.first; pair on the basis of first parameter in ascending order // return o.first-this.first to sort on basis of first parameter in descending order // return this.second -o.second to Sort pair on the basis of second parameter in ascending order //return o.second-this.second to sort on basis of second parameter in descending order } } public static void sort(int ar[],int l,int r) { if(l<r) { int m=l+(r-l)/2; sort(ar,l,m); sort(ar,m+1,r); merge(ar,l,m,r); } } public static int hcf(int x,int y) { if(y==0) return x; return hcf(y,x%y); } public static void merge(int ar[],int l,int m,int r) { int n1=m-l+1;int n2=r-m; int L[]=new int[n1]; int R[]=new int[n2]; for(int i=0;i<n1;i++) L[i]=ar[l+i]; for(int i=0;i<n2;i++) R[i]=ar[m+i+1]; int i=0;int j=0;int k=l; while(i<n1 && j<n2) { if(L[i]<=R[j]) { ar[k++]=L[i++]; }else { ar[k++]=R[j++]; } } while(i<n1) ar[k++]=L[i++]; while(j<n2) ar[k++]=R[j++]; } public static int[] sort(int ar[]) { sort(ar,0,ar.length-1); //Array.sort uses O(n^2) in its worst case ,so better use merge sort return ar; } public static void rec(int ar[],int n,long d,long tar) { } public static void func() { int n=in.nextInt();int k=in.nextInt();int inc=1;int dec=1; long ar[]=new long[n];//int temp=0; for(int i=0;i<n;i++) { ar[i]=in.nextInt(); } for(int i=0;i<n-1;i++) { if(ar[i]<ar[i+1]) dec=0; if(ar[i]>ar[i+1]) inc=0; } // if(inc==1 && dec==0) { // // // if(k>) // // // } if(inc==0 && dec==1) { System.out.println(-1);return; } int j=0; for(int i=1;i<=k-1;i++) { //int temp=1; for( j=0;j<n-1;j++) { if(ar[j]<ar[j+1] ) { ar[j]+=1;break;} // if(j==n-2) } if(j==n-1) { System.out.println(-1);return; } } for(int i=0;i<n-1;i++) { if(ar[i]<ar[i+1]) { System.out.println(i+1);return; } } System.out.println(-1); } public static void main(String[] args) { int t=in.nextInt(); while(t-->0) { func(); //global=0; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6e89a79cb61bc5ee305b27d304773bba
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class B522021 { static 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') break; 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(); } } public static void main(String args[]) throws IOException{ Reader in = new Reader(); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); int k = in.nextInt(); int a[] = new int[n]; for(int i =0; i<n; i++){ a[i] = in.nextInt(); } solve(a,k); } } static void solve(int a[] , int k ){ int n = a.length; int count = 0 ; while(true){ boolean flag = false; for(int i = 0; i<n-1; i++){ if(a[i]<a[i+1]){ a[i]++; count++; if(count==k){ System.out.println(i+1); return; } flag = true; break; } else if ( count==k){ System.out.println(i+1); return; } } if(flag == false){ System.out.println(-1); return; } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
019d9331c960d3dda1e33f2421723054
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class NewColony { public static void main(String[] args) { FastReader fs = new FastReader(); StringBuilder sb = new StringBuilder(); int t = fs.nextInt(); for(int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = fs.nextInt(); } boolean reachEnd = false; int curr = 0; while(k > 0) { for(int j = 0; j < n && k > 0; j++) { curr = j + 1; if(j == n - 1) { reachEnd = true; break; }else if(arr[j] < arr[j + 1]) { k--; arr[j]++; break; } } if(reachEnd) break; } if(reachEnd) sb.append("-1"); else sb.append(curr); sb.append("\n"); } System.out.print(sb); } public static long div(long a, long b) { return (a + b - 1) / b; } public 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()); } float nextFloat() { return Float.parseFloat(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
12d367a59c63af6d2040f3e5277ce4f5
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class cc_march2020_long { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t -- >0) { int n=scn.nextInt(); int k=scn.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=scn.nextInt(); } int sum=0; boolean flag=false; for(int i=0;i<n && !flag;i++) { int total=0; int j=i-1; TreeSet<Integer>set=new TreeSet(); for(;j>=0;j--) { if(arr[j]>=arr[i]) { break; } set.add(arr[j]); total+=(arr[i]-arr[j]); } if(sum+total>=k) { set.add(arr[i]); // System.out.println(set+" "+sum); for(int val:set) { int extra=0; int len=0; for(int p=i-1;p>j && arr[p]<val ;p--) { extra+=(val-arr[p]); len++; arr[p]=val; } if(sum+extra >= k) { k=k-sum; System.out.println(k%len==0 ? (i-len+1) :(i-(k%len)+1)); flag=true; break; }else { sum+=extra; } } break; }else { for(int J=i-1;J>j;J--) { arr[J]=arr[i]; } sum+=total; } } if(!flag) { System.out.println("-1"); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
2031bc2838b29d35ec9c78905983fe04
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.* ; public class QuarentinePractice { public static void main(String[] args) { Scanner sc = new Scanner(System.in) ; int t = sc.nextInt() ; while(t -- != 0) { int n = sc.nextInt() ; long k = sc.nextLong() ; int arr[] = new int[n] ; int max = Integer.MIN_VALUE ; int remain = 0 ; for(int i = 0 ; i< n ; i++) { arr[i] = sc.nextInt() ; if(arr[i] > max) max = arr[i] ; } for(int i = 0 ; i< n ; i++) { remain += (max-arr[i]) ; } if(k > remain) { System.out.println("-1"); } else { int j = 0 ; int store = 0 ; while(j < n && k != 0) { if(j<n-1 && k != 0) { if(arr[j] >= arr[j+1]) { j ++ ; } else if(arr[j] < arr[j+1]) { store = j+1 ; arr[j] += 1 ; j = 0 ; k -- ; } } else if(j == n-1) { store = -1 ; j = 0 ; k -- ; } } System.out.println(store); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
700934902db81d93a6d127575013c514
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.* ; public class QuarentinePractice { public static void main(String[] args) { Scanner sc = new Scanner(System.in) ; int t = sc.nextInt() ; while(t -- != 0) { int n = sc.nextInt() ; long k = sc.nextLong() ; int arr[] = new int[n] ; int max = Integer.MIN_VALUE ; int remain = 0 ; for(int i = 0 ; i< n ; i++) { arr[i] = sc.nextInt() ; if(arr[i] > max) max = arr[i] ; } for(int i = 0 ; i< n ; i++) { remain += (max-arr[i]) ; } if(k > remain) { System.out.println("-1"); } else { int ans = 0 ; for(int i = 0 ; i< k ; i++) { int fill = 0 ; for(int j = 0 ; j< n-1 ; j++) { if(arr[j] < arr[j+1]) { ans = j+1 ; arr[j] ++ ; fill = 1 ; break ; } } if(fill == 0) ans = -1 ; } System.out.println(ans); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
aed6cd3c6c4cdbef92a4969753b259da
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long k = sc.nextLong(); long[] h = new long[n]; for (int j = 0; j < n; j++) { h[j] = sc.nextInt(); } int current = 0; while (k > 0) { for (current = 0; current < n - 1; current++) { if (h[current] < h[current + 1]) { break; } } if (current == n - 1) { current = -1; break; } k--; h[current]++; } System.out.println(current == -1 ? -1 : current + 1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
1011f3840f0854da4b6ee3362ca0fc25
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class NewColony { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- != 0) fairnum(); } public static void fairnum() { int n=sc.nextInt(); int k=sc.nextInt(); int i,h[]=new int[n]; for(i=0;i<n;i++) h[i]=sc.nextInt(); if(0==n-1) { System.out.println("-1"); return; } while(k--!=0) { i=0; // if(!(h[i]<h[i+1])) while(h[i]>=h[i+1]) { if(i+1!=n-1) i++; else{ System.out.println("-1"); return; } } h[i]++; } System.out.println(i+1); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output