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
a1433fead4d2c1fee674e08a7a6c79c8
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) > 1$$$.
256 megabytes
import java.util.Scanner; public class Del { static long gcd(long a, long b){ if (b == 0) return a; return gcd(b, a % b); } static long getSum(long n){ long sum; for (sum = 0; n > 0; sum += n % 10, n /= 10); return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ long n = sc.nextLong(); long s = getSum(n); long gcd = gcd(n, s); if(gcd > 1){ System.out.println(n); }else{ long ans = n; while(true){ ans++; s = getSum(ans); gcd = gcd(ans,s); if(gcd > 1){ break; } } System.out.println(ans); } } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$> 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
2a95729223c4ec2bd9b24f80486d9ee4
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) > 1$$$.
256 megabytes
import java.io.*; public class GCDSum { public static long sumDigits(long x) { long sum = 0; while(x > 0) { sum += x % 10; x = x / 10; } return sum; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long gcdSum(long x) { while(gcd(x, sumDigits(x)) <= 1L) { ++x; } return x; } public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t, x; t = Integer.parseInt(br.readLine()); for(int i = 0; i < t; i++) { x = Long.parseLong(br.readLine()); bw.write(String.valueOf(gcdSum(x))); bw.newLine(); } bw.close(); br.close(); } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
a74ce09468c5e55a50093a9659ff6a17
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { long n = sc.nextLong(); long ans = 1; while (ans == 1) { ans = gcd(n, sum(n)); n++; } System.out.printf("%d%n", --n); } sc.close(); } public static long sum(long n) { long ans = 0; while(n > 0) { ans += n % 10; n = n/10; } return ans; } public static long gcd(long a, long b) { if (a < b) return gcd(b, a); if (b == 1) return 1; long m = a % b; if (m == 0) return b; return gcd(b, m); } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
427935559b890be19546c23e484f4fc8
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Code { private static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } private static long sumOfDigitsOfN(long num) { long sum = 0; while (num != 0) { sum += num % 10; num /= 10; } return sum; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { long n = sc.nextLong(); long sum = sumOfDigitsOfN(n); long result = gcd(n, sum); while (result == 1) { n += 1; sum = sumOfDigitsOfN(n); result = gcd(n, sum); } System.out.println(n); } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
9c91e3659b709c2802b20946021ae008
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.io.*; public class Main { public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long getSum(long x){ long sum=0; while(x!=0){ sum+=x%10; x/=10; } return sum; } public static void main(String args[]) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t>0){ long n=Long.parseLong(br.readLine()); long i=n,s=getSum(n); while(true){ long x=gcd(i,s); if(x>1){ System.out.println(i); break; } if(i%10!=9) s++; else{ s=getSum(i+1); } i++; } t--; } br.close(); } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
6f80c141852f9e84180548de1d4156ad
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class gcd_sum { static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long sumofdigits(long n){ long sum = 0; String s = String.valueOf(n); for (int i = 0; i < s.length(); i++) { sum = sum + Integer.parseInt(String.valueOf(s.charAt(i))); } //System.out.println(sum); return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc -- > 0){ long n = sc.nextLong(); boolean flag = true; while (flag){ if(gcd(n,sumofdigits(n) ) != 1){ System.out.println(n); break; } else { n++; } } } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
04ccecde516febe9c2ca344f5567db74
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.io.*; import java.util.*; public class gcd{ public static long gcd_(long a,long b) { if(b==0) return a; else return gcd_(b,a%b); } public static long Digisum(long a) { long sum = 0; while(a!=0) { long rem = a%10; a/=10; sum+= rem; } return sum; } public static void main(String[] args) throws Exception { // String[] parts=br.readLine().split(" "); // int n=Integer.parseInt(parts[0]); // int k=Integer.parseInt(parts[1]); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t > 0) { t--; long n = Long.parseLong(br.readLine()); long val = gcd_(n,Digisum(n)); while(val<=1) { n++; val = gcd_(n,Digisum(n)); } System.out.println(n); } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
06667d5d1237a63071320818f56f3f6f
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
// Working program with FastReader //Let's do it ^.^ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codechef2 { // static final long mod = (long) (1e19+7); 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) { long n = s.nextLong(); for(long i = n;; i++) { long b = sum(i); if(gcd(i,b)>1) { sb.append(i).append("\n"); break; } } } System.out.println(sb.toString()); } public static long gcd(long a, long b) { return b==0L?a:gcd(b,a%b); } public static long sum(long n) { long sum = 0L; while(n>0) { long r = n%10; sum+=r; n/=10; } return sum; } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
23f1b718b7a809064d606857f4250043
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A_GCd { public static void main(String[] args) { try { final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); long [] ts = new long [t]; for (int i = 0; i < t; i++) { String ab = in.readLine(); long a = Long.parseLong(ab); long res = findVal(a); ts[i] = res; } for (int i = 0; i < t; i++) { System.out.println(ts[i]); } } catch (final IOException e) { e.printStackTrace(); } } public static long findVal (long k) { //int [] vals = new int[size]; //int j = 0; int sum = 0; long old = k; while (k >= 10) { // vals[j] = k % 10; sum += k % 10; k = k/10; //j++; //vals[j]; } sum += k; boolean fv = false; if (old % sum == 0 && sum > 1) { return old; } if (sum > 1) { for (int m = 2; m < sum; m++) { if (sum % m == 0 && old % m == 0) { fv = true; break; } } } if (fv) { return old; } else { old++; return findVal(old); } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
06841af3be7a27110524923d15bef4a6
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class trial_1 { static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int it = 0; it < t; it++) { long n = in.nextLong(); if (n == 1) { System.out.println(2); continue; } else if (n < 10) { System.out.println(n); continue; } long tmp = n; for (long i = 0; i < Long.MAX_VALUE ; i++ ) { n = tmp; int digitSum = 0; for (int x = 0; n > 0; x++) { digitSum += (n % 10); n = n / 10; } if (gcd(tmp, digitSum) > 1) { System.out.println(tmp); break; } tmp++; } //System.out.println(n); } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
7dc88cd366c1dbe265adc76fc388cc61
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.util.Scanner; public class gcdSum { public static long getgcd(long a , long b){ if(b==0){ return a; } else { return getgcd(b, a%b); } } public static long getsum(long n){ long sum =0; while (n!=0){ sum=sum+(n%10); n = n/10; } return sum; } public static long mainfun(long n){ while (true){ if(getgcd(n,getsum(n))>1){ return n; } else{ n++; } } } public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); while (t-->0){ long n = input.nextLong(); System.out.println(mainfun(n)); } } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 11
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
53a70cd1a3003f560998b63c64a26c48
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static final int mod = 998244353; static int t = 1; static boolean[] isPrime; static int[] smallestFactorOf; static final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3; static int cmp; @SuppressWarnings({"unused"}) public static void main(String[] args) throws Exception { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { long n = fr.nextLong(); for (long num = n; num > -1; num++) { char[] digs = Long.toString(num).toCharArray(); int len = digs.length; int digsSum = 0; for (int i = 0; i < len; i++) digsSum += (digs[i] - '0'); long gcd = gcd(digsSum, num); if (gcd <= 1) continue; out.println(num); continue OUTER; } } out.close(); } static boolean isPalindrome(char[] s) { char[] rev = s.clone(); reverse(rev); return Arrays.compare(s, rev) == 0; } static class Segment implements Comparable<Segment> { int size; long val; int stIdx; Segment left, right; Segment(int ss, long vv, int ssii) { size = ss; val = vv; stIdx = ssii; } @Override public int compareTo(Segment that) { cmp = size - that.size; if (cmp == 0) cmp = stIdx - that.stIdx; if (cmp == 0) cmp = Long.compare(val, that.val); return cmp; } } static class Pair implements Comparable<Pair> { int first, second; int idx; Pair() { first = second = 0; } Pair (int ff, int ss) {first = ff; second = ss;} Pair (int ff, int ss, int ii) { first = ff; second = ss; idx = ii; } public int compareTo(Pair that) { cmp = first - that.first; if (cmp == 0) cmp = second - that.second; return (int) cmp; } } // (range add - segment min) segTree static int nn; static int[] arr; static int[] tree; static int[] lazy; static void build(int node, int leftt, int rightt) { if (leftt == rightt) { tree[node] = arr[leftt]; return; } int mid = (leftt + rightt) >> 1; build(node << 1, leftt, mid); build(node << 1 | 1, mid + 1, rightt); tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]); } static void segAdd(int node, int leftt, int rightt, int segL, int segR, int val) { if (lazy[node] != 0) { tree[node] += lazy[node]; if (leftt != rightt) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } if (segL > rightt || segR < leftt) return; if (segL <= leftt && rightt <= segR) { tree[node] += val; if (leftt != rightt) { lazy[node << 1] += val; lazy[node << 1 | 1] += val; } lazy[node] = 0; return; } int mid = (leftt + rightt) >> 1; segAdd(node << 1, leftt, mid, segL, segR, val); segAdd(node << 1 | 1, mid + 1, rightt, segL, segR, val); tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]); } static int minQuery(int node, int leftt, int rightt, int segL, int segR) { if (lazy[node] != 0) { tree[node] += lazy[node]; if (leftt != rightt) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } if (segL > rightt || segR < leftt) return Integer.MAX_VALUE / 10; if (segL <= leftt && rightt <= segR) return tree[node]; int mid = (leftt + rightt) >> 1; return Math.min(minQuery(node << 1, leftt, mid, segL, segR), minQuery(node << 1 | 1, mid + 1, rightt, segL, segR)); } static void compute_automaton(String s, int[][] aut) { s += '#'; int n = s.length(); int[] pi = prefix_function(s.toCharArray()); for (int i = 0; i < n; i++) { for (int c = 0; c < 26; c++) { int j = i; while (j > 0 && 'A' + c != s.charAt(j)) j = pi[j-1]; if ('A' + c == s.charAt(j)) j++; aut[i][c] = j; } } } static void timeDFS(int current, int from, UGraph ug, int[] time, int[] tIn, int[] tOut) { tIn[current] = ++time[0]; for (int adj : ug.adj(current)) if (adj != from) timeDFS(adj, current, ug, time, tIn, tOut); tOut[current] = ++time[0]; } static boolean areCollinear(long x1, long y1, long x2, long y2, long x3, long y3) { // we will check if c3 lies on line through (c1, c2) long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); return a == 0; } static int[] treeDiameter(UGraph ug) { int n = ug.V(); int farthest = -1; int[] distTo = new int[n]; diamDFS(0, -1, 0, ug, distTo); int maxDist = -1; for (int i = 0; i < n; i++) if (maxDist < distTo[i]) { maxDist = distTo[i]; farthest = i; } distTo = new int[n + 1]; diamDFS(farthest, -1, 0, ug, distTo); distTo[n] = farthest; return distTo; } static void diamDFS(int current, int from, int dist, UGraph ug, int[] distTo) { distTo[current] = dist; for (int adj : ug.adj(current)) if (adj != from) diamDFS(adj, current, dist + 1, ug, distTo); } static class TreeDistFinder { UGraph ug; int n; int[] depthOf; LCA lca; TreeDistFinder(UGraph ug) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(0, -1, ug, 0, depthOf); lca = new LCA(ug, 0); } TreeDistFinder(UGraph ug, int a) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(a, -1, ug, 0, depthOf); lca = new LCA(ug, a); } private void depthCalc(int current, int from, UGraph ug, int depth, int[] depthOf) { depthOf[current] = depth; for (int adj : ug.adj(current)) if (adj != from) depthCalc(adj, current, ug, depth + 1, depthOf); } public int dist(int a, int b) { int lc = lca.lca(a, b); return (depthOf[a] - depthOf[lc] + depthOf[b] - depthOf[lc]); } } public static long[][] GCDSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; } else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = gcd(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeGCDQ(long[][] table, int l, int r) { // [a,b) if(l > r)return 1; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return gcd(table[t][l], table[t][r-(1<<t)]); } static class Trie { TrieNode root; Trie(char[][] strings) { root = new TrieNode('A', false); construct(root, strings); } public Stack<String> set(TrieNode root) { Stack<String> set = new Stack<>(); StringBuilder sb = new StringBuilder(); for (TrieNode next : root.next) collect(sb, next, set); return set; } private void collect(StringBuilder sb, TrieNode node, Stack<String> set) { if (node == null) return; sb.append(node.character); if (node.isTerminal) set.add(sb.toString()); for (TrieNode next : node.next) collect(sb, next, set); if (sb.length() > 0) sb.setLength(sb.length() - 1); } private void construct(TrieNode root, char[][] strings) { // we have to construct the Trie for (char[] string : strings) { if (string.length == 0) continue; root.next[string[0] - 'a'] = put(root.next[string[0] - 'a'], string, 0); if (root.next[string[0] - 'a'] != null) root.isLeaf = false; } } private TrieNode put(TrieNode node, char[] string, int idx) { boolean isTerminal = (idx == string.length - 1); if (node == null) node = new TrieNode(string[idx], isTerminal); node.character = string[idx]; node.isTerminal |= isTerminal; if (!isTerminal) { node.isLeaf = false; node.next[string[idx + 1] - 'a'] = put(node.next[string[idx + 1] - 'a'], string, idx + 1); } return node; } class TrieNode { char character; TrieNode[] next; boolean isTerminal, isLeaf; boolean canWin, canLose; TrieNode(char c, boolean isTerminallll) { character = c; isTerminal = isTerminallll; next = new TrieNode[26]; isLeaf = true; } } } static class Edge implements Comparable<Edge> { int from, to; long weight, ans; int id; // int hash; Edge(int fro, int t, long wt, int i) { from = fro; to = t; id = i; weight = wt; // hash = Objects.hash(from, to, weight); } /*public int hashCode() { return hash; }*/ public int compareTo(Edge that) { return Long.compare(this.id, that.id); } } public static long[][] minSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMinQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(table[t][l], table[t][r-(1<<t)]); } public static long[][] maxSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMaxQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MIN_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.max(table[t][l], table[t][r-(1<<t)]); } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(UGraph ug, int root) { n = ug.V(); height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(ug, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(UGraph ug, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (int adj : ug.adj(node)) { if (!visited[adj]) { dfs(ug, adj, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static class Point implements Comparable<Point> { long x; long y; long z; long id; // private int hashCode; Point() { x = z = y = 0; // this.hashCode = Objects.hash(x, y, cost); } Point(Point p) { this.x = p.x; this.y = p.y; this.z = p.z; this.id = p.id; // this.hashCode = Objects.hash(x, y, cost); } Point(long x, long y, long z, long id) { this.x = x; this.y = y; this.z = z; this.id = id; // this.hashCode = Objects.hash(x, y, id); } Point(long a, long b) { this.x = a; this.y = b; this.z = 0; // this.hashCode = Objects.hash(a, b); } Point(long x, long y, long id) { this.x = x; this.y = y; this.id = id; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; if (this.z < o.z) return -1; if (this.z > o.z) return 1; return 0; } @Override public boolean equals(Object that) { return this.compareTo((Point) that) == 0; } } static class BinaryLift { // FUNCTIONS: k-th ancestor and LCA in log(n) int[] parentOf; int maxJmpPow; int[][] binAncestorOf; int n; int[] lvlOf; // How this works? // a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}. // b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we // lift level in the tree. public BinaryLift(UGraph tree) { n = tree.V(); maxJmpPow = logk(n, 2) + 1; parentOf = new int[n]; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); parentConstruct(0, -1, tree, 0); binConstruct(); } // TODO: Implement lvlOf[] initialization public BinaryLift(int[] parentOf) { this.parentOf = parentOf; n = parentOf.length; maxJmpPow = logk(n, 2) + 1; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); UGraph tree = new UGraph(n); for (int i = 1; i < n; i++) tree.addEdge(i, parentOf[i]); binConstruct(); parentConstruct(0, -1, tree, 0); } private void parentConstruct(int current, int from, UGraph tree, int depth) { parentOf[current] = from; lvlOf[current] = depth; for (int adj : tree.adj(current)) if (adj != from) parentConstruct(adj, current, tree, depth + 1); } private void binConstruct() { for (int node = 0; node < n; node++) for (int lvl = 0; lvl < maxJmpPow; lvl++) binConstruct(node, lvl); } private int binConstruct(int node, int lvl) { if (node < 0) return -1; if (lvl == 0) return binAncestorOf[node][lvl] = parentOf[node]; if (node == 0) return binAncestorOf[node][lvl] = -1; if (binAncestorOf[node][lvl] != -1) return binAncestorOf[node][lvl]; return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1); } // return ancestor which is 'k' levels above this one public int ancestor(int node, int k) { if (node < 0) return -1; if (node == 0) if (k == 0) return node; else return -1; if (k > (1 << maxJmpPow) - 1) return -1; if (k == 0) return node; int ancestor = node; int highestBit = Integer.highestOneBit(k); while (k > 0 && ancestor != -1) { ancestor = binAncestorOf[ancestor][logk(highestBit, 2)]; k -= highestBit; highestBit = Integer.highestOneBit(k); } return ancestor; } public int lca(int u, int v) { if (u == v) return u; // The invariant will be that 'u' is below 'v' initially. if (lvlOf[u] < lvlOf[v]) { int temp = u; u = v; v = temp; } // Equalizing the levels. u = ancestor(u, lvlOf[u] - lvlOf[v]); if (u == v) return u; // We will now raise level by largest fitting power of two until possible. for (int power = maxJmpPow - 1; power > -1; power--) if (binAncestorOf[u][power] != binAncestorOf[v][power]) { u = binAncestorOf[u][power]; v = binAncestorOf[v][power]; } return ancestor(u, 1); } } static class DFSTree { // NOTE: The thing is made keeping in mind that the whole // input graph is connected. UGraph tree; UGraph backUG; int hasBridge; int n; Edge backEdge; DFSTree(UGraph ug) { this.n = ug.V(); tree = new UGraph(n); hasBridge = -1; backUG = new UGraph(n); treeCalc(0, -1, new boolean[n], ug); } private void treeCalc(int current, int from, boolean[] marked, UGraph ug) { if (marked[current]) { // This is a backEdge. backUG.addEdge(from, current); backEdge = new Edge(from, current, 1, 0); return; } if (from != -1) tree.addEdge(from, current); marked[current] = true; for (int adj : ug.adj(current)) if (adj != from) treeCalc(adj, current, marked, ug); } public boolean hasBridge() { if (hasBridge != -1) return (hasBridge == 1); // We have to determine the bridge. bridgeFinder(); return (hasBridge == 1); } int[] levelOf; int[] dp; private void bridgeFinder() { // Finding the level of each node. levelOf = new int[n]; levelDFS(0, -1, 0); // Applying DP solution. // dp[i] -> Highest level reachable from subtree of 'i' using // some backEdge. dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE / 100); dpDFS(0, -1); // Now, we will check each edge and determine whether its a // bridge. for (int i = 0; i < n; i++) for (int adj : tree.adj(i)) { // (i -> adj) is the edge. if (dp[adj] > levelOf[i]) hasBridge = 1; } if (hasBridge != 1) hasBridge = 0; } private void levelDFS(int current, int from, int lvl) { levelOf[current] = lvl; for (int adj : tree.adj(current)) if (adj != from) levelDFS(adj, current, lvl + 1); } private int dpDFS(int current, int from) { dp[current] = levelOf[current]; for (int back : backUG.adj(current)) dp[current] = Math.min(dp[current], levelOf[back]); for (int adj : tree.adj(current)) if (adj != from) dp[current] = Math.min(dp[current], dpDFS(adj, current)); return dp[current]; } } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) grid[i][j] = fr.nextInt(); } return grid; } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(Comparator<T> cmp) { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { return super.put(key, super.getOrDefault(key, 0) + 1); } public Integer removeCM(T key) { int count = super.getOrDefault(key, -1); if (count == -1) return -1; if (count == 1) return super.remove(key); else return super.put(key, count - 1); } public Integer getCM(T key) { return super.getOrDefault(key, 0); } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static long dioGCD(long a, long b, long[] x0, long[] y0) { if (b == 0) { x0[0] = 1; y0[0] = 0; return a; } long[] x1 = new long[1], y1 = new long[1]; long d = dioGCD(b, a % b, x1, y1); x0[0] = y1[0]; y0[0] = x1[0] - y1[0] * (a / b); return d; } static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) { g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0); if (c % g[0] > 0) { return false; } x0[0] *= c / g[0]; y0[0] *= c / g[0]; if (a < 0) x0[0] = -x0[0]; if (b < 0) y0[0] = -y0[0]; return true; } static long[][] prod(long[][] mat1, long[][] mat2) { int n = mat1.length; long[][] prod = new long[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // determining prod[i][j] // it will be the dot product of mat1[i][] and mat2[][i] for (int k = 0; k < n; k++) prod[i][j] += mat1[i][k] * mat2[k][j]; return prod; } static long[][] matExpo(long[][] mat, long power) { int n = mat.length; long[][] ans = new long[n][n]; if (power == 0) return null; if (power == 1) return mat; long[][] half = matExpo(mat, power / 2); ans = prod(half, half); if (power % 2 == 1) { ans = prod(ans, mat); } return ans; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static boolean[] prefMatchesSuff(char[] s) { int n = s.length; boolean[] res = new boolean[n + 1]; int[] pi = prefix_function(s); res[0] = true; for (int p = n; p != 0; p = pi[p]) res[p] = true; return res; } static int[] prefix_function(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i-1]; while (j > 0 && s[i] != s[j]) j = pi[j-1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static long hash(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static void Yes() {out.println("Yes");}static void YES() {out.println("YES");}static void yes() {out.println("Yes");}static void No() {out.println("No");}static void NO() {out.println("NO");}static void no() {out.println("no");} static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static long mapTo1D(long row, long col, long n, long m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(num + 1); HashMap<Integer, Integer> fnps = new HashMap<>(); while (num != 1) { fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modDiv(long a, long b){return mod(a * power(b, mod - 2, mod), mod);} static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } 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(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();} static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();} static long mod(long a, long m){return(a%m+1000000L*m)%m;} } /* * * int[] arr = new int[] {1810, 1700, 1710, 2320, 2000, 1785, 1780 , 2130, 2185, 1430, 1460, 1740, 1860, 1100, 1905, 1650}; int n = arr.length; sort(arr); int bel1700 = 0, bet1700n1900 = 0, abv1900 = 0; for (int i = 0; i < n; i++) if (arr[i] < 1700) bel1700++; else if (1700 <= arr[i] && arr[i] < 1900) bet1700n1900++; else if (arr[i] >= 1900) abv1900++; out.println("COUNT: " + n); out.println("PERFS: " + toString(arr)); out.println("MEDIAN: " + arr[n / 2]); out.println("AVERAGE: " + Arrays.stream(arr).average().getAsDouble()); out.println("[0, 1700): " + bel1700 + "/" + n); out.println("[1700, 1900): " + bet1700n1900 + "/" + n); out.println("[1900, 2400): " + abv1900 + "/" + n); * * */ // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 17
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
03cba4b2d9efd51eb72d9bc4a0a50db4
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
//https://codeforces.com/problemset/problem/1498/A import java.util.Scanner; public class GCD_Sum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testcase = sc.nextInt(); while (testcase-- != 0) { long n = sc.nextLong(); for (long i = n; i <= n + 2; i++) { long temp = gcd_sum(i); if (temp > 1) { System.out.println(i); break; } } } sc.close(); } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } public static long gcd_sum(long num) { long temp = num; long sum = 0; while (temp > 0) { sum += temp % 10; temp /= 10; } return gcd(num, sum); } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 17
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
b1bc5c87ba65f5daf1c55e2000b5aa12
train_107.jsonl
1617028500
The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) &gt; 1$$$.
256 megabytes
import java.util.Scanner; /** * @author zyx * @version 1.0 * @Date 2022/9/8 19:19 */ public class S013_02Gcd { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); long[] res = new long[t]; for (int i = 0; i < t; i++) { res[i] = ylhGcd(sc.nextLong()); } for (long r : res) { System.out.println(r); } } public static long ylhGcd(long n) { long num = 0; long tem = n; while (tem > 0) { num += tem % 10; tem /= 10; } long gcd = gcd(n, num); if (gcd > 1) { return n; } return ylhGcd(n + 1); } public static long gcd(long n, long m) { if (m == 0) { return n; } return gcd(m, n % m); } }
Java
["3\n11\n31\n75"]
1 second
["12\n33\n75"]
NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1$$$.$$$\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3$$$.So the smallest number $$$\ge 11$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\ 4) = 1$$$.$$$\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1$$$.$$$\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3$$$.So the smallest number $$$\ge 31$$$ whose $$$gcdSum$$$ $$$&gt; 1$$$ is $$$33$$$.Test case 3: $$$\ n = 75$$$: $$$\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3$$$.The $$$\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$&gt; 1$$$. Hence, it is the answer.
Java 17
standard input
[ "brute force", "math" ]
204e75827b7016eb1f1fbe1d6b60b03d
The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.
800
Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.
standard output
PASSED
3e2ab5b0e0324efd18f593752a7c2bf3
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; public final class E { public static void main(String[] args) throws IOException { final PrintWriter pw = new PrintWriter(System.out); final FastReader fs = new FastReader(); final int n = fs.nextInt(); final int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fs.nextInt(); } final List<int[]> list = new ArrayList<>(n * (n + 1) / 2); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { list.add(new int[] { i + 1, j + 1, arr[i] - arr[j] }); } else { list.add(new int[] { j + 1, i + 1, arr[j] - arr[i] }); } } } list.sort((a, b) -> Integer.compare(b[2], a[2])); for (int i = 0; i < list.size(); i++) { final int[] curr = list.get(i); pw.printf("? %d %d\n", curr[0], curr[1]); pw.flush(); if (fs.next().charAt(0) == 'Y') { pw.printf("! %d %d\n", curr[0], curr[1]); pw.flush(); return; } } pw.printf("! %d %d\n", 0, 0); pw.flush(); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(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 { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
bcab24e2b46a0287ed2ae4278dad2297
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; public final class E { public static void main(String[] args) throws IOException { final FastReader fs = new FastReader(); final PrintWriter pw = new PrintWriter(System.out); final int n = fs.nextInt(); final int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fs.nextInt(); } final List<int[]> list = new ArrayList<>(n * (n + 1) / 2); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { list.add(new int[] { i + 1, j + 1, arr[i] - arr[j] }); } else { list.add(new int[] { j + 1, i + 1, arr[j] - arr[i] }); } } } list.sort((a, b) -> Integer.compare(b[2], a[2])); for (int i = 0; i < list.size(); i++) { final int[] curr = list.get(i); pw.printf("? %d %d\n", curr[0], curr[1]); pw.flush(); if ("Yes".equals(fs.next())) { pw.printf("! %d %d\n", curr[0], curr[1]); pw.flush(); return; } } pw.printf("! %d %d\n", 0, 0); pw.flush(); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(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 { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)); return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
eb46b9b8afa64f0cf0d58813b68f80e1
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb; int n=Integer.parseInt(bu.readLine()); StringTokenizer st=new StringTokenizer(bu.readLine()); int i,j,k[]=new int[n]; for(i=0;i<n;i++) k[i]=Integer.parseInt(st.nextToken()); ArrayList<Integer> g[]=new ArrayList[n+1]; for(i=0;i<=n;i++) g[i]=new ArrayList<>(); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(k[i]>0 && k[j]>0) { int abs=Math.abs(k[i]-k[j]); if(k[i]>k[j]) {g[abs].add(i+1); g[abs].add(j+1);} else {g[abs].add(j+1); g[abs].add(i+1);} } int ans[]={0,0}; for(i=n;i>=0;i--) { for(j=0;j<g[i].size();j+=2) { int x=g[i].get(j),y=g[i].get(j+1); sb=new StringBuilder(); sb.append("? "); sb.append(x); sb.append(" "); sb.append(y); sb.append("\n"); System.out.print(sb); String s=bu.readLine(); System.out.flush(); if(s.charAt(0)=='Y') {ans[0]=x; ans[1]=y; break;} } if(ans[0]!=0) break; } System.out.println("! "+ans[0]+" "+ans[1]); System.out.flush(); } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
5b3978e943708ef7d94e2ea552fd0412
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Comparator; public class Main { private static long[] a; private static int n; private static void run() throws IOException { n = in.nextInt(); a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int index = 0; Node[] nodes = new Node[n * (n - 1) / 2]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { nodes[index++] = new Node(i, j); } } Arrays.sort(nodes, Comparator.comparingLong(o -> -Math.abs(a[o.x] - a[o.y]))); for (int i = 0; i < index; i++) { Node now = nodes[i]; out.printf("? %d %d\n", (now.x + 1), (now.y + 1)); out.flush(); if (in.next().charAt(0) == 'Y') { out.printf("! %d %d\n", (now.x + 1), (now.y + 1)); out.flush(); System.exit(0); } } out.println("! 0 0"); out.flush(); System.exit(0); } static class Node { private int x, y; public Node(int x, int y) { if (a[x] > a[y]) { this.x = x; this.y = y; } else { this.x = y; this.y = x; } } } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); // int t = in.nextInt(); // for (int i = 0; i < t; i++) { // } run(); out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
e6b6c3f298f3d91aad9bf4d112d62cdb
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; public class Main { private static long[] a; private static int n; static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(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 { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } private static void run() throws IOException { FastReader scanner = new FastReader(); PrintWriter printWriter = new PrintWriter(System.out); n = scanner.nextInt(); a = new long[n]; for (int i = 0; i < n; i++) a[i] = scanner.nextInt(); PriorityQueue<Node> priorityQueue = new PriorityQueue<>(Comparator.comparingLong(o -> -Math.abs(a[o.x] - a[o.y]))); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { priorityQueue.add(new Node(i, j)); } } while (!priorityQueue.isEmpty()) { Node now = priorityQueue.remove(); printWriter.printf("? %d %d\n", (now.x + 1), (now.y + 1)); printWriter.flush(); if (scanner.next().charAt(0) == 'Y') { printWriter.printf("! %d %d\n", (now.x + 1), (now.y + 1)); printWriter.flush(); System.exit(0); } } printWriter.println("! 0 0"); printWriter.flush(); System.exit(0); } static class Node { private int x, y; public Node(int x, int y) { if (a[x] > a[y]) { this.x = x; this.y = y; } else { this.x = y; this.y = x; } } } public static void main(String[] args) throws IOException { // in = new Reader(new InputStreamReader(System.in)); // out = new PrintWriter(new OutputStreamWriter(System.out)); // int t = in.nextInt(); // for (int i = 0; i < t; i++) { // } run(); // out.flush(); // in.close(); // out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } // @SuppressWarnings("FieldCanBeLocal") // private static Reader in; // private static PrintWriter out; // // private static void print_array(int[] array) { // for (int now : array) { // out.print(now); // out.print(' '); // } // out.println(); // } // // private static void print_array(long[] array) { // for (long now : array) { // out.print(now); // out.print(' '); // } // out.println(); // } static class Reader { BufferedReader reader; StringTokenizer st; Reader(InputStreamReader stream) { reader = new BufferedReader(stream, 32768); st = null; } void close() throws IOException { reader.close(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() throws IOException { return reader.readLine(); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
e4dd6f703ade8bbfcc36cb53b98fa050
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.util.*; import java.io.*; // you can compare with output.txt and expected out // copy and paste, use IDE help to change class name public class Round711E { public static MyPrintWriter out; public static MyScanner in; final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private static void preferFileIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); boolean isDebug = false; boolean isFileIO = false; preferFileIO(isFileIO); Round711E sol = new Round711E(); int n = in.nextInt(); // indegree int[] k = new int[n]; for(int j=0; j<n; j++) k[j] = in.nextInt(); sol.init(k); boolean ing = true; int[] ask; while(ing){ ing = sol.decide(); if(!ing){ ask = sol.getAnswer(); out.printf("! %d %d\n", ask[0], ask[1]); out.flush(); } else{ ask = sol.getQuery(); out.printf("? %d %d\n", ask[0], ask[1]); out.flush(); String res = in.next(); if(res.equals("Yes")) sol.feed(true); else sol.feed(false); } } in.close(); out.close(); } private int[] getAnswer() { if(ans != null) return ans; else return new int[]{0, 0}; // if(vertices.isEmpty()) // return new int[]{0, 0}; // else // return ans; } private boolean decide() { if(vertices.isEmpty()) found = true; else if(minVerticesIdx == 0){ last = iter.next().index; if(minVertices.contains(last)){ ans = new int[]{minVertices.get(0), last}; iter.remove(); while(iter.hasNext()){ iter.next(); iter.remove(); } // for(int i=0; i<minVertices.size(); i++) // vertices.removeFirst(); processVertices(); decide(); } } return !found; } int[] ans = null; private void feed(boolean res) { found = res; if(!found) minVerticesIdx = minVerticesIdx+1 == minVertices.size()? 0 : minVerticesIdx+1; else{ ans = new int[]{minVertices.get(minVerticesIdx), last}; } } int last; int minVerticesIdx; private int[] getQuery() { // exactly one directed road between every pair of houses // inDegree = 0 -> we can ignore that house. decrease indegree's of other houses by 1 // inDegree = n-1 -> ignore that house. decrease n by 1 // inDegree = 1 // inDeg(v) = 1, for all x but u, v -> x // if exists x -> u, then x -> u -> v -> x // if not, inDeg(u) = 0, contradiction. // hence for all x, x is reachable from v // we query as follows: for each x, v is reachable from x? // min inDegree = k // inDeg(v) = k // X -> v -> Y, |X| = k // let x in X, i.e., x -> v // there exists y in Y, s.t. y -> x, since |X\x| = k-1, and min indegree = k // hence x is reachable from v // hence for all u, x is reachable from v // we query as follows: for each u, v is reachable from u? // to maximize |k_A - k_B| // we have to ask starting from big indegree vertices // also have to iterate all min in-degree vertices before we move on // it is possible that min inDegree vertices form a component // we have to handle that as well // ans[0] = A, ans[1] = B return new int[]{last, minVertices.get(minVerticesIdx)}; } ArrayDeque<Vertex> vertices; boolean found; Iterator<Vertex> iter; ArrayList<Integer> minVertices; private void init(int[] k) { ArrayList<Vertex> temp = new ArrayList<Vertex>(k.length); for(int i=0; i<k.length; i++) temp.add(new Vertex(i+1, k[i])); Collections.sort(temp, new Comparator<Vertex>(){ @Override public int compare(Round711E.Vertex o1, Round711E.Vertex o2) { return Integer.compare(o1.inDegree, o2.inDegree); } }); vertices = new ArrayDeque<Vertex>(temp); processVertices(); } private void processVertices() { while(!vertices.isEmpty()){ if(vertices.getLast().inDegree == vertices.size()-1){ vertices.removeLast(); } else if(vertices.getFirst().inDegree == 0){ vertices.removeFirst(); for(Vertex v: vertices) v.inDegree--; } else break; } if(vertices.isEmpty()) found = true; else{ found = false; iter = vertices.descendingIterator(); minVertices = new ArrayList<>(); for(Vertex v: vertices){ if(v.inDegree != vertices.getFirst().inDegree) break; else minVertices.add(v.index); } minVerticesIdx = 0; } } class Vertex{ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getEnclosingInstance().hashCode(); result = prime * result + Objects.hash(inDegree, index); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Vertex other = (Vertex) obj; if (!getEnclosingInstance().equals(other.getEnclosingInstance())) return false; return inDegree == other.inDegree && index == other.index; } int index; int inDegree; public Vertex(int index, int inDegree) { this.index = index; this.inDegree = inDegree; } @Override public String toString() { return "Vertex [index=" + index + ", inDegree=" + inDegree + "]"; } private Round711E getEnclosingInstance() { return Round711E.this; } } long solve(String s){ return 0; } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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 MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } public void print(long[] arr){ if(arr != null){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void println(long[] arr){ print(arr); println(); } public void print(int[] arr){ if(arr != null){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void println(int[] arr){ print(arr); println(); } public <T> void print(ArrayList<T> arr){ if(arr != null){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void println(ArrayList<T> arr){ print(arr); println(); } public void println(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void println(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
42ccdf39063116da40a0ad48f960e720
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; public class CF1498E extends PrintWriter { CF1498E() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1498E o = new CF1498E(); o.main(); o.flush(); } void main() { int n = sc.nextInt(); int[] kk = new int[n]; Integer[] ii = new Integer[n]; for (int i = 0; i < n; i++) { kk[i] = sc.nextInt(); ii[i] = i; } Arrays.sort(ii, (i, j) -> kk[i] - kk[j]); int d_ = -1, i_ = -1, j_ = -1; for (int h = -1, k = 0, i = 0; i < n; i++) if ((k += kk[ii[i]]) == i * (i + 1) / 2) { if (i - h >= 2) { int d = kk[ii[i]] - kk[ii[h + 1]]; if (d_ < d) { d_ = d; i_ = ii[i]; j_ = ii[h + 1]; } } h = i; } println("! " + (i_ + 1) + " " + (j_ + 1)); } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
cdb81d7c0e0069004120392b9a189cc4
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Comparator; public class Main { private static long[] a; private static int n; private static void run() throws IOException { n = in.nextInt(); a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int index = 0; Node[] nodes = new Node[n * (n - 1) / 2]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { nodes[index++] = new Node(i, j); } } Arrays.sort(nodes, Comparator.comparingLong(o -> -Math.abs(a[o.x] - a[o.y]))); for (int i = 0; i < index; i++) { Node now = nodes[i]; out.printf("? %d %d\n", (now.x + 1), (now.y + 1)); out.flush(); if (in.next().charAt(0) == 'Y') { out.printf("! %d %d\n", (now.x + 1), (now.y + 1)); out.flush(); System.exit(0); } } out.println("! 0 0"); out.flush(); System.exit(0); } static class Node { private int x, y; public Node(int x, int y) { if (a[x] > a[y]) { this.x = x; this.y = y; } else { this.x = y; this.y = x; } } } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); // int t = in.nextInt(); // for (int i = 0; i < t; i++) { // } run(); out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
e334bf2fe56ef69c02bd4abf3f76bfef
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
//package round711; import java.io.*; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Queue; public class E { InputStream is; FastWriter out; String INPUT = ""; void solve() { int n = ni(); int[] a = na(n); int[][] ai = new int[n*(n-1)][]; int p = 0; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ if(i != j && a[j] - a[i] >= 0){ ai[p++] = new int[]{a[j] - a[i], j, i}; } } } ai = Arrays.copyOf(ai, p); Arrays.sort(ai, (x, y) -> { if (x[0] != y[0]) return x[0] - y[0]; return (x[1] - y[1]); }); for(int i = ai.length-1;i >= 0;i--){ out.println("? " + (ai[i][1]+1) + " " + (ai[i][2]+1)); out.flush(); if(ns().equals("Yes")){ out.println("! " + (ai[i][1]+1) + " " + (ai[i][2]+1)); out.flush(); return; } } out.println("! 0 0"); out.flush(); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().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 && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } 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[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } 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(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 11
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
f2331b44d56104a408deafcc84894eae
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
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.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.util.Collections; 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); ETwoHouses solver = new ETwoHouses(); solver.solve(1, in, out); out.close(); } static class ETwoHouses { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); ArrayList<ETwoHouses.Pair> list = new ArrayList<>(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] >= arr[j]) { list.add(new ETwoHouses.Pair(i + 1, j + 1, arr[i] - arr[j])); } else { list.add(new ETwoHouses.Pair(j + 1, i + 1, arr[j] - arr[i])); } } } Collections.sort(list); for (ETwoHouses.Pair p : list) { out.println("? " + p.a + " " + p.b); out.flush(); String ans = in.next(); if (ans.equals("Yes")) { out.println("! " + p.a + " " + p.b); out.flush(); out.close(); return; } } out.println("! 0 0"); out.flush(); out.close(); } static class Pair implements Comparable<ETwoHouses.Pair> { int a; int b; int c; Pair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(ETwoHouses.Pair v) { return -(c - v.c); } } } 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 println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
6ff43c9d8c63bcad7fb39a21467539a8
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
//created by Whiplash99 import java.io.*; import java.util.*; public class E { private static PrintWriter out; private static void ask(int a, int b) { out.println("? "+a+" "+b); out.flush(); } private static void answer(int a, int b) { out.println("! "+a+" "+b); out.flush(); System.exit(0); } static class Pair implements Comparable<Pair> { int f, s, t; Pair(int f, int s, int t){this.f = f;this.s = s; this.t=t;} public int compareTo(Pair b){return Integer.compare(b.t, this.t);} } public static void main(String[] args) throws Exception { Reader r=new Reader(); out=new PrintWriter(System.out); int i,j,k,N; N=r.nextInt(); int[] a=new int[N]; for(i=0;i<N;i++) a[i]=r.nextInt(); r.readLine(); Pair[] pairs=new Pair[N*(N-1)/2]; k=0; for(i=0;i<N;i++) { for(j=i+1;j<N;j++) pairs[k++]=new Pair(i,j,Math.abs(a[i]-a[j])); } Arrays.sort(pairs); for(Pair p:pairs) { int u=p.f, v=p.s; if(a[u]>a[v]) ask(u+1,v+1); else ask(v+1,u+1); if(r.readLine().trim().charAt(0)=='Y') answer(u+1,v+1); } answer(0,0); } 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];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
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
73cf081baa96fc0766be86a6b85c2ece
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
//created by Whiplash99 import java.io.*; import java.util.*; public class E { private static void ask(int a, int b) { System.out.println("? "+a+" "+b); System.out.flush(); } private static void answer(int a, int b) { System.out.println("! "+a+" "+b); System.out.flush(); System.exit(0); } static class Pair implements Comparable<Pair> { int f, s, t; Pair(int f, int s, int t){this.f = f;this.s = s; this.t=t;} public int compareTo(Pair b){return Integer.compare(b.t, this.t);} } public static void main(String[] args) throws Exception { Reader r=new Reader(); int i,j,k,N; N=r.nextInt(); int[] a=new int[N]; for(i=0;i<N;i++) a[i]=r.nextInt(); r.readLine(); Pair[] pairs=new Pair[N*(N-1)/2]; k=0; for(i=0;i<N;i++) { for(j=i+1;j<N;j++) pairs[k++]=new Pair(i,j,Math.abs(a[i]-a[j])); } Arrays.sort(pairs); for(Pair p:pairs) { int u=p.f, v=p.s; if(a[u]>a[v]) ask(u+1,v+1); else ask(v+1,u+1); if(r.readLine().trim().charAt(0)=='Y') answer(u+1,v+1); } answer(0,0); } 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];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
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
f3e51c4e6f1b6e6dab8349f8fbd2bf51
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; public class Main2{ static void main() throws Exception{ int n=sc.nextInt(); int[][]deg=new int[n][]; for(int i=0;i<n;i++)deg[i]=new int[] {i,sc.nextInt()}; Arrays.sort(deg,(x,y)->x[1]-y[1]); int[][]arr=new int[n*(n-1)/2][]; int idx=0; for(int c=n-1;c>=0;c--) { int i=deg[c][0]; for(int o=0;o<c;o++) { int j=deg[o][0]; arr[idx++]=new int[] {i,j,deg[c][1]-deg[o][1]}; } } Arrays.sort(arr,(x,y)->y[2]-x[2]); for(int c=0;c<arr.length;c++) { int i=arr[c][0],j=arr[c][1]; pw.println("? "+(i+1)+" "+(j+1)); pw.flush(); boolean yes=sc.next().equals("Yes"); if(yes) { pw.println("! "+(i+1)+" "+(j+1)); return; } } pw.println("! 0 0"); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; // tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case %d:\n", i); main(); } pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
c9f43219da94ddadc87144a01a2658d9
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.util.*; import java.io.*; public class Two_Houses { 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 shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } Arrays.sort(a); } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static FastReader t = new FastReader(); private static PrintWriter o = new PrintWriter(System.out); public static void main(String[] args) { // TODO Auto-generated method stub int n = t.nextInt(); int[] a = new int[n]; boolean flag = false; PriorityQueue<Pair> pq = new PriorityQueue<>((x, y) -> y.abs - x.abs); for (int i = 0; i < n; ++i) a[i] = t.nextInt(); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (a[i] >= a[j]) pq.add(new Pair(i + 1, j + 1, a[i] - a[j])); else pq.add(new Pair(j + 1, i + 1, a[j] - a[i])); while (!pq.isEmpty()) { Pair p = pq.remove(); String res = query(p.i, p.j); if (res.equals("Yes")) { flag = true; o.println("! " + p.i + " " + p.j); break; } } if (!flag) o.println("! 0 0"); o.flush(); o.close(); } private static String query(int x, int y) { o.println("? " + x + " " + y); o.flush(); return t.next(); } } class Pair { int i; int j; int abs; Pair(int i, int j, int abs) { this.i = i; this.j = j; this.abs = abs; } } //ok
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
dbebbe06b29c05c3072605d292c5e7f2
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.util.*; import java.io.*; public class Two_Houses { 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 shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } Arrays.sort(a); } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static FastReader t = new FastReader(); private static PrintWriter o = new PrintWriter(System.out); public static void main(String[] args) { // TODO Auto-generated method stub int n = t.nextInt(); int[] a = new int[n]; boolean flag = false; List<Pair> list = new ArrayList<>(); for (int i = 0; i < n; ++i) a[i] = t.nextInt(); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (a[i] >= a[j]) list.add(new Pair(i + 1, j + 1, a[i] - a[j])); else list.add(new Pair(j + 1, i + 1, a[j] - a[i])); Collections.sort(list, (x, y) -> y.abs - x.abs); for (Pair p : list) { String res = query(p.i, p.j); if (res.equals("Yes")) { flag = true; o.println("! " + p.i + " " + p.j); break; } } if (!flag) o.println("! 0 0"); o.flush(); o.close(); } private static String query(int x, int y) { o.println("? " + x + " " + y); o.flush(); return t.next(); } } class Pair { int i; int j; int abs; Pair(int i, int j, int abs) { this.i = i; this.j = j; this.abs = abs; } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
7e755103f26b2e201bbed8eec7cc4ee9
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.awt.List; import java.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Hashtable; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class N711 { static PrintWriter out; static Scanner sc; static ArrayList<Integer>q,w; static ArrayList<Integer>adj[]; static HashSet<Integer>primesH; static boolean prime[]; //static ArrayList<Integer>a; static HashSet<Long>tmp; static boolean[]v; static int[]a,b,c,d; static long l[],dp[][][]; static char[][]mp; static int A,B,n,m,h; static long oo=((long)1e9)+7; public static void main(String[]args) throws IOException { sc=new Scanner(System.in); out=new PrintWriter(System.out); //A(); //B(); // C(); // CC(); //D(); E(); //minimum for subarrays of fixed length //F(); out.close(); } static long gcd (long a, long b) { return b==0?a:gcd (b, a % b); } private static void A() throws IOException { int t=ni(); while(t-->0) { long n=nl(); for(;;n++) { String s=n+""; int sum=0; for(int i=0;i<s.length();i++) { sum+=s.charAt(i)-'0'; } long g=gcd(n,sum); if(g>1)break; } out.println(n); } } static void B() throws IOException { int t=ni(); while(t-->0) { int n=ni(),w=ni(); a=nai(n); int h=0; int lrg=1; TreeMap<Integer, Integer>tr=new TreeMap<Integer, Integer>(); for(int i=0;i<n;i++) { lrg=Math.max(lrg, a[i]); tr.put(a[i], tr.getOrDefault(a[i], 0)+1); } int tk=0; for(;tk<n&&lrg>0;h++) { int ww=w; int lrgg=lrg; while(tk<n&&lrgg>=1) { int ft=ww/lrgg; if(ww==0)break; int val=tr.getOrDefault(lrgg, 0); int tt=Math.min(ft,val ); if(tt>0) tr.put(lrgg, val-tt); ww=ww-lrgg*tt; lrgg/=2; tk+=tt; } while(lrg>0&&tr.getOrDefault(lrg, 0)==0) { lrg/=2; } } out.println(h); } } static void C() throws IOException{ int t=ni(); while(t-->0) { int n=ni(),k=ni(); long ans; long vs[]=new long[n-1]; // Arrays.fill(vs, 1); if(k==1)out.println(1); else if(k==2)out.println(n+1); else { ans=n+1; for(int i=0;i<vs.length;i++) { vs[i]=i+1; //ans=(ans+vs[i]%oo)%oo; } long pr=n*(n-1)/2; ans=(ans+pr)%oo; for(int i=0;i<k-3&&n>1;i++) { if(i%2==1) { long tmp=vs[n-2]; vs[n-2]=pr; long sum=pr; ans=(ans+pr)%oo; for(int j=n-3;j>=0;j--) { //long tmp=vs[j+1]; pr-=tmp; if(pr<0)pr+=oo; tmp=vs[j]; vs[j]=pr; sum=(sum+vs[j])%oo; ans=(ans+vs[j])%oo; } pr=sum; }else { long tmp=vs[0]; vs[0]=pr; long sum=pr; ans=(ans+pr)%oo; for(int j=1;j<n-1;j++) { pr-=tmp; if(pr<0) { pr=(pr+oo); } tmp=vs[j]; vs[j]=pr; sum=(sum+vs[j])%oo; ans=(ans+vs[j])%oo; } pr=sum; } } //ans=(ans+pr)%oo; out.println(ans); //out.println(oo); } } } static void CC() throws IOException { int t=ni(); while(t-->0) { n=ni(); int k=ni(); dp=new long[n][k+1][2]; for(long[][]d1:dp) for(long[]d2:d1)Arrays.fill(d2, -1); out.println(dp(0,k,0)); } } private static long dp(int i, int k, int d) { if(k==1)return 1; if(i<0||i==n)return 0; if(dp[i][k][d]!=-1)return dp[i][k][d]; long ans=2; if(d==0) { if(i<n-1) { ans+=dp(i+1,k,d)-1; } ans%=oo; if(i>0) { ans+=dp(i-1,k-1,1-d)-1; } ans%=oo; }else { if(i<n-1) { ans+=dp(i+1,k-1,1-d)-1; } ans%=oo; if(i>0) { ans+=dp(i-1,k,d)-1; } ans%=oo; } return dp[i][k][d]=ans; } static void D() throws IOException { int n=ni(),m=ni(); double[][]vs=new double[n][3]; for(int i=0;i<n;i++) { vs[i][0]=ni(); vs[i][1]=nl()/100000.0; vs[i][2]=ni(); } int[]ans=new int[m+1]; Arrays.fill(ans, -1); boolean[]v=new boolean[m+1],vv=new boolean[m+1]; v[0]=true; for(int i=0;i<n;i++) { int hh=0; for(boolean x:v) { vv[hh++]=x; } int t=(int)vs[i][0]; int cur=(int)Math.ceil(vs[i][1]); int c=(int)vs[i][2]; if(t==1) { // for(int j=cur,cc=c;j<=m&&cc>0;j+=cur,cc--) { // if(ans[j]==-1)ans[j]=i+1; // //else break; // } for(int j=0;j<=m;j++) { if(!v[j])continue; //if(ans[j]==-1)continue; for(long k=j*1l,cc=c;k<=m&&cc>=0;k+=cur,cc--) { if(v[(int)k])break; if(cc<c) vv[(int)k]=true; ans[(int)k]=i+1; } } }else { for(int j=0;j<=m;j++) { if(!v[j])continue; //if(ans[j]==-1)continue; long k=j*1l; for(int cc=c;k<=m&&cc>=0;k=(long)Math.ceil(k*1l*vs[i][1]),cc--) { if(v[(int)k])break; if(cc<c) vv[(int)k]=true; ans[(int)k]=i+1; } } } hh=0; for(boolean x:vv) { v[hh++]=x; } } for(int i=1;i<=m;i++)out.print(ans[i]+" "); out.println(); } static void E() throws IOException { int n=ni(); a=nai(n); ArrayList<Tri> pq=new ArrayList<Tri>(); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { pq.add(new Tri(i+1,j+1,Math.abs(a[i]-a[j]))); } } Collections.sort(pq); int i=0; while(i<pq.size()) { Tri p=pq.get(i++); int x=a[p.x-1],y=a[p.y-1]; if(x<y) { int tmp=p.x; p.x=p.y; p.y=tmp; } out.println("? "+p.x+" "+p.y); out.flush(); String s=ns(); if(s.charAt(0)=='Y'){ out.println("! "+p.x+" "+p.y); out.flush(); out.close();return; } } out.println("! "+0+" "+0); out.flush(); out.close(); } static class Tri implements Comparable<Tri>{ int x,y,z; public Tri(int x,int y,int z) { this.x=x; this.y=y; this.z=z; } @Override public int compareTo(Tri t) { return t.z-z; } } private static void paint(char[][] mp, int i) { for(int j=0;j<mp.length;j++) { mp[j][i]='X'; } if(i>1) { for(int a=0;a<mp.length;a++) { if(mp[a][i-2]=='X') { mp[a][i-1]='X'; break; } if(a==mp.length-1) { mp[a][i-1]='X'; } } } if(i<mp[0].length-2) { for(int a=0;a<mp.length;a++) { if(mp[a][i+2]=='X') { mp[a][i+1]='X'; break; } if(a==mp.length-1) { mp[a][i+1]='X'; } } } } static int ni() throws IOException { return sc.nextInt(); } static double nd() throws IOException { return sc.nextDouble(); } static long nl() throws IOException { return sc.nextLong(); } static String ns() throws IOException { return sc.next(); } static int[] nai(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); return a; } static long[] nal(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); return a; } static int[][] nmi(int n,int m) throws IOException{ int[][]a=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); } } return a; } static long[][] nml(int n,int m) throws IOException{ long[][]a=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextLong(); } } return a; } static void o(String x) { out.print(x); } static void ol(String x) { out.println(x); } static void ol(int x) { out.println(x); } static void disp1(int []a) { for(int i=0;i<a.length;i++) { out.print(a[i]+" "); } out.println(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
03d8007c095f7f716e23b58b46c43ea1
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.BufferedInputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import java.util.Random; public class P1498E { private static void query(int a, int b) { System.out.println("? " + a + " " + b); System.out.flush(); } private static void guess(int a, int b) { System.out.println("! " + a + " " + b); System.out.flush(); } public static void main(String[] args) throws IOException { try { final FastReader fr = new FastReader(System.in); final int n = fr.readInt(); final int[][] K = new int[n][2]; for (int i = 0; i < n; i++) { K[i][0] = fr.readInt(); K[i][1] = i+1; } Arrays.sort(K, (k1, k2) -> Integer.compare(k1[0], k2[0])); int i0; for (i0 = 0; i0 < n && K[i0][0] == i0; i0++); int i1; for (i1 = n-1; i1 > i0 && K[i1][0] == i1; i1--); if (i0 >= i1) { guess(0, 0); return; } int n1 = (i1 - i0 + 1); final int[][] P = new int[n1 * (n1+1) / 2][2]; int k = 0; for (int i = i0; i < i1; i++) { for (int j = i + 1; j <= i1; j++) { P[k][0] = i; P[k][1] = j; k++; } } Arrays.sort(P, (p1, p2) -> Integer.compare( K[p2[1]][0] - K[p2[0]][0], K[p1[1]][0] - K[p1[0]][0])); final byte[] buf = new byte[3]; for (int[] p : P) { final int a = K[p[1]][1]; final int b = K[p[0]][1]; query(a, b); fr.readToken(buf); if (buf[0] == 'Y') { guess(a, b); return; } } guess(0, 0); } catch (Throwable t) { t.printStackTrace(System.out); System.exit(1); } } private static final void log(Object o) { System.err.println(o); } private static final void log(String format, Object... args) { System.err.println(String.format(null, format, args)); } private static class FastReader implements Closeable { private static final byte B_SP = ' '; private static final byte B_CR = '\r'; private static final byte B_LF = '\n'; private static final byte B_0 = '0'; private static final byte B_9 = '9'; private static final byte B_DASH = '-'; private final InputStream in; private byte prev; public FastReader(InputStream in) { this.in = in; } public byte read() throws IOException { return (prev = (byte) in.read()); } public byte readNonWs() throws IOException { byte b; do { b = read(); } while (b <= B_SP); return b; } public int readInt() throws IOException { byte b = readNonWs(); final boolean neg = (b == B_DASH); if (neg) { b = read(); } int i = (b - B_0); b = read(); while (b >= B_0 && b <= B_9) { i = i * 10 + (b - B_0); b = read(); } return neg ? -i : i; } public int[] readInts(int n) throws IOException { final int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = readInt(); } return arr; } public int[][] readInts(int n, int m) throws IOException { final int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = readInt(); } } return arr; } public long readLong() throws IOException { byte b = readNonWs(); final boolean neg = (b == B_DASH); if (neg) { b = read(); } long i = (b - B_0); b = read(); while (b >= B_0 && b <= B_9) { i = i * 10 + (b - B_0); b = read(); } return neg ? -i : i; } public long[] readLongs(int n) throws IOException { final long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = readLong(); } return arr; } public long[][] readLongs(int n, int m) throws IOException { final long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = readLong(); } } return arr; } public int readToken(byte[] tbuf) throws IOException { byte b = readNonWs(); int i = 0; do { tbuf[i++] = b; b = read(); } while (b >= B_SP); return i; } public int readLine(byte[] lbuf) throws IOException { byte b; if (prev == B_CR) { b = read(); if (b == B_LF) { b = read(); } } else { b = read(); } int i = 0; while (b >= B_SP) { lbuf[i++] = b; b = read(); } return i; } @Override public void close() throws IOException { in.close(); } } private static int[] toIntArray(final List<Integer> lst) { final int[] arr = new int[lst.size()]; int i = 0; for (final int x : lst) { arr[i++] = x; } return arr; } private static long[] toLongArray(final List<Long> lst) { final long[] arr = new long[lst.size()]; int i = 0; for (final long x : lst) { arr[i++] = x; } return arr; } private static int[][] getDarts(final int n, final int m, final int[][] E) { final List<List<Integer>> darts = new ArrayList<>(n); for (int i = 0; i < n; i++) { darts.add(new ArrayList<>()); } for (int j = 0; j < m; j++) { final int u = E[j][0]; final int v = E[j][1]; darts.get(u).add(v); darts.get(v).add(u); } final int[][] D = new int[n][]; int i = 0; for (final List<Integer> lst : darts) { D[i++] = toIntArray(lst); } return D; } private static final Random rng = new Random(); private static void shuffle(int[] arr, int lo, int hi) { int k = hi - lo; for (int i = hi-1; i > lo; i--) { int j = lo + rng.nextInt(k--); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } private static void sort(int[] arr) { shuffle(arr, 0, arr.length); Arrays.sort(arr); } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
7bbb38968cd5d968e0c5996400989247
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static long mod = (long) 1e9 + 7; static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int n = i(); int[] degree = input(n); PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o2[0] - o1[0]; } }); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (degree[i] >= degree[j]) { pq.offer(new int[]{degree[i] - degree[j], i, j}); } else { pq.offer(new int[]{degree[j] - degree[i], j, i}); } } } while (!pq.isEmpty()) { int[] a = pq.poll(); int i = a[1]; int j = a[2]; out.println("? " + (i + 1) + " " + (j + 1)); out.flush(); if (s().equals("Yes")) { out.println("! " + (i + 1) + " " + (j + 1)); out.flush(); return; } } out.println("! 0 0"); out.flush(); } static int sd(long i) { int d = 0; while (i > 0) { d += i % 10; i = i / 10; } return d; } static int lower(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] >= x) { r = m; } else { l = m; } } return r; } static int upper(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] <= x) { l = m; } else { r = m; } } return l; } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static int lowerBound(int A[], int low, int high, int x) { if (low > high) { if (x >= A[high]) { return A[high]; } } int mid = (low + high) / 2; if (A[mid] == x) { return A[mid]; } if (mid > 0 && A[mid - 1] <= x && x < A[mid]) { return A[mid - 1]; } if (x < A[mid]) { return lowerBound(A, low, mid - 1, x); } return lowerBound(A, mid + 1, high, x); } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) { out.print(a + " "); } out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } 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); } } } class Pair { long i, j; Pair(long i, long j) { this.i = i; this.j = j; } } 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
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
08c316b5c7f500e2f47d39654d5a9c41
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; /* polyakoff */ public class Main { static FastReader in; static PrintWriter out; static Random rand = new Random(); static final int oo = (int) 1e9 + 10; static final long OO = (long) 1e18 + 10; static final int MOD = (int) 1e9 + 7; static class Pair { int v, u, delta; Pair(int v, int u, int delta) { this.v = v; this.u = u; this.delta = delta; } } static boolean ask(int v, int u) { out.println("? " + (v + 1) + " " + (u + 1)); out.flush(); return in.next().equals("Yes"); } static void solve() { int n = in.nextInt(); int[] k = new int[n]; for (int i = 0; i < n; i++) { k[i] = in.nextInt(); } ArrayList<Pair> pairs = new ArrayList<>(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (k[i] >= k[j]) pairs.add(new Pair(i, j, k[i] - k[j])); else pairs.add(new Pair(j, i, k[j] - k[i])); } } Collections.sort(pairs, (o1, o2) -> { if (o1.delta == o2.delta) { if (o1.v == o2.v) return o1.u - o2.u; return o1.v - o2.v; } return o2.delta - o1.delta; }); for (Pair p : pairs) { if (ask(p.v, p.u)) { out.printf("! " + (p.v + 1) + " " + (p.u + 1)); out.flush(); return; } } out.println("! 0 0"); out.flush(); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); int t = 1; // t = in.nextInt(); while (t-- > 0) { solve(); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { this(System.in); } FastReader(String file) throws FileNotFoundException { this(new FileInputStream(file)); } FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String line; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
f64ad4a5890d5bb0920d3dc27cf5894f
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { // lol who need the queries, they are overrated anyway Xd int n=sc.nextInt(); Integer[][] a=new Integer[n][2]; for(int i=0;i<n;i++) { a[i][0]=sc.nextInt(); a[i][1]=i+1; } Arrays.sort(a,(x,y)->x[0]-y[0]); long sum=0; long x=0; long y=0; TreeSet<pair>ts=new TreeSet<pair>(); long cow=-1; for(int i=0;i<n;i++) { sum+=a[i][0]; ts.add(new pair(a[i][0], a[i][1])); if(sum==(i*1l*i+i)/2) { if(ts.size()>1) { if(ts.last().x-ts.first().x>cow) { cow=ts.last().x-ts.first().x; x=ts.first().y; y=ts.last().y; } } ts=new TreeSet<Main.pair>(); } } pw.println("! "+x+" "+y); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
8380a39ba864803d828fba762d01808f
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.util.*; public class Main2{ static void main() throws Exception{ int n=sc.nextInt(); int[][]deg=new int[n][]; for(int i=0;i<n;i++)deg[i]=new int[] {i,sc.nextInt()}; Arrays.sort(deg,(x,y)->x[1]-y[1]); int[][]arr=new int[n*(n-1)/2][]; int idx=0; for(int c=n-1;c>=0;c--) { int i=deg[c][0]; for(int o=0;o<c;o++) { int j=deg[o][0]; arr[idx++]=new int[] {i,j,deg[c][1]-deg[o][1]}; } } Arrays.sort(arr,(x,y)->y[2]-x[2]); for(int c=0;c<arr.length;c++) { int i=arr[c][0],j=arr[c][1]; pw.println("? "+(i+1)+" "+(j+1)); pw.flush(); boolean yes=sc.next().equals("Yes"); if(yes) { pw.println("! "+(i+1)+" "+(j+1)); return; } } pw.println("! 0 0"); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; // tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case %d:\n", i); main(); } pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
1478b62c50b48227c32abd78ed4a969d
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class A implements Runnable { public void run() { long startTime = System.nanoTime(); int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt() << 9 | i + 1; } Arrays.sort(a); int z = n; int ax = 0, ay = 0, res = -1; M: while (z > 0) { for (int k = 1; k <= z; k++) { int s = 0; for (int i = 0; i < k; i++) { s += a[z - i - 1] >> 9; } if (s == k * (2 * z - k - 1) / 2) { for (int i = 0; i < k; i++) { for (int j = 0; j < i; j++) { int x = a[z - i - 1]; int y = a[z - j - 1]; int cur = Math.abs((x >> 9) - (y >> 9)); if (res < cur) { res = cur; ax = x & 511; ay = y & 511; } } } z -= k; continue M; } } } println("! " + ax + " " + ay); if (fileIOMode) { System.out.println((System.nanoTime() - startTime) / 1e9); } out.close(); } //----------------------------------------------------------------------------------- private static boolean fileIOMode; private static BufferedReader in; private static PrintWriter out; private static StringTokenizer tokenizer; public static void main(String[] args) throws Exception { fileIOMode = args.length > 0 && args[0].equals("!"); if (fileIOMode) { in = new BufferedReader(new FileReader("a.in")); out = new PrintWriter("a.out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tokenizer = new StringTokenizer(""); new Thread(new A()).start(); } private static String nextLine() { try { return in.readLine(); } catch (IOException e) { return null; } } private static String nextToken() { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } private static int nextInt() { return Integer.parseInt(nextToken()); } private static long nextLong() { return Long.parseLong(nextToken()); } private static double nextDouble() { return Double.parseDouble(nextToken()); } private static BigInteger nextBigInteger() { return new BigInteger(nextToken()); } private static void print(Object o) { if (fileIOMode) { System.out.print(o); } out.print(o); } private static void println(Object o) { if (fileIOMode) { System.out.println(o); } out.println(o); } private static void printf(String s, Object... o) { if (fileIOMode) { System.out.printf(s, o); } out.printf(s, o); } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
41bc425be3caff30963ed49202bebe70
train_107.jsonl
1617028500
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$|k_A - k_B|$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.util.BitSet; import java.util.function.BinaryOperator; public class D { 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 s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(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] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static int MOD=(int)1e9+7; public static void main(String[] args) { StringBuilder ans = new StringBuilder(); // int t = ri(); int t=1; while(t-- >0) { int n=ri(); List<Node> list=new ArrayList<>(); for(int i=1;i<=n;i++) { list.add(new Node(i,ri())); } list.sort(new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { if(o1.val==o2.val) { return o1.i-o2.i; } return o1.val-o2.val; } }); boolean found = false; for(int i=list.size()-1;i>=0;i--) { Node node = list.get(i); for(int j=0;j<i;j++) { out.println("? " + node.i + " " + list.get(j).i); out.flush(); String str=rs(); if(str.equals("Yes")) { out.println("! "+node.i+" "+list.get(j).i); out.flush(); found = true; break; } } if(found) { break; } } if(!found) { out.println("! 0 0"); out.flush(); } } // out.print(ans.toString()); // out.flush(); } static class Node { int i,val; public Node(int i, int val) { this.i = i; this.val = val; } } }
Java
["3\n1 1 1\nYes", "4\n1 2 0 3\nNo\nNo\nNo\nNo\nNo\nNo"]
3.5 seconds
["? 1 2\n! 1 2", "? 2 1\n? 1 3\n? 4 1\n? 2 3\n? 4 2\n? 4 3\n! 0 0"]
NoteIn the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
Java 8
standard input
[ "brute force", "graphs", "greedy", "interactive", "sortings" ]
90a9d883408d5c283d6e7680c0b94c8b
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$0 \le k_i \le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house.
2,200
null
standard output
PASSED
61f117507f24478d54b4106ca6515a31
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.*; public class Main { static void solve(int n,int m,int a[],int b[], int c[],int req[],int ava[],int freq[]){ int V = n+1; ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(V); for (int i = 0; i < V; i++) list.add(new ArrayList<Integer>()); for(int i=1;i<=n;i++){ if(a[i]!=b[i]){ req[b[i]]++; list.get(b[i]).add(i); } freq[b[i]]++; } if(freq[c[m]]==0) { System.out.println("no"); return; } for(int i=1;i<=n;i++){ if(req[i]>ava[i]){ System.out.println("no"); return; } } System.out.println("yes"); int last=-1; int z = list.get(c[m]).size(); if(z>0) { last=list.get(c[m]).get(z-1); list.get(c[m]).remove(z-1); } else{ for(int i=1;i<=n;i++){ if(b[i]==c[m]) { last=i; break; } } } for(int i=1;i<=m;i++){ if(list.get(c[i]).size()>0){ System.out.print(list.get(c[i]).get(0)+" "); list.get(c[i]).remove(0); } else System.out.print(last+" "); } System.out.println(); } public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int a[]=new int[n+1]; int b[]=new int[n+1]; int c[]=new int[m+1]; int req[]=new int[n+1]; int ava[]=new int[n+1]; int freq[]=new int[n+1]; for(int i=1 ;i<=n;i++) a[i]=sc.nextInt(); for(int i=1;i<=n;i++) b[i]=sc.nextInt(); for(int i=1;i<=m;i++){ c[i]=sc.nextInt(); ava[c[i]]++; } solve(n,m,a,b,c,req,ava,freq); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
1419f02f670b90d124a746022831ebc4
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(), m = in.nextInt(); int a[] = in.readArray(n), b[] = in.readArray(n), c[] = in.readArray(m); ArrayList<Integer> need[] = new ArrayList[n+1]; for(int i=0;i<n;i++) need[i+1] = new ArrayList<>(); for(int i=0;i<n;i++){ if(a[i]!=b[i]) need[b[i]].add(i); } StringBuilder sb = new StringBuilder(); int p = -1; for(int i=0;i<n;i++){ if(b[i]==c[m-1]){ if(p==-1||a[i]!=b[i]) p = i; } } if(p==-1){ out.println("NO"); continue; } for(int i=0;i<m;i++){ if(need[c[i]].size()>0){ sb.append((need[c[i]].get(0)+1)+" "); need[c[i]].remove(0); } else sb.append((p+1)+" "); } boolean valid = true; for(int i=0;i<n;i++) if(need[i+1].size()>0) valid = false; if(!valid) out.println("NO"); else out.println("YES\n"+sb); } out.flush(); } 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) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } 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; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
dc1bdf599b0a5e36e563e067a21bc80c
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static class Node{ int i; int j; Node(int x, int y){ i = x; j = y; } } public static void main(String arg[]){ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t > 0) { int i, j, n = scan.nextInt(), k = scan.nextInt(); int [] arr1 = new int[n]; int [] arr2 = new int[n]; int [] arr3 = new int[k]; int [] arr4 = new int[n + 1]; int [] arr6 = new int[n + 1]; int [] arr8 = new int[k]; ArrayList<Integer> [] arr5 = new ArrayList[n + 1]; ArrayList <Integer> arr; for (i = 0; i < n; ++i) { arr1[i] = scan.nextInt(); arr5[i + 1] = new ArrayList<Integer>(); } for (i = 0; i < n; ++i) { arr2[i] = scan.nextInt(); if (arr1[i] != arr2[i]) { arr4[arr2[i]]++; arr5[arr2[i]].add(i); } else { arr6[arr1[i]] = i + 1; } } for (i = 0; i < k; ++i) { arr3[i] = scan.nextInt(); arr4[arr3[i]]--; } for (i = 1; i <= n; ++i) { if (arr4[i] > 0) { break; } } if (i < n + 1) { System.out.println("NO"); } else { for (i = 0; i < k; ++i) { if (arr5[arr3[i]].size() == 0) { if (arr6[arr3[i]] > 0) { arr8[i] = arr6[arr3[i]]; } } else { arr = arr5[arr3[i]]; arr8[i] = arr.get(arr.size() - 1) + 1; arr.remove(arr.size() - 1); arr6[arr3[i]] = arr8[i]; } } j = -1; for (i = k - 1; i >= 0; --i) { if (arr8[i] == 0) { if (j == -1) { break; } arr8[i] = j; } j = arr8[i]; } if (i >= 0) { System.out.println("NO"); } else { System.out.println("YES"); for (i = 0; i < k; ++i) { System.out.print(arr8[i] + " "); } System.out.println(""); } } t--; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
b5e57eefdb788295ad4dc66a2c80eb78
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 Fence_Painting_699 implements Runnable { 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[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; ++i) arr[i] = nextInt(); return arr; } List<Integer> input( int size) { List<Integer> al=new ArrayList<>(); for(int i=0; i<size; i++) { al.add(nextInt()); } return al; } long[] nextLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; ++i) arr[i] = nextLong(); return arr; } double[] nextDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; ++i) arr[i] = nextDouble(); return arr; } } static int mod = (int) (1e9 + 7); static final PrintWriter out=new PrintWriter(System.out, true); static final FastReader fr = new FastReader(); static int N=300010; static List<Integer> g[]=new List[N]; int [] a=new int[N],b=new int[N],c=new int[N]; public static void main(String[] args) throws java.lang.Exception { new Thread(null, new Fence_Painting_699(), "Main", 1 << 26).start(); } void solve() { int n=fr.nextInt(),m=fr.nextInt(); for(int i=1; i<=n; i++) g[i]=new ArrayList<>(); for(int i=0; i<n; i++) a[i]=fr.nextInt(); for(int i=0; i<n; i++) { b[i]=fr.nextInt(); if(b[i]!=a[i]) { g[b[i]].add(i); } } for(int i=0; i<m; i++) c[i]=fr.nextInt(); int last=-1; if((int)g[c[m-1]].size()>0) { last=g[c[m-1]].get(0); g[c[m-1]].remove(0); }else { for(int i=0; i<n; i++) { if(b[i]==c[m-1]) { last=i; break; } } } if(last==-1) { out.print("NO"); return; } int ans[]=new int[m]; ans[m-1]=last; for(int i=0; i<m-1; i++) { if((int)g[c[i]].size()>0) { ans[i]=g[c[i]].get(0); g[c[i]].remove(0); }else { ans[i]=last; } } for(int i=0; i<n; i++ ) { if((int)g[b[i]].size()>0) { out.print("NO"); return; } } out.print("YES\n"); for(int i=0; i<m; i++) { out.print((ans[i]+1)+" "); } } @Override public void run() { long start = System.nanoTime(); // Program Start boolean testcase = true; int t = testcase ? fr.nextInt() : 1; while (t-- > 0) { solve(); out.print("\n"); out.flush(); } long end = System.nanoTime(); // Program End System.err.println("Time taken: " + (end - start) / 1000000 + " ms"); } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || 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; } static long binary_Expo(long a, long b) { // calculating a^b long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res; } static int i_gcd(int a, int b) {// iterative way to calculate gcd. while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) {// here b is the remainder if (b == 0) return a; //because each time b will divide a. return gcd(b, a % b); } static long ceil_div(long a, long b) { // a numerator b denominator return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static int upper_Bound(int a[], int x) {//closest to the left+1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(int a[], int x) {//closest to the right int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void sort(int a[]) { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
3d9028f9d88acfafe56ab3623f00d9bb
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 Fence_Painting_699 implements Runnable { 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[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; ++i) arr[i] = nextInt(); return arr; } List<Integer> input( int size) { List<Integer> al=new ArrayList<>(); for(int i=0; i<size; i++) { al.add(nextInt()); } return al; } long[] nextLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; ++i) arr[i] = nextLong(); return arr; } double[] nextDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; ++i) arr[i] = nextDouble(); return arr; } } static int mod = (int) (1e9 + 7); static final PrintWriter out=new PrintWriter(System.out, true); static final FastReader fr = new FastReader(); static int N=300010; static List<Integer> g[]=new List[N]; int [] a=new int[N],b=new int[N],c=new int[N]; public static void main(String[] args) throws java.lang.Exception { new Thread(null, new Fence_Painting_699(), "Main", 1 << 26).start(); } void solve() { int n=fr.nextInt(),m=fr.nextInt(); for(int i=1; i<=n; i++) g[i]=new ArrayList<>(); for(int i=0; i<n; i++) a[i]=fr.nextInt(); for(int i=0; i<n; i++) { b[i]=fr.nextInt(); if(b[i]!=a[i]) { g[b[i]].add(i); } } for(int i=0; i<m; i++) c[i]=fr.nextInt(); int last=-1; if((int)g[c[m-1]].size()>0) { last=g[c[m-1]].get(g[c[m-1]].size()-1);//g[c[m-1]].back() g[c[m-1]].remove(g[c[m-1]].size()-1);//g[c[m-1]].pop_back() }else { for(int i=0; i<n; i++) { if(b[i]==c[m-1]) { last=i; break; } } } if(last==-1) { out.print("NO"); return; } int ans[]=new int[m]; ans[m-1]=last; for(int i=0; i<m-1; i++) { if((int)g[c[i]].size()>0) { ans[i]=g[c[i]].get(g[c[i]].size()-1); g[c[i]].remove(g[c[i]].size()-1); }else { ans[i]=last; } } for(int i=0; i<n; i++ ) { if((int)g[b[i]].size()>0) { out.print("NO"); return; } } out.print("YES\n"); for(int i=0; i<m; i++) { out.print((ans[i]+1)+" "); } } @Override public void run() { long start = System.nanoTime(); // Program Start boolean testcase = true; int t = testcase ? fr.nextInt() : 1; while (t-- > 0) { solve(); out.print("\n"); out.flush(); } long end = System.nanoTime(); // Program End System.err.println("Time taken: " + (end - start) / 1000000 + " ms"); } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || 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; } static long binary_Expo(long a, long b) { // calculating a^b long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res; } static int i_gcd(int a, int b) {// iterative way to calculate gcd. while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) {// here b is the remainder if (b == 0) return a; //because each time b will divide a. return gcd(b, a % b); } static long ceil_div(long a, long b) { // a numerator b denominator return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static int upper_Bound(int a[], int x) {//closest to the left+1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(int a[], int x) {//closest to the right int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void sort(int a[]) { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
08ddb234eabecc850d935d107ccc403f
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); l: while(t-->0) { StringBuilder sb=new StringBuilder(); int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m]; LinkedList<Integer> d[]=new LinkedList[n+1]; d[n]=new LinkedList<Integer>(); for(int i=0;i<n;i++) { d[i]=new LinkedList<Integer>(); a[i]=sc.nextInt(); } for(int i=0;i<n;i++) {//d:b中的所有颜色和对应的位置 if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i); } for(int i=0;i<n;i++) { if(b[i]!=a[i]) d[b[i]].add(i); } for(int i=0;i<m;i++) { c[i]=sc.nextInt(); } if(d[c[m-1]].isEmpty()) {//判断最后一个是否可以画 System.out.println("NO"); continue; } for(int i=m-1;i>-1;i--) if(d[c[i]].isEmpty()) ans[i]=ans[i+1]; else ans[i]=d[c[i]].pollLast(); for(int i=0;i<m;i++) { a[ans[i]]=c[i]; sb.append(ans[i]+1+" "); } for(int i=0;i<n;i++) if(a[i]!=b[i]) { System.out.println("NO"); continue l; } System.out.println("YES\n"+sb); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
59d7d9d34f723f113000aff0d829535d
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.*; import java.io.*; import java.util.*; public class Main{ static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { Scanner sc=new Scanner(System.in); l: for(int t=sc.nextInt();t-->0;) { StringBuilder sb=new StringBuilder(); int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n],b[]=new int[n],c[]=new int[m],ans[]=new int[m]; LinkedList<Integer> d[]=new LinkedList[n+1]; d[n]=new LinkedList<Integer>(); for(int i=0;i<n;i++) { d[i]=new LinkedList<Integer>(); a[i]=sc.nextInt(); } for(int i=0;i<n;i++) if((b[i]=sc.nextInt())==a[i]) d[b[i]].add(i); for(int i=0;i<n;i++) if(b[i]!=a[i]) d[b[i]].add(i); for(int i=0;i<m;i++) c[i]=sc.nextInt(); if(d[c[m-1]].isEmpty()) {System.out.println("NO");continue;} for(int i=m-1;i>-1;i--) if(d[c[i]].isEmpty()) ans[i]=ans[i+1]; else ans[i]=d[c[i]].pollLast(); for(int i=0;i<m;i++) { a[ans[i]]=c[i]; sb.append(ans[i]+1+" "); } for(int i=0;i<n;i++) if(a[i]!=b[i]) {System.out.println("NO"); continue l;} System.out.println("YES\n"+sb); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
f561818a7b2422ddbd24ad42d05751a5
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
// 06-Feb-2021 import java.util.*; import java.io.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; private FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayOne(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; i++) a[i] = nextInt(); return a; } 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(); StringBuilder str = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(), m= s.nextInt(); int mxreq[] = new int[n + 1]; int req[] = new int[n + 1]; int avail[] = new int[n + 1]; int last[] = new int[n + 1]; int a[] = new int[n + 1]; int b[] = new int[n + 1]; int c[] = new int[m]; ArrayList<Integer> ids[] = new ArrayList[n + 1]; for(int i = 0; i < ids.length; i++) { ids[i] = new ArrayList<>(); } for(int i = 1; i <= n; i++) { a[i] = s.nextInt(); } for(int i = 1; i <= n; i++) { b[i] = s.nextInt(); if(b[i] != a[i]) { req[b[i]]++; ids[b[i]].add(i); } mxreq[b[i]]++; last[b[i]] = i; } for(int i = 0; i<m; i++) { c[i] =s.nextInt(); avail[c[i]]++; } boolean f= false; for(int i = 1; i<= n;i++) { if(avail[i] < req[i]) { f = true; break; } } if(mxreq[c[m - 1]] == 0) { f = true; } if(f) { str.append("NO\n"); continue; } str.append("YES\n"); int ind = 0; if(ids[c[m - 1]].isEmpty()) { ind = last[c[m - 1]]; }else { ind = ids[c[m - 1]].get(0); } for(int i = 0; i< m; i++) { if(ids[c[i]].isEmpty()) { str.append(ind +" "); }else { str.append(ids[c[i]].remove(ids[c[i]].size() - 1) +" "); } } str.append("\n"); } System.out.println(str); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
23fac8ee01b6aa858af0956fe0249ff6
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.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static int D1[],D2[],par[]; static boolean set[],G[][]; public static void main(String args[])throws IOException { int T=i(); outer : while(T-->0) { int N=i(),M=i(); int A[]=input(N); int B[]=new int[N]; ArrayList<paint> diff=new ArrayList<paint>(); int fb[]=new int[N+1]; int fdiff[]=new int[N+1]; int fcopy[]=new int[N+1]; int fC[]=new int[N+1]; for(int i=0; i<N; i++) { B[i]=i(); fb[B[i]]++; if(A[i]!=B[i]) { diff.add(new paint(B[i],i+1)); fdiff[B[i]]++; fcopy[B[i]]++; } } Collections.sort(diff); //for(paint x:diff)System.out.println(x.color+" "+x.index); int C[]=new int[M]; for(int i=0; i<M; i++) { C[i]=i(); fC[C[i]]++; } int last=-1; for(paint x:diff) //Every Col of Diff in B { if(fC[x.color]==0) { System.out.println("NO"); continue outer; } fC[x.color]--; } if(fb[C[M-1]]==0) // last is not present in B { System.out.println("NO"); continue outer; } if(fdiff[C[M-1]]==0) { for(int i=0; i<N; i++) { if(B[i]==C[M-1]) { last=i+1; break; } } } else { int l=0,r=diff.size()-1,m; while(l<=r) { m=(l+r)/2; int col=diff.get(m).color; if(col==C[M-1]) { //ans.append(diff.get(m).index+" "); last=diff.get(m).index; diff.remove(m); fdiff[col]--; break; } else if(col<C[M-1]) { l=m+1; } else { r=m-1; } } } for(int i=0; i<M-1; i++) { if(fdiff[C[i]]==0) { ans.append(last+" "); } else { int l=0,r=diff.size()-1,m; while(l<=r) { m=(l+r)/2; int col=diff.get(m).color; if(col==C[i]) { ans.append(diff.get(m).index+" "); diff.remove(m); fdiff[col]--; break; } else if(col<C[i]) { l=m+1; } else { r=m-1; } } } } ans.append(last); System.out.println("YES"); System.out.println(ans); ans.delete(0, ans.length()); } } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; //transfers the size par[b]=a; //changes the parent } } static ArrayList<Integer> IND(int B[],HashMap<Integer,Integer> mp) { ArrayList<Integer> A=new ArrayList<>(); for(int i:B) { if(mp.containsKey(i)) { A.add(i); int f=mp.get(i)-1; if(f==0) mp.remove(i); else mp.put(i, f); } } return A; } static HashMap<Integer,Integer> hash(int A[],int index) { HashMap<Integer,Integer> mp=new HashMap<Integer,Integer>(); for(int i=index; i<A.length; i++) { int f=mp.getOrDefault(A[i], 0)+1; mp.put(A[i], f); } return mp; } static void swap(char A[],int a,int b) { char ch=A[a]; A[a]=A[b]; A[b]=ch; } static long lower_Bound(long A[],int low,int high, long x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } 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 setGraph(int N) { //set=new boolean[N+1]; //size=new int[N+1]; //par=new int[N+1]; g=new ArrayList<ArrayList<Integer>>(); //tg=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) { g.add(new ArrayList<Integer>()); //tg.add(new ArrayList<Integer>()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } 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 boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } //Debugging Functions Starts static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } //IO FUNCTIONS END } class paint implements Comparable<paint> { int color,index; paint(int c,int i) { color=c; index=i; } public int compareTo(paint X) { return this.color-X.color; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
57f1ab46afd5067b46dca7f3c6d6b694
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 cf699 { public static void main(String[] args) throws Exception{ InputStreamReader ip=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ip); long t=Integer.parseInt(br.readLine()); while(t-->0){ // String[] arrp = (br.readLine()).trim().split(" "); String[] arrp = (br.readLine()).trim().split(" "); int n=Integer.parseInt(arrp[0]); int m=Integer.parseInt(arrp[1]); String[] str=(br.readLine()).trim().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(str[i]); } str=(br.readLine()).trim().split(" "); int[] b = new int[n]; ArrayList<Integer>[] g=new ArrayList [n+1]; for (int i = 0; i <= n; i++) { g[i]=new ArrayList<>();} for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(str[i]); if(a[i]!=b[i]){ (g[b[i]]).add(i); } } str=(br.readLine()).trim().split(" "); int[] c = new int[m]; for (int i = 0; i < m; i++) { c[i] = Integer.parseInt(str[i]); } int last=-1; if((g[c[m-1]]).size()>0){ ArrayList<Integer> list=(g[c[m-1]]); last=list.get(list.size()-1); list.remove(list.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"); continue;} int[] ans=new int[m]; ans[m-1]=last; for(int i=0;i<(m-1);i++){ if((g[c[i]]).size()>0){ ArrayList<Integer> list=(g[c[i]]); ans[i]=list.get(list.size()-1); list.remove(list.size()-1); }else{ ans[i]=last; } } boolean flag=false; for(int i=1;i<=n;i++){ if((g[i]).size()>0){ flag=true; break; } } if(flag){System.out.println("NO"); continue;} System.out.println("YES"); for(int i=0;i<m;i++){ System.out.print((ans[i]+1)+" "); } System.out.println(); } } } // public static void main(String[] args) throws Exception{ // InputStreamReader ip=new InputStreamReader(System.in); // BufferedReader br=new BufferedReader(ip); // long t=Integer.parseInt(br.readLine()); // while(t-->0){ // // String[] arrp = (br.readLine()).trim().split(" "); // String[] arrp = (br.readLine()).trim().split(" "); // int n=Integer.parseInt(arrp[0]); // int m=Integer.parseInt(arrp[1]); // String[] str=(br.readLine()).trim().split(" "); // int[] a = new int[n]; // boolean flag=false; // for (int i = 0; i < n; i++) { // a[i] = Integer.parseInt(str[i]); // } // str=(br.readLine()).trim().split(" "); // int[] b = new int[n]; // int ct=0; // HashMap<Integer,ArrayList<Integer>> hm1=new HashMap<>(); // int prev=-1; // HashMap<Integer,ArrayList<Integer>> hm3=new HashMap<>(); // for (int i = 0; i < n; i++) { // b[i] = Integer.parseInt(str[i]); // if(a[i]!=b[i]){ // // prev=i+1; // ct++; // ArrayList<Integer> list=hm1.getOrDefault(b[i],new ArrayList<>()); // list.add(i); // hm1.put(b[i],list); // } // ArrayList<Integer> list3=hm3.getOrDefault(b[i],new ArrayList<>()); // list3.add(i); // hm3.put(b[i],list3); // } // str=(br.readLine()).trim().split(" "); // int[] c = new int[m]; // HashMap<Integer,Integer> hm2=new HashMap<>(); // for (int i = 0; i < m; i++) { // c[i] = Integer.parseInt(str[i]); // hm2.put(c[i],hm2.getOrDefault(c[i],0)+1); // // if((hm1.getOrDefault(c[i],0)).size()>1){prev=i+1;} // } // for(int key:hm1.keySet()){ // if(hm2.getOrDefault(key,0)<((hm1.get(key)).size())){flag=true; break;} // } // if(ct>m || flag){System.out.println("NO"); continue;} // prev=-1; // // // for(int i=m-1;i>=0;i--){ // // // // if(list.size()>0){prev=list.get(list.size()-1)+1; break;} // // // } // ArrayList<Integer> list3=hm3.getOrDefault(c[m-1],new ArrayList<>()); // if(list3.size()>0){ // prev = list3.get(list3.size()-1)+1;//g[c[m - 1]].back(); // list3.remove(list3.size()-1); // } // else{ // for(int i = 0 ;i < n;i++){ // if(b[i] == c[m - 1]){ // prev = i+1; // break; // } // } // } // if(prev==-1){System.out.println("NO"); continue;} // // int[] pt=new int[m]; // int[]ans=new int[m]; // ans[m-1]=prev; // for(int i=0;i<m;i++){ // ArrayList<Integer> list=hm1.getOrDefault(c[i],new ArrayList<>()); // if(list.size()>0){ // ans[i]=list.get(list.size()-1)+1; // list.remove(list.size()-1); // }else{ // ans[i]=prev; // } // } // flag=false; // for(int key:hm3.keySet()){ // if(((hm3.get(key)).size())>0){System.out.println("NO"); flag=true; break;} // } // if(flag){continue;} // System.out.println("YES"); // for(int i=0;i<m;i++){ // System.out.print(ans[i]+" "); // } // // for(int i=0;i<m;i++){ // // if(ans[i]>0) System.out.print(ans[i]+" "); // // else{System.out.print(prev+" ");} // // } // System.out.println(); // if(ct==0){ // System.out.println("YES"); // for(int i=0;i<m;i++){ // System.out.print(prev+" "); // } // System.out.println(); // }else{ // // int[] pt=new int[m]; // System.out.println("YES"); // int[]ans=new int[m]; // ans[m-1]=prev; // for(int i=0;i<m;i++){ // ArrayList<Integer> list=hm1.getOrDefault(c[i],new ArrayList<>()); // if(list.size()>0){ // System.out.print(((list.get(list.size()-1))+1)+" "); // // ans[i]=((list.get(list.size()-1))+1); // // prev=Math.max(ans[i],prev); // // prev=(list.get(list.size()-1))+1; // list.remove(list.size()-1); // }else{ // System.out.print(prev+" "); // } // } // // for(int i=0;i<m;i++){ // // if(ans[i]>0) System.out.print(ans[i]+" "); // // else{System.out.print(prev+" ");} // // } // System.out.println(); // } // } // } // } // public static void main(String[] args) throws Exception{ // InputStreamReader ip=new InputStreamReader(System.in); // BufferedReader br=new BufferedReader(ip); // long t=Integer.parseInt(br.readLine()); // while(t-->0){ // // String[] arrp = (br.readLine()).trim().split(" "); // String[] arrp = (br.readLine()).trim().split(" "); // int n=Integer.parseInt(arrp[0]); // int k=Integer.parseInt(arrp[1]); // String[] str=(br.readLine()).trim().split(" "); // int[] arr = new int[n]; // boolean flag=true; // for (int i = 0; i < n; i++) { // arr[i] = Integer.parseInt(str[i]); // if(i>0 && arr[i-1]<arr[i]){flag=false;} // } // if(flag){System.out.println("-1"); continue;} // int ans=0; // for(int i=0;i<k;i++){ // flag=true; // for(int j=0;j<(n-1);j++){ // if(arr[j]<arr[j+1]){arr[j]++; ans=j+1; flag=false; break;} // } // if(flag){break;} // } // if(flag){System.out.println("-1");} // else{System.out.println(ans);} // } // } // } // static int[][] arr={{0,1},{0,-1},{1,0},{-1,0}}; //u,d,r,l // public static void main(String[] args) throws Exception{ // InputStreamReader ip=new InputStreamReader(System.in); // BufferedReader br=new BufferedReader(ip); // long t=Integer.parseInt(br.readLine()); // while(t-->0){ // // String[] arrp = (br.readLine()).trim().split(" "); // // int px=Integer.parseInt(arrp[0]); // // int py=Integer.parseInt(arrp[1]); // String str=(br.readLine()); // // int[] arr = new int[n]; // int cx=0,cy=0; // for(int i=0;i<str.length();i++){ // if(str.charAt(i)=='R' && px>0 && cx<px){ // cx++; // }else if(str.charAt(i)=='L' && px<0 && cx>px){ // cx--; // }else if(str.charAt(i)=='U' && py>0 && cy<py){ // cy++; // }else if(str.charAt(i)=='D' && py<0 && cy>py){ // cy--; // } // if(cx==px && cy==py){break;} // } // if(cx==px && cy==py){System.out.println("YES");} // else{System.out.println("NO");} // } // } // }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
3a22efc5049168ac029bf8a77f70aa39
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Inet4Address; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /* @author kalZor */ public class TaskC { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class Solver { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(),m = in.nextInt(); int[] a = in.nextArray(n); int[] b = in.nextArray(n); int[] c = in.nextArray(m); ArrayList<PriorityQueue<Integer>> adj = new ArrayList<>(); int curr = -1; for(int i=0;i<n;i++) { adj.add(new PriorityQueue<>()); if(c[m-1]==b[i]) curr = i; } int[] ans = new int[m]; int[] diff = new int[n]; int cnt = 0; for(int i=0;i<n;i++){ if(a[i]!=b[i]){ diff[b[i]-1]++; adj.get(b[i]-1).add(i); cnt++; } } for(int i=0;i<m;i++){ if(diff[c[i]-1]>0){ diff[c[i]-1]--; ans[i] = adj.get(c[i]-1).poll()+1; cnt--; if(i==m-1) curr = ans[i]-1; } } if(cnt>0||(curr==-1)) { out.println("NO"); return; } out.println("YES"); // out.println(Arrays.toString(c)); for(int i=0;i<m;i++){ if(ans[i]==0) ans[i]=curr+1; out.print(ans[i]+" "); } out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream stream){ br = new BufferedReader(new InputStreamReader(stream)); } 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()); } public String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } public int[] nextArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
f425a9cc68ff9d9b0f40adce6e8d32f5
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.Scanner; public class C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(" "); int cases = scan.nextInt(); while(cases-->0) { int n = scan.nextInt(); int m = scan.nextInt(); int[] a = new int[n+1]; int[] b = new int[n+1]; int[] c = new int[m+1]; ArrayList<Integer> arr[] = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { arr[i] = new ArrayList<>(); } int[] ans = new int[m+1]; for (int i = 1; i <= m; i++) { ans[i] = 0; } for (int i = 1; i <= n; i++) { a[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { b[i] = scan.nextInt(); } for (int i = 1; i <= m; i++) { c[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { if(b[i] != a[i]) { arr[b[i]].add(i); } } int cm = c[m]; int last = -1; int i; if(arr[cm].size()==0) { for (i = 1; i <= n; i++) { if(b[i] == cm) { last = i; break; } } }else { last = arr[cm].get(0); arr[cm].remove(0); } if(last == -1) { System.out.println("NO"); continue; } ans[m] = last; for (int j = 1; j <= m - 1; j++) { if(arr[c[j]].size() > 0) { ans[j] = arr[c[j]].get(0); arr[c[j]].remove(0); }else { ans[j] = last; } } boolean flag = false; for (int j = 1; j <= n; j++) { if(arr[j].size() != 0 ) { flag = true; break; } } if(flag) { System.out.println("NO"); continue; } System.out.println("YES"); for (int j = 1; j <= m; j++) { System.out.print(ans[j]+" "); } System.out.println(); } scan.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
3eb3fce887476ce8cc5be8263d7268b7
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.Scanner; public class D { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(" "); int cases = scan.nextInt(); while(cases-->0) { int n = scan.nextInt(); int m = scan.nextInt(); int[] a = new int[n+1]; int[] b = new int[n+1]; int[] c = new int[m+1]; ArrayList<Integer> arr[] = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { arr[i] = new ArrayList<>(); } int[] ans = new int[m+1]; for (int i = 1; i <= m; i++) { ans[i] = 0; } for (int i = 1; i <= n; i++) { a[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { b[i] = scan.nextInt(); } for (int i = 1; i <= m; i++) { c[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { if(b[i] != a[i]) { arr[b[i]].add(i); } } int cm = c[m]; int last = -1; int i; if(arr[cm].size()==0) { for (i = 1; i <= n; i++) { if(b[i] == cm) { last = i; } } }else { last = arr[cm].get(arr[cm].size()-1); arr[cm].remove(arr[cm].size()-1); } if(last == -1) { System.out.println("NO"); continue; } ans[m] = last; for (int j = 1; j <= m - 1; j++) { if(arr[c[j]].size() > 0) { ans[j] = arr[c[j]].get(0); arr[c[j]].remove(0); }else { ans[j] = last; } } boolean flag = false; for (int j = 1; j <= n; j++) { if(arr[j].size() != 0 ) { flag = true; break; } } if(flag) { System.out.println("NO"); continue; } System.out.println("YES"); for (int j = 1; j <= m; j++) { System.out.print(ans[j]+" "); } System.out.println(); } scan.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
c60a69ebed3bb96619a381b81435a062
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 C { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(), m = fs.nextInt(); int a[] = new int[n]; int b[] = new int[n]; int c[] = new int[m]; List<Integer> org[] = new ArrayList[n + 1]; List<Integer> changes[] = new ArrayList[n + 1]; for (int i = 0; i < n + 1; i++) { org[i] = new ArrayList<>(); changes[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { a[i] = fs.nextInt(); } for (int i = 0; i < n; i++) { b[i] = fs.nextInt(); org[b[i]].add(i); if (a[i] != b[i]) { changes[b[i]].add(i); } } for (int i = 0; i < m; i++) { c[i] = fs.nextInt(); } if (org[c[m - 1]].isEmpty()) { System.out.println("NO"); continue outer; } boolean exist = true; int[] ans = new int[m]; for (int i = m - 1; i >= 0; i--) { if (!changes[c[i]].isEmpty()) { int size = changes[c[i]].size(); ans[i] = changes[c[i]].remove(size - 1) + 1; } else if (!org[c[i]].isEmpty()) { int size = org[c[i]].size(); ans[i] = org[c[i]].get(size - 1) + 1; } else { ans[i] = ans[m - 1]; } } for (int i = 0; i <= n; i++) { if (!changes[i].isEmpty()) { System.out.println("No"); continue outer; } } System.out.println("Yes"); for (int i = 0; i < m; i++) { System.out.print(ans[i] + " "); } System.out.println(); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
646e0f4cbada6f073100c9387d49e0c0
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.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int N = sc.nextInt(), M = sc.nextInt(); int original[] = new int[N]; int desired[] = new int[N]; int painters[] = new int[M]; Set<Integer> set = new HashSet(); Map<Integer, List<Integer>> pos = new HashMap(); for(int i = 0; i<N; i++)original[i] = sc.nextInt(); int position = Integer.MIN_VALUE; for(int i = 0; i<N; i++){ desired[i] = sc.nextInt(); set.add(desired[i]); } for(int i = 0; i<M; i++)painters[i] = sc.nextInt(); int cnt = 0; for(int i = 0; i<N; i++){ if(desired[i] == painters[M - 1])position = Math.max(position, i + 1); if(original[i] != desired[i]){ if(!pos.containsKey(desired[i])){ pos.put(desired[i], new ArrayList()); pos.get(desired[i]).add(i + 1); } else pos.get(desired[i]).add(i + 1); cnt++; } } if(cnt > M)out.println("NO"); else{ int arr[] = new int[M]; if(pos.containsKey(painters[M - 1])){ int size = pos.get(painters[M - 1]).size(); position = pos.get(painters[M - 1]).get(size - 1); } for(int i = 0; i<M; i++){ if(pos.containsKey(painters[i])){ arr[i] = pos.get(painters[i]).get(0); pos.get(painters[i]).remove(0); if(pos.get(painters[i]).size() == 0)pos.remove(painters[i]); } } if(pos.size() != 0)out.println("NO"); else if(cnt < M && !set.contains(painters[M - 1]))out.println("NO"); else{ out.println("YES"); for(int a: arr){ if(a == 0)out.print(position + " "); else out.print(a + " "); } out.println(); } } } out.flush(); } 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
4af22420ff9a21fb74c429991a15da00
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.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.StringTokenizer; public class CF1481_D2_C { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { solve(scanner); } } private static void solve(FastScanner scanner) { int n = scanner.nextInt(); int m = scanner.nextInt(); int[] init = scanner.nextArray(n); int[] fin = scanner.nextArray(n); int[] paint = scanner.nextArray(m); HashMap<Integer, Integer> initMap = new HashMap<>(); HashMap<Integer, LinkedList<Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (init[i] != fin[i]) { LinkedList<Integer> list = map.getOrDefault(fin[i], new LinkedList<>()); list.add(i + 1); map.put(fin[i], list); } } for (int i = 0; i < n; i++) { if (init[i] == fin[i]) { initMap.put(init[i], i + 1); } } // if(map.isEmpty()){ // System.out.println("YES"); // // // } int[] p = new int[m]; int last = -1; for (int i = m - 1; i >= 0; i--) { LinkedList<Integer> list = map.getOrDefault(paint[i], new LinkedList<>()); if (list.isEmpty() && last == -1) { if (initMap.containsKey(paint[i])) { p[i] = initMap.get(paint[i]); last = p[i]; } else { System.out.println("NO"); return; } } else if (list.isEmpty()) { p[i] = last; } else { if (last == -1) last = list.peek(); p[i] = list.poll(); } } for (Map.Entry<Integer, LinkedList<Integer>> e : map.entrySet()) { if (!e.getValue().isEmpty()) { System.out.println("NO"); return; } } System.out.println("YES"); printArrayInLine(p); } static HashMap<Integer, Integer> arrToMap(int[] arr, int n) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } return map; } public static void printArrayInLine(int[] arr) { StringBuilder builder = new StringBuilder(); for (int value : arr) { builder.append(value).append(" "); } System.out.println(builder); } 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } Integer[] nextArray(int n, boolean object) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextArrayLong(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } Long[] nextArrayLong(int n, boolean object) { Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
02c6cf1804c63efa062caa6bea72934d
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
/* "Why do we fall? So we can learn to pick ourselves up." */ import java.util.*; import java.io.*; import java.math.*; public class A { static FastReader sc=new FastReader(); public static void main(String[] args) { StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int m=i(); int A[]=input(n); int B[]=input(n); int C[]=input(m); HashMap<Integer,Integer> map=hash(C); HashMap<Integer,Integer> map1=new HashMap<Integer, Integer>(); for(int i=0;i<n;i++) { if(A[i]!=B[i] && !map1.containsKey(B[i])) { map1.put(B[i],1); } else if(A[i]!=B[i] && map1.containsKey(B[i])) { map1.put(B[i],map1.get(B[i])+1); } } if(map1.size()==0) { int f=-1; int k=0; HashMap<Integer,Integer> mm=hash(A); if(!mm.containsKey(C[m-1])) { System.out.println("NO"); continue outer; } System.out.println("YES"); int ind=-1; for(int i=0;i<n;i++) { if(A[i]==C[m-1]) { ind=i+1; break; } } for(int i=0;i<m;i++) { System.out.print(ind+" "); } System.out.println(); continue outer; } HashMap<Integer,Integer> mmm=hash(B); if(!mmm.containsKey(C[m-1])) { System.out.println("NO"); continue outer; } for(int key : map1.keySet()) { int v=map1.get(key); if(!map.containsKey(key) || map.get(key)<v) { System.out.println("NO"); continue outer; } } int in=-1; for(int i=n-1;i>=0;i--) { if(B[i]==C[m-1] && A[i]!=B[i]) { in=i+1; break; } } if(in==-1) { for(int i=n-1;i>=0;i--) { if(B[i]==C[m-1]) { in=i+1; break; } } } TreeSet<Integer> D[]=new TreeSet[n+1]; for(int i=0;i<=n;i++) { D[i]=new TreeSet<Integer>(); } for(int i=0;i<n;i++) { if(i==(in-1) || A[i]==B[i]) continue; int y=B[i]; D[y].add(i+1); } System.out.println("YES"); for(int i=0;i<m-1;i++) { if(!map1.containsKey(C[i])) { System.out.print(in+" "); } else { if(D[C[i]].size()==0) { System.out.print(in+" "); continue; } int y=D[C[i]].first(); D[C[i]].remove(y); System.out.print(y+" "); } } if(D[C[m-1]].size()==0) { System.out.print(in+" "); } else { int y=D[C[m-1]].first(); D[C[m-1]].remove(y); System.out.print(y+" "); } System.out.println(); } //System.out.println(sb.toString()); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static int[][] input(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]=i(); } } return A; } static long mod(long x) { int mod=1000000007; return ((x%mod + mod)%mod); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
ad922e29f0160c1b252c62b6dd6eb140
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.util.concurrent.PriorityBlockingQueue; public class Main { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; static long fact[] = new long[1000001]; static long inverse[] = new long[1000001]; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // fact[0] = 1; // inverse[0] = 1; // for(int i = 1;i<fact.length;i++){ // fact[i] = (fact[i-1] * i)%mod; // // inverse[i] = binaryExpo(fact[i], mod-2); // } int t = nextInt(); while(t-->0){ solve(); } } public static void solve() throws IOException{ int n = nextInt(); int m = nextInt(); int[]arr = new int[n]; for(int i =0 ;i<n;i++){ arr[i] = nextInt(); } int[]indices = new int[n+1]; Arrays.fill(indices,-1); int[]brr = new int[n]; Map<Integer,List<Integer>>map = new HashMap<>(); for(int i = 0;i<n;i++){ brr[i] = nextInt(); indices[brr[i]] = i; } int[]c = new int[m]; for(int i = 0;i<m;i++){ c[i] = nextInt(); } if(indices[c[m-1]] == -1){ println("NO"); return; } int index = indices[c[m-1]]; for(int i = 0;i<n;i++){ if(brr[i] == c[m-1] && arr[i] != brr[i]){ index = i; break; } } for(int i = 0;i<n;i++){ if(i != index && arr[i] != brr[i]){ if(!map.containsKey(brr[i])){ List<Integer>list = new ArrayList<>(); list.add(i); map.put(brr[i],list); } else{ map.get(brr[i]).add(i); } } } List<Integer>ans = new ArrayList<>(); for(int i =0;i<m;i++){ if(map.containsKey(c[i])){ List<Integer>list = map.get(c[i]); int size = list.size(); ans.add(list.get(size-1) + 1); arr[list.get(size-1)] = c[i]; list.remove(size-1); if(list.size() == 0)map.remove(c[i]); } else{ arr[index] = c[i]; ans.add(index+1); } } // System.out.println(Arrays.toString(arr)); // System.out.println(Arrays.toString(brr)); for(int i = 0;i<n;i++){ if(arr[i] != brr[i]){ println("NO"); return; } } out.println("YES"); for(int i = 0;i<m;i++){ out.print(ans.get(i) + " "); } out.println(); out.flush(); } public static int[] bringSame(int u,int v ,int parent[][],int[]depth){ if(depth[u] < depth[v]){ int temp = u; u = v; v = temp; } int k = depth[u] - depth[v]; for(int i = 0;i<=18;i++){ if((k & (1<<i)) != 0){ u = parent[u][i]; } } return new int[]{u,v}; } public static void findDepth(List<List<Integer>>list,int cur,int parent,int[]depth,int[][]p){ List<Integer>temp = list.get(cur); p[cur][0] = parent; for(int i = 0;i<temp.size();i++){ int next = temp.get(i); if(next != parent){ depth[next] = depth[cur]+1; findDepth(list,next,cur,depth,p); } } } public static int lca(int u, int v,int[][]parent){ if(u == v)return u; for(int i = 18;i>=0;i--){ if(parent[u][i] != parent[v][i]){ u = parent[u][i]; v = parent[v][i]; } } return parent[u][0]; } public static void plus(int node,int low,int high,int tlow,int thigh,int[]tree){ if(low >= tlow && high <= thigh){ tree[node]++; return; } if(high < tlow || low > thigh)return; int mid = (low + high)/2; plus(node*2,low,mid,tlow,thigh,tree); plus(node*2 + 1 , mid + 1, high,tlow, thigh,tree); } public static boolean allEqual(int[]arr,int x){ for(int i = 0;i<arr.length;i++){ if(arr[i] != x)return false; } return true; } public static long helper(StringBuilder sb){ return Long.parseLong(sb.toString()); } public static int min(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow&& high <= thigh)return tree[node][0]; if(high < tlow || low > thigh)return Integer.MAX_VALUE; int mid = (low + high)/2; // println(low+" "+high+" "+tlow+" "+thigh); return Math.min(min(node*2,low,mid,tlow,thigh,tree) ,min(node*2+1,mid+1,high,tlow,thigh,tree)); } public static int max(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow && high <= thigh)return tree[node][1]; if(high < tlow || low > thigh)return Integer.MIN_VALUE; int mid = (low + high)/2; return Math.max(max(node*2,low,mid,tlow,thigh,tree) ,max(node*2+1,mid+1,high,tlow,thigh,tree)); } public static long[] help(List<List<Integer>>list,int[][]range,int cur){ List<Integer>temp = list.get(cur); if(temp.size() == 0)return new long[]{range[cur][1],1}; long sum = 0; long count = 0; for(int i = 0;i<temp.size();i++){ long []arr = help(list,range,temp.get(i)); sum += arr[0]; count += arr[1]; } if(sum < range[cur][0]){ count++; sum = range[cur][1]; } return new long[]{sum,count}; } public static long findSum(int node,int low, int high,int tlow,int thigh,long[]tree,long mod){ if(low >= tlow && high <= thigh)return tree[node]%mod; if(low > thigh || high < tlow)return 0; int mid = (low + high)/2; return((findSum(node*2,low,mid,tlow,thigh,tree,mod) % mod) + (findSum(node*2+1,mid+1,high,tlow,thigh,tree,mod)))%mod; } public static boolean allzero(long[]arr){ for(int i =0 ;i<arr.length;i++){ if(arr[i]!=0)return false; } return true; } public static long count(long[][]dp,int i,int[]arr,int drank,long sum){ if(i == arr.length)return 0; if(dp[i][drank]!=-1 && arr[i]+sum >=0)return dp[i][drank]; if(sum + arr[i] >= 0){ long count1 = 1 + count(dp,i+1,arr,drank+1,sum+arr[i]); long count2 = count(dp,i+1,arr,drank,sum); return dp[i][drank] = Math.max(count1,count2); } return dp[i][drank] = count(dp,i+1,arr,drank,sum); } public static void help(int[]arr,char[]signs,int l,int r){ if( l < r){ int mid = (l+r)/2; help(arr,signs,l,mid); help(arr,signs,mid+1,r); merge(arr,signs,l,mid,r); } } public static void merge(int[]arr,char[]signs,int l,int mid,int r){ int n1 = mid - l + 1; int n2 = r - mid; int[]a = new int[n1]; int []b = new int[n2]; char[]asigns = new char[n1]; char[]bsigns = new char[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[i+l]; asigns[i] = signs[i+l]; } for(int i = 0;i<n2;i++){ b[i] = arr[i+mid+1]; bsigns[i] = signs[i+mid+1]; } int i = 0; int j = 0; int k = l; boolean opp = false; while(i<n1 && j<n2){ if(a[i] <= b[j]){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } else{ arr[k] = b[j]; int times = n1 - i; if(times%2 == 1){ if(bsigns[j] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = bsigns[j]; } j++; opp = !opp; k++; } } while(i<n1){ arr[k] = a[i]; if(opp){ if(asigns[i] == 'R'){ signs[k] = 'L'; } else{ signs[k] = 'R'; } } else{ signs[k] = asigns[i]; } i++; k++; } while(j<n2){ arr[k] = b[j]; signs[k] = bsigns[j]; j++; k++; } } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val; if(expo%2 == 1){ val = (binaryExpo(base, expo-1)*base)%mod; } else{ val = binaryExpo(base, expo/2); val = (val*val)%mod; } return (val%mod); } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b); // give emphasis to the number of occurences of elements it might help. // if a number is even then we can find the pair for each and every number in that range whose bitwise AND is zero. // if a you find a problem related to the graph make a graph // There is atleast one prime number between the interval [n , 3n/2]; // If a problem is related to maths then try to form an mathematical equation.
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
3ebb671f5151c679cd4b00c594486e52
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.Scanner; import java.util.StringTokenizer; import java.util.*; public class codeforcesC{ 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(); int t=sc.nextInt(); while(t-->0){ StringBuilder sb=new StringBuilder(); int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; int req[]=new int[n+1]; int ans[]=new int[n]; Map<Integer,List<Integer>> map=new HashMap<>(); 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(); if(b[i]!=a[i]){req[b[i]]--; if(map.get(b[i])==null){List<Integer> l=new ArrayList<>();map.put(b[i],l);} map.get(b[i]).add(i+1); } } int c[]=new int[m]; for(int i=0;i<m;i++){c[i]=sc.nextInt();req[c[i]]++; } boolean flag=true; for(int w:req){ if(w<0){flag=false;} } int pos=-1; if(flag){ int last=c[m-1]; boolean check=false; for(int i=0;i<n;i++){ if(b[i]==last && a[i]!=b[i]){check=true;pos=i+1;} } if(pos==-1){ for(int i=0;i<n;i++){ if(b[i]==last){check=true;pos=i+1;} } } if(check){} else{flag=false;} } if(!flag){System.out.println("NO");} else{ System.out.println("YES"); for(int i=0;i<m;i++){ if(map.get(c[i])!=null && map.get(c[i]).size()>0){ sb.append(map.get(c[i]).remove(0)+" "); } else{sb.append(pos+" ");} } System.out.println(sb.toString()); } } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
e26f39165d8c2f15f7676537f50da1c0
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.io.*; public class Main2 { 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 sc = new FastReader(); public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i<n;i++) a[i] = sc.nextInt(); Queue<Integer> []q = new LinkedList[n+1]; int ans[] = new int[m]; for(int i=1;i<=n;i++) { q[i] = new LinkedList<>(); } for(int i=0;i<n;i++) { b[i] = sc.nextInt(); if(b[i]!=a[i]) { q[b[i]].add(i+1); } } int c[] = new int[m]; for(int i=0;i<m;i++) { c[i] = sc.nextInt(); } int waste = -1; if(!q[c[m-1]].isEmpty()) waste = q[c[m-1]].peek(); if(waste == -1) for(int i=n-1;i>=0;i--) { if(c[m-1] == b[i]) { waste = i + 1; break; } } if(waste == -1) { out.write("NO\n"); continue; } for(int i=0;i<m;i++) { if(q[c[i]].isEmpty()) { ans[i] = -1; } else { ans[i] = q[c[i]].poll(); } } if(ans[m-1] != -1) waste = ans[m-1]; for(int i=0;i<m;i++) if(ans[i] == -1) ans[i] = waste; boolean is = true; for(int i=1;i<=n;i++) { if(!q[i].isEmpty()) { is = false; break; } } if(is) { out.write("YES\n"); for(int z : ans) out.write((z)+" "); } else out.write("NO"); out.write("\n"); } out.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
dcd0f941309e14cf3f0066bf20ba6224
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.stream.IntStream; import java.util.Arrays; import java.util.Collection; import java.io.IOException; import java.util.stream.Collectors; import java.io.InputStreamReader; import java.util.stream.Stream; import java.util.StringTokenizer; import java.util.Queue; import java.io.BufferedReader; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kamel */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskC { static int size = 100_001; static int[] inPlace = new int[size]; static Queue<Integer>[] outPlace = new Queue[size]; static int[] a = new int[size]; static int[] b = new int[size]; static int[] c = new int[size]; static { for (int i = 0; i < outPlace.length; i++) { outPlace[i] = new ArrayDeque<>(); } } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int outPlaceCount = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int i = 0; i < n; i++) { b[i] = in.nextInt(); } for (int i = 0; i < m; i++) { c[i] = in.nextInt(); } for (int i = 0; i <= n; i++) { outPlace[i].clear(); } Arrays.fill(inPlace, 0); for (int i = 0; i < n; i++) { if (a[i] == b[i]) { inPlace[a[i]] = i + 1; } else { outPlace[b[i]].add(i + 1); outPlaceCount++; } } int[] result = new int[m]; int last = -1; boolean valid = true; for (int i = m - 1; i >= 0; i--) { int v = c[i]; if (outPlace[v].isEmpty()) { if (last > 0) { result[i] = last; } else { if (inPlace[v] <= 0) { valid = false; break; } else { last = inPlace[v]; result[i] = last; } } } else { int toPlace = outPlace[v].poll(); outPlaceCount--; last = toPlace; result[i] = last; } } if (!valid || outPlaceCount > 0) { out.println("NO"); } else { out.println("YES"); String ans = Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")); out.println(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()); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
ff6c74dd520a31e5fe9d11d84f81c4d4
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.stream.IntStream; import java.util.Arrays; import java.util.Collection; import java.io.IOException; import java.util.stream.Collectors; import java.io.InputStreamReader; import java.util.stream.Stream; import java.util.StringTokenizer; import java.util.Queue; import java.io.BufferedReader; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kamel */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int size = n + 1; int outPlaceCount = 0; Queue<Integer>[] outPlace = new Queue[size]; for (int i = 0; i < outPlace.length; i++) { outPlace[i] = new ArrayDeque<>(); } int[] inPlace = new int[size]; int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[m]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int i = 0; i < n; i++) { b[i] = in.nextInt(); } for (int i = 0; i < m; i++) { c[i] = in.nextInt(); } for (int i = 0; i < n; i++) { if (a[i] == b[i]) { inPlace[a[i]] = i + 1; } else { outPlace[b[i]].add(i + 1); outPlaceCount++; } } int[] result = new int[m]; int last = -1; boolean valid = true; for (int i = m - 1; i >= 0; i--) { int v = c[i]; if (outPlace[v].isEmpty()) { if (last > 0) { result[i] = last; } else { if (inPlace[v] <= 0) { valid = false; break; } else { last = inPlace[v]; result[i] = last; } } } else { int toPlace = outPlace[v].poll(); outPlaceCount--; last = toPlace; result[i] = last; } } if (!valid || outPlaceCount > 0) { out.println("NO"); } else { out.println("YES"); String ans = Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")); out.println(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()); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
8754c3572df1b8647deaa3e18c4932b3
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Stack; public class C1481 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-- > 0) { int n=sc.nextInt(),m=sc.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Map<Integer, Stack<Integer>> map = new HashMap<>(); Map<Integer, Stack<Integer>> map2 = new HashMap<>(); int[] b = new int[n]; for(int i=0;i<n;i++){ b[i]=sc.nextInt(); if(a[i]!=b[i]){ map.putIfAbsent(b[i], new Stack<>()); map.get(b[i]).add(i); } else { map2.putIfAbsent(a[i], new Stack<>()); map2.get(a[i]).add(i); } } boolean flag= true; int[] c = new int[m]; int[] ans = new int[m]; Arrays.fill(ans, -1); for(int i=0;i<m;i++) { c[i] = sc.nextInt(); } Stack<Integer> pending = new Stack<>(); for(int i=0;i<m;i++){ if(map.containsKey(c[i]) && !map.get(c[i]).empty()){ ans[i]=map.get(c[i]).pop(); while(!pending.empty()){ ans[pending.pop()]=ans[i]; } map2.putIfAbsent(c[i], new Stack<>()); map2.get(c[i]).add(ans[i]); } else if (map2.containsKey(c[i]) && !map2.get(c[i]).empty()) { ans[i] = map2.get(c[i]).peek(); while(!pending.empty()){ ans[pending.pop()]=ans[i]; } } else{ pending.add(i); } /*StringBuilder tmp = new StringBuilder(); for(int j=0;j<=i;j++){ tmp.append(ans[j]+" "); } System.out.println(tmp);*/ } for(Map.Entry<Integer, Stack<Integer>> entry : map.entrySet()){ if(!entry.getValue().empty()){ flag=false; } } if(!pending.empty()){ flag=false; } if(flag==false){ System.out.println("NO"); } else{ System.out.println("YES"); StringBuilder s=new StringBuilder(); for(int i=0;i<m;i++){ s.append(ans[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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
1b84e1c87f0223100d960219a84fdb58
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.*; import java.util.*; public class solution { public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); int t=sc.nextInt(); StringBuilder sb = new StringBuilder(); 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]; ArrayList<ArrayList<Integer>> adj=new ArrayList<>(); adj.add(new ArrayList<Integer>() ); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); adj.add(new ArrayList<Integer>() ); } for(int i=0;i<n;i++) { b[i]=sc.nextInt(); } for(int i=0;i<m;i++) { c[i]=sc.nextInt(); } int count=0;int ind=-1; for(int i=0;i<n;i++) { if(a[i]!=b[i]) {count++; adj.get(b[i]).add(i+1); } } if(adj.get(c[m-1]).size()>0) { ind=adj.get(c[m-1]).get(0); } else { for(int i=0;i<n;i++) { if(c[m-1]==b[i]) { ind=i+1; break; } } } int out[]=new int[m]; if(ind!=-1) { Arrays.fill(out,ind); } if(ind==-1) sb.append("NO"+"\n"); else { boolean flag=true; for(int i=0;i<m;i++) { // System.out.println(adj.get(c[i])+" *"); if(adj.get(c[i]).size()>0) { out[i]=adj.get(c[i]).get(adj.get(c[i]).size()-1); adj.get(c[i]).remove(adj.get(c[i]).size()-1); count--; } } if(count>0) { sb.append("NO"+"\n"); flag=false; } if(flag) { sb.append("YES"+ "\n"); for(int i=0;i<m;i++) sb.append(out[i]+" "); sb.append("\n"); } } } System.out.println(sb.toString()); } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ } class pair { int val;int ind; pair(int val,int ind) { this.val=val; this.ind=ind; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } 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; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
85f0b43ab72abad8b9d95fdecb1e25ea
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.lang.*; // import java.math.*; public class Codeforces { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(),m=I(); int a[]=new int[n]; int b[]=new int[n]; HashMap<Integer,ArrayList<Integer>> hm=new HashMap<>(); HashMap<Integer,ArrayList<Integer>> hm1=new HashMap<>(); for(int i=0;i<n;i++){ a[i]=I(); } for(int i=0;i<n;i++){ b[i]=I(); if(b[i]!=a[i]){ if(hm.containsKey(b[i])){ ArrayList<Integer> arr=hm.get(b[i]); arr.add(i); hm.put(b[i],arr); }else{ ArrayList<Integer> arr=new ArrayList<>(); arr.add(i); hm.put(b[i],arr); } } if(hm1.containsKey(b[i])){ ArrayList<Integer> arr=hm1.get(b[i]); arr.add(i); hm1.put(b[i],arr); }else{ ArrayList<Integer> arr=new ArrayList<>(); arr.add(i); hm1.put(b[i],arr); } } int c[]=new int[m]; for(int i=0;i<m;i++){ c[i]=I(); } int inhm[]=new int[(int)1e5+5]; int inhm1[]=new int[(int)1e5+5]; int ans[]=new int[m]; Arrays.fill(ans,-1); for(int i=0;i<m;i++){ if(hm.containsKey(c[i])){ ans[i]=hm.get(c[i]).get(inhm[c[i]]); a[ans[i]]=c[i]; inhm[c[i]]=(inhm[c[i]]+1)%hm.get(c[i]).size(); continue; } if(hm1.containsKey(c[i])){ ans[i]=hm1.get(c[i]).get(inhm1[c[i]]); a[ans[i]]=c[i]; inhm1[c[i]]=(inhm1[c[i]]+1)%hm1.get(c[i]).size(); continue; } } if(ans[m-1]==-1){ out.println("NO"); continue; } for(int i=m-2;i>=0;i--){ if(ans[i]==-1)ans[i]=ans[i+1]; } int q=0; for(int i=0;i<n;i++){ if(a[i]!=b[i]){ q=1; break; } } if(q==1)out.println("NO"); else{ out.println("YES"); for(int i:ans){ out.print((i+1)+" "); }out.println(); } } out.close(); } public static class pair { int a; int b; public pair(int val,int index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } //sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static long kadane(long a[],int n) { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static void DFS(int s,boolean visited[]) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited); } } } public static void setGraph(int n,int m)throws IOException { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } public static int BS(long a[],long x,int ii,int jj) { // int n=a.length; int mid=0; int i=ii,j=jj; while(i<=j) { mid=(i+j)/2; if(a[mid]<x){ i=mid+1; }else if(a[mid]>x){ j=mid-1; }else{ return mid; } } return -1; } public static int lower_bound(int arr[],int s, int N, int X) { if(arr[N]<X)return N; if(arr[0]>X)return -1; int left=s,right=N; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X) return mid; else if(arr[mid]>X){ if(mid>0 && arr[mid]>X && arr[mid-1]<=X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<N && arr[mid+1]>X && arr[mid]<=X){ return mid; }else{ left=mid+1; } } } return left; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); 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) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<=n;x+=x&(-x)) { farr[x]+=p; } } public long get(int x) { long ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } //SEGMENT TREE CODE // public static void segmentUpdate(int si,int ss,int se,int qs,int qe,long x) // { // if(ss>qe || se<qs)return; // if(qs<=ss && qe>=se) // { // seg[si][0]+=1L; // seg[si][1]+=x*x; // seg[si][2]+=2*x; // return; // } // int mid=(ss+se)/2; // segmentUpdate(2*si+1,ss,mid,qs,qe,x); // segmentUpdate(2*si+2,mid+1,se,qs,qe,x); // } // public static long segmentGet(int si,int ss,int se,int x,long f,long s,long t,long a[]) // { // if(ss==se && ss==x) // { // f+=seg[si][0]; // s+=seg[si][1]; // t+=seg[si][2]; // long ans=a[x]+(f*((long)x+1L)*((long)x+1L))+s+(t*((long)x+1L)); // return ans; // } // int mid=(ss+se)/2; // if(x>mid){ // return segmentGet(2*si+2,mid+1,se,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a); // }else{ // return segmentGet(2*si+1,ss,mid,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a); // } // } public static class myComp1 implements Comparator<pair1> { //sort in ascending order. public int compare(pair1 p1,pair1 p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } //sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp1()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static boolean isPalindrome(String s,int n) { for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } 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; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } 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; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(int i=0;i<a.length;i++){ out.print(a[i].a+"->"+a[i].b+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArrayL(ArrayList<Long> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayI(ArrayList<Integer> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayS(ArrayList<String> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapInt(HashMap<Integer,Integer> hm){ for(Map.Entry<Integer,Integer> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMapLong(HashMap<Long,Long> hm){ for(Map.Entry<Long,Long> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static int i(char ch){return Integer.parseInt(String.valueOf(ch));} public static int I()throws IOException{return sc.nextInt();} public static long L()throws IOException{return sc.nextLong();} public static String S()throws IOException{return sc.readLine();} public static double D()throws IOException{return sc.nextDouble();} } class FastReader { final private int BUFFER_SIZE = 1 << 18; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(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[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
1bf607a4aac31fe72f4fafdddddda514
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class FencePainting { public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in));//new FileReader("cowdance.in") PrintWriter out = new PrintWriter(System.out);//new FileWriter("cowdance.out") StringTokenizer st = new StringTokenizer(read.readLine()); int t = Integer.parseInt(st.nextToken()); for (int ie = 0; ie < t; ie++) { st = new StringTokenizer(read.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); ArrayList<ArrayList<Integer>> req = new ArrayList<ArrayList<Integer>>(); for(int i=0;i<=n;i++) { req.add(new ArrayList<Integer>()); } int [] b = new int [n]; int [] arr = new int [m]; int [] a = new int [n]; int [] p = new int [m]; st = new StringTokenizer(read.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(read.readLine()); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(st.nextToken()); if(a[i] != b[i]){ req.get(b[i]).add(i); } } st = new StringTokenizer(read.readLine()); for (int i = 0; i < m; i++) { p[i] = Integer.parseInt(st.nextToken()); } int lastR = -1; if(req.get((int) p[m-1]).size()>0) { lastR = req.get((int) p[m-1]).get(req.get((int) p[m-1]).size()-1); req.get((int) p[m-1]).remove(req.get((int) p[m-1]).size()-1); }else { for (int i = 0; i < n; i++) { if (b[i] == p[m-1]) { lastR = i; break; } } } arr[m-1] = lastR; boolean work = true; if (lastR == -1) { work = false; } for(int i=0;i<m-1;i++) { if(req.get(p[i]).size() == 0) { arr[i]=lastR; }else { arr[i] = req.get(p[i]).get(req.get(p[i]).size()-1); req.get(p[i]).remove(req.get(p[i]).size()-1); } } for(int i=1;i<=n;i++) { if(req.get(i).size()>0) { work = false; } } if (work) { out.println("YES"); for (int i = 0; i < m; i++) { out.print((arr[i]+1)+" "); } out.println(); }else { out.println("NO"); } } out.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
22bfaa6b8de040530341a4640e54911b
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class Main { public static boolean gsd(long x , int y) { for(int i = 2 ; i <= Math.min(y, x) ; i ++ ) { if (x%i == 0 && y%i == 0 ) return true; } return false; } public static int s(long x) { int sum = 0 ; do { sum += x%10 ; x /= 10; }while(x >0 ); return sum; } public static int right(String s) { int c = 0 ; for(int i = s.length()-1 ; i>=0; i -- ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static int left(String s) { int c = 0 ; for(int i = 0 ; i<s.length(); i ++ ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static boolean onlyas (String s) { for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) != 'a') return false ; }return true; } public static String convert(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) == '1') sb.append("0"); else sb.append("1"); } return sb.toString(); } public static void main(String[] args) throws IOException, InterruptedException{ PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while(t-- > 0 ) { int n = sc.nextInt() ; int m = sc.nextInt(); int [] a = new int[n]; int [] b = new int [n];int [] ans = new int [m] ; int [] c = new int [m]; HashMap<Integer, Queue<Integer>> hm = new HashMap<Integer, Queue<Integer>>(); HashMap<Integer,Integer> hh = new HashMap<>(); for (int i = 0 ; i < n ; i ++ ) { a[i] = sc.nextInt(); } for(int i = 0 ; i < n ;i ++ ) { int y = sc.nextInt(); b[i] = y; hh.put(y, i); if(y != a[i]) { if(hm.containsKey(y)) { hm.get(y).add(i); }else { Queue<Integer>queue = new LinkedList<Integer>(); queue.add(i); hm.put(y, queue); } } } for (int i = 0 ; i < m ; i ++ ) { c[i]= sc.nextInt(); } // System.out.print(hm); if (!hh.containsKey(c[m-1])) pw.println("NO"); else { ans[m-1] = hh.get(c[m-1]); int rr = ans[m-1]; for(int i = m-1 ; i >=0 ; i -- ) { if (hm.containsKey(c[i])) { int u = hm.get(c[i]).poll(); ans[i]= u; a[u]= b[u]; // System.out.print(c[i]); if(hm.get(c[i]).isEmpty()) hm.remove(c[i]); } else { ans[i]= ans[m-1]; } } // StringBuilder ss = new StringBuilder(); int r = 0 ; // pw.println(Arrays.toString(a) + " " +Arrays.toString(b)); for(r = 0 ; r < n; r ++ ) { if (a[r]!=b[r]) { pw.println("NO") ; break ; } }if(r == n ) { pw.println("YES"); for(int i = 0 ; i < m ; i ++ ) { pw.print(ans[i]+1 + " " ); } pw.println(); } } } pw.close (); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
ea1c14b0571e7a5a2004d12346693aa2
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class Main { public static boolean gsd(long x , int y) { for(int i = 2 ; i <= Math.min(y, x) ; i ++ ) { if (x%i == 0 && y%i == 0 ) return true; } return false; } public static int s(long x) { int sum = 0 ; do { sum += x%10 ; x /= 10; }while(x >0 ); return sum; } public static int right(String s) { int c = 0 ; for(int i = s.length()-1 ; i>=0; i -- ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static int left(String s) { int c = 0 ; for(int i = 0 ; i<s.length(); i ++ ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static boolean onlyas (String s) { for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) != 'a') return false ; }return true; } public static String convert(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) == '1') sb.append("0"); else sb.append("1"); } return sb.toString(); } public static void main(String[] args) throws IOException, InterruptedException{ PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while(t-- > 0 ) { int n = sc.nextInt() ; int m = sc.nextInt(); int [] a = new int[n]; int [] b = new int [n];int [] ans = new int [m] ; int [] c = new int [m]; HashMap<Integer, Queue<Integer>> hm = new HashMap<Integer, Queue<Integer>>(); HashMap<Integer,Integer> hh = new HashMap<>(); for (int i = 0 ; i < n ; i ++ ) { a[i] = sc.nextInt(); } for(int i = 0 ; i < n ;i ++ ) { int y = sc.nextInt(); b[i] = y; hh.put(y, i); if(y != a[i]) { if(hm.containsKey(y)) { hm.get(y).add(i); }else { Queue<Integer>queue = new LinkedList<Integer>(); queue.add(i); hm.put(y, queue); } } } for (int i = 0 ; i < m ; i ++ ) { c[i]= sc.nextInt(); } // System.out.print(hm); if (!hh.containsKey(c[m-1])) pw.println("NO"); else { ans[m-1] = hh.get(c[m-1]); int rr = ans[m-1]; for(int i = m-1 ; i >=0 ; i -- ) { if (hm.containsKey(c[i])) { int u = hm.get(c[i]).poll(); ans[i]= u; a[u]= b[u]; // System.out.print(c[i]); if(hm.get(c[i]).isEmpty()) hm.remove(c[i]); } else { ans[i]= (i<m-1) ? ans[i+1] : rr; } } // StringBuilder ss = new StringBuilder(); int r = 0 ; // pw.println(Arrays.toString(a) + " " +Arrays.toString(b)); for(r = 0 ; r < n; r ++ ) { if (a[r]!=b[r]) { pw.println("NO") ; break ; } }if(r == n ) { pw.println("YES"); for(int i = 0 ; i < m ; i ++ ) { pw.print(ans[i]+1 + " " ); } pw.println(); } } } pw.close (); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
4aebb1e796f2257e82151b054d8163c5
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.Closeable; import java.io.IOException; import java.io.InputStream; 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 FencePainting implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int t = in.ni(); while (t-- > 0) { read(); solveCase(); } } private int n, m; private int[] now, target, color; private void read() { n = in.ni(); m = in.ni(); now = new int[n]; for (int i = 0; i < n; i++) { now[i] = in.ni(); } target = new int[n]; for (int i = 0; i < n; i++) { target[i] = in.ni(); } color = new int[m]; for (int i = 0; i < m; i++) { color[i] = in.ni(); } } private void solveCase() { boolean possible = true; int plankForUnwanted = -1; for (int i = 0; i < n; i++) { if (target[i] == color[m - 1] && now[i] != target[i]) { plankForUnwanted = i; break; } } if (plankForUnwanted == -1) { for (int i = 0; i < n; i++) { if (target[i] == color[m - 1]) { plankForUnwanted = i; break; } } if (plankForUnwanted == -1) { print(false, null); return; } } Map<Integer, ArrayDeque<Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (i == plankForUnwanted) continue; if (now[i] != target[i]) { ArrayDeque<Integer> list = map.getOrDefault(target[i], new ArrayDeque<>()); list.addLast(i); map.put(target[i], list); } } int[] result = new int[m]; for (int i = 0; i < m - 1; i++) { int plank = plankForUnwanted; if (map.containsKey(color[i])) { ArrayDeque<Integer> queue = map.get(color[i]); plank = queue.poll(); if (queue.size() == 0) { map.remove(color[i]); } } result[i] = plank; now[plank] = color[i]; } result[m - 1] = plankForUnwanted; now[plankForUnwanted] = color[m - 1]; for (int i = 0; i < n; i++) { possible &= now[i] == target[i]; } print(possible, result); } private void print(boolean possible, int[] result) { if (possible) { out.println("YES"); for (int idx : result) { out.print(idx + 1); out.print(' '); } out.println(); } else { out.println("NO"); } } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (FencePainting instance = new FencePainting()) { instance.solve(); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
35cc132dac6ad1d9c6157d50ae7b87f8
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.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; // graph, dfs,bfs, get connected components,iscycle, isbipartite, dfs on trees public class scratch_25 { static class Graph{ public static class Vertex{ HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex } public static HashMap<Integer,Vertex> vt; // for vertices(all) public Graph(){ vt= new HashMap<>(); } public static int numVer(){ return vt.size(); } public static boolean contVer(int ver){ return vt.containsKey(ver); } public static void addVer(int ver){ Vertex v= new Vertex(); vt.put(ver,v); } public static void addEdge(int ver1, int ver2, int weight){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } Vertex v1= vt.get(ver1); Vertex v2= vt.get(ver2); v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge v2.nb.put(ver1,weight); } public static void delEdge(int ver1, int ver2){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } vt.get(ver1).nb.remove(ver2); vt.get(ver2).nb.remove(ver1); } public static void delVer(int ver){ if(!vt.containsKey(ver)){ return; } Vertex v1= vt.get(ver); ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); vt.get(s).nb.remove(ver); } vt.remove(ver); } static boolean done[]; static int parent[]; static ArrayList<Integer>vals= new ArrayList<>(); public static boolean isCycle(int i){ Stack<Integer>stk= new Stack<>(); stk.push(i); while(!stk.isEmpty()){ int x= stk.pop(); vals.add(x); // System.out.print("current="+x+" stackinit="+stk); if(!done[x]){ done[x]=true; } else if(done[x] ){ return true; } ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet()); for (int j = 0; j <ar.size() ; j++) { if(parent[x]!=ar.get(j)){ parent[ar.get(j)]=x; stk.push(ar.get(j)); } } // System.out.println(" stackfin="+stk); } return false; } static int[]level; static int[] curr; static int[] fin; public static void dfs(int src){ done[src]=true; level[src]=0; Queue<Integer>q= new LinkedList<>(); q.add(src); while(!q.isEmpty()){ int x= q.poll(); ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ level[v]=level[x]+1; done[v]=true; q.offer(v); } } } } static int oc[]; static int ec[]; public static void dfs1(int src){ Queue<Integer>q= new LinkedList<>(); q.add(src); done[src]= true; // int count=0; while(!q.isEmpty()){ int x= q.poll(); // System.out.println("x="+x); int even= ec[x]; int odd= oc[x]; if(level[x]%2==0){ int val= (curr[x]+even)%2; if(val!=fin[x]){ // System.out.println("bc"); even++; vals.add(x); } } else{ int val= (curr[x]+odd)%2; if(val!=fin[x]){ // System.out.println("bc"); odd++; vals.add(x); } } ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); // System.out.println(arr); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ done[v]=true; oc[v]=odd; ec[v]=even; q.add(v); } } } } } // int count=0; //static long count=0; static 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 int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { double x; double y; public Pair(double x, double y) { this.x = x; this.y = y;} @Override public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { if(this.x >o.x){ return 1; } else if(this.x==o.x){ return 0; } else{ return -1; } } } // After writing solution, quick scan for: // array out of bounds // special cases e.g. n=1? // // Big numbers arithmetic bugs: // int overflow // sorting, or taking max, or negative after MOD public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t= Reader.nextInt(); for (int tt = 0; tt <t ; tt++) { int n= Reader.nextInt(); int m= Reader.nextInt(); HashMap<Integer, Queue<Integer>>places= new HashMap<>(); int max[]= new int[n+1]; int a[]= new int[n]; int b[]= new int[n]; int c[]= new int[m]; int have[]= new int[n+1]; for (int i = 0; i <n ; i++) { Queue<Integer>x= new LinkedList<>(); places.put(i+1,x); } for (int i = 0; i <n ; i++) { a[i]= Reader.nextInt(); } for (int i = 0; i <n ; i++) { b[i]= Reader.nextInt(); if(b[i]!=a[i]){ if(!places.containsKey(b[i])){ Queue<Integer>ind= new LinkedList<>(); ind.add(i); places.put(b[i],ind); } else{ Queue<Integer>ar= places.get(b[i]); ar.add(i); places.put(b[i],ar); } } max[b[i]]++; } for (int i = 0; i <m ; i++) { c[i]= Reader.nextInt(); have[c[i]]++; } boolean b1=true; for (int i = 0; i <n ; i++) { if(have[i+1]<places.get(i+1).size()){ b1=false; break; } } if(!b1){ out.append("NO"+"\n"); } else{ if(max[c[m-1]]==0){ out.append("NO"+"\n"); } else{ out.append("YES"+"\n"); int put=0; if(places.get(c[m-1]).size()==0){ for (int i = n-1; i >=0 ; i--) { if(b[i]==c[m-1]){ put=i; break; } } } else{ for(int x:places.get(c[m-1])){ put=x; } } for (int i = 0; i <m ; i++) { int x= c[i]; if(places.get(x).size()==0){ out.append((put+1)+" "); } else{ out.append((places.get(x).peek()+1)+" "); Queue<Integer>q= places.get(x); q.poll(); } } out.append("\n"); } } } out.flush(); out.close(); } public static int localMinUtil(int[] arr, int low, int high, int n) { // Find index of middle element int mid = low + (high - low) / 2; // Compare middle element with its neighbours // (if neighbours exist) if(mid == 0 || arr[mid - 1] > arr[mid] && mid == n - 1 || arr[mid] < arr[mid + 1]) return mid; // If middle element is not minima and its left // neighbour is smaller than it, then left half // must have a local minima. else if(mid > 0 && arr[mid - 1] < arr[mid]) return localMinUtil(arr, low, mid - 1, n); // If middle element is not minima and its right // neighbour is smaller than it, then right half // must have a local minima. return localMinUtil(arr, mid + 1, high, n); } // A wrapper over recursive function localMinUtil() public static int localMin(int[] arr, int n) { return localMinUtil(arr, 0, n - 1, n); } public static String convert(String s,int n){ if(s.length()==n){ return s; } else{ int x= s.length(); int v=n-x; String str=""; for (int i = 0; i <v ; i++) { str+='0'; } str+=s; return str; } } public static int lower(int arr[],int key){ int low = 0; int high = arr.length-1; while(low < high){ int mid = low + (high - low)/2; if(arr[mid] >= key){ high = mid; } else{ low = mid+1; } } return low; } public static int upper(int arr[],int key){ int low = 0; int high = arr.length-1; while(low < high){ int mid = low + (high - low+1)/2; if(arr[mid] <= key){ low = mid; } else{ high = mid-1; } } return low; } static long modExp(long a, long b, long mod) { //System.out.println("a is " + a + " and b is " + b); if (a==1) return 1; long ans = 1; while (b!=0) { if (b%2==1) { ans = (ans*a)%mod; } a = (a*a)%mod; b/=2; } return ans; } public static long modmul(long a, long b, long mod) { return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod; } static long sum(long n){ // System.out.println("lol="+ (n*(n-1))/2); return (n*(n+1))/2; } public static ArrayList<Integer> Sieve(int n) { boolean arr[]= new boolean [n+1]; Arrays.fill(arr,true); arr[0]=false; arr[1]=false; for (int i = 2; i*i <=n ; i++) { if(arr[i]){ for (int j = 2; j <=n/i ; j++) { int u= i*j; arr[u]=false; }} } ArrayList<Integer> ans= new ArrayList<>(); for (int i = 0; i <n+1 ; i++) { if(arr[i]){ ans.add(i); } } return ans; } static long power( long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static long ceil_div(long a, long b){ return (a+b-1)/b; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
d9c2f0b0ad6dd81aa30602f8c5f9362c
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
//I AM THE CREED /* //I AM THE CREED /* package codechef; // don't place package name! */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.awt.Point; public class Main{ static final Random random=new Random(); public static void main(String[] args) throws IOException { Scanner input=new Scanner(System.in); input.nextInt(); while(input.hasNext()){ int n=input.nextInt(); int m=input.nextInt(); HashMap<Integer, HashSet<Integer>> mismatch=new HashMap<>(); HashMap<Integer, HashSet<Integer>> match=new HashMap<>(); int[] possible=new int[m]; HashSet<Integer> colors=new HashSet<>(); int[] a=new int[n]; int[] b=new int[n]; int[] c=new int[m]; for(int i=0;i<n;i++){ a[i]=input.nextInt(); } for(int i=0;i<n;i++){ b[i]=input.nextInt(); colors.add(b[i]); } for(int i=0;i<m;i++){ c[i]=input.nextInt(); } for(int i=m-1;i>=0;i--){ if(colors.contains(c[i])){ possible[i]=c[i]; } else{ possible[i]=(i==m-1?-1:possible[i+1]); } } for(int i=0;i<n;i++){ if(a[i]==b[i]){ HashSet<Integer> set=match.getOrDefault(b[i], new HashSet<Integer>()); set.add(i); match.put(b[i], set); continue; } HashSet<Integer> set=mismatch.getOrDefault(b[i], new HashSet<Integer>()); set.add(i); mismatch.put(b[i], set); } int[] sol=new int[m]; boolean painted=true; for(int i=0;i<m;i++){ if(mismatch.containsKey(c[i])){ HashSet<Integer> set=mismatch.get(c[i]); int index=-1; for(int key:set){ index=key; break; } set.remove(index); if(set.size()==0){ mismatch.remove(c[i]); } set=match.getOrDefault(c[i], new HashSet<Integer>()); set.add(index); match.put(c[i], set); sol[i]=index+1; continue; } if(match.containsKey(c[i])){ HashSet<Integer> set=match.get(c[i]); int index=-1; for(int key:set){ index=key; break; } sol[i]=index+1; continue; } if(mismatch.size()!=0){ int col=-1; int index=-1; for(int key:mismatch.keySet()){ col=key; for(int ind:mismatch.get(key)){ index=ind; break; } break; } sol[i]=index+1; continue; } if(possible[i]==-1){ painted=false; break; } int col=possible[i]; int index=-1; HashSet<Integer> set=match.get(col); for(int ind:set){ index=ind; break; } set.remove(index); if(set.size()==0){ match.remove(col); } set=new HashSet<Integer>(); set.add(index); mismatch.put(col, set); sol[i]=index+1; } StringBuilder sb=new StringBuilder(); for(int i=0;i<sol.length;i++){ sb.append(sol[i]+" "); } if(painted && mismatch.size()==0){ System.out.println("YES"); System.out.println(sb.toString()); } else{ System.out.println("NO"); } } } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } //Credits to SecondThread(https://codeforces.com/profile/SecondThread) for this tempelate static void ruffleSort(int [] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } //Credits to SecondThread(https://codeforces.com/profile/SecondThread) for this tempelate. 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
fc5d2dbc6ffaaebc5ec3c20288230f48
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 test { public static void main(String[] args) throws IOException { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = 1; int T=fs.nextInt(); outer: for (int tt=0; tt<T; tt++) { int n = fs.nextInt(); int m = fs.nextInt(); int[] a = fs.readArray(n); int[] b = fs.readArray(n); int[] c = fs.readArray(m); HashMap<Integer, List<Integer>> hm = new HashMap(); int myLastPaintIdx = -1, myLastPaintIdx2 = -1; Set<Integer> reqSet = new HashSet(); Set<Integer> allSet = new HashSet<>(); for (int i=0; i<n; i++) { allSet.add(b[i]); if (a[i]!=b[i]) { List<Integer> lst = hm.getOrDefault(b[i], new LinkedList()); lst.add(i); hm.put(b[i], lst); reqSet.add(i); } if (b[i]==c[m-1] && b[i]!=a[i]) { myLastPaintIdx=i; } if (b[i]==c[m-1]) { myLastPaintIdx2=i; } } if (myLastPaintIdx==-1) { myLastPaintIdx=myLastPaintIdx2; } HashMap<Integer, Integer> cm = new HashMap(); for (int i=0; i<m; i++) { cm.put(c[i], cm.getOrDefault(c[i],0)+1); } for (int key: hm.keySet()) { if (cm.getOrDefault(key, 0)<hm.get(key).size()) { out.println("NO"); continue outer; } } int reqCount = reqSet.size(); if (m<reqCount) { out.println("NO"); continue; } if (!allSet.contains(c[m-1]) || myLastPaintIdx==-1) { out.println("NO"); continue; } StringBuilder sb = new StringBuilder(); if (hm.size()==0) { for (int i=0 ;i<m; i++) { sb.append((myLastPaintIdx+1)+" "); } out.println("YES"); out.println(sb); continue; } // out.println("myLastPaintIdx : "+ myLastPaintIdx); for (int i=0; i<m; i++) { int paint = c[i]; if (hm.containsKey(paint)) { List<Integer> lst = hm.get(paint); int idx = lst.get(0); sb.append((idx+1)+" "); a[idx]=paint; lst.remove(0); if (lst.size()==0) { hm.remove(paint); } reqSet.remove(idx); } else { sb.append((1+myLastPaintIdx)+" "); a[myLastPaintIdx]=paint; } } boolean f = true; for (int i=0; i<n; i++) { // out.print(a[i]+" "); if (a[i]!=b[i]) { f=false; break; } } if (!f) { out.println("NO"); } else { out.println("YES"); out.println(sb); } } 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 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
2e76b0288d994374e41ceba6072201a5
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.StringTokenizer; import java.util.*; public class FencePainting { MyScanner scanner = new MyScanner(); String yes = "YES", no = "NO"; private void canPaint(int[] initial, int[] desire, int[] changes) { Map<Integer, List<Integer>> missing = new HashMap<>(); Map<Integer, Integer> colorOnDesire = new HashMap<>(); int missed = 0; for (int i = 0; i < initial.length; i++) { if (initial[i] != desire[i]) { missing.computeIfAbsent(desire[i], v -> new ArrayList<>()).add(i); missed++; } colorOnDesire.put(desire[i], i); } if (missed > changes.length) { System.out.println(no); return; } // Special handling, learn from answer Integer special = -1; Integer lastChange = changes[changes.length - 1]; if (missing.containsKey(lastChange)) { special = missing.get(lastChange).iterator().next(); missing.get(lastChange).remove(special); if (missing.get(lastChange).size() == 0) { missing.remove(lastChange); } } else if (colorOnDesire.containsKey(lastChange)) { special = colorOnDesire.get(lastChange); } else { System.out.println(no); return; } // int[] ans = new int[changes.length]; // ans[ans.length - 1] = special; List<Integer> paintOrder = new ArrayList<>(); for (int i = 0; i < changes.length - 1; i++) { Integer change = changes[i]; if (missing.containsKey(change)) { int idx = missing.get(change).remove(0); paintOrder.add(idx + 1); if (missing.get(change).isEmpty()) { missing.remove(change); } } else { paintOrder.add(special + 1); } } paintOrder.add(special + 1); if (!missing.isEmpty()) { System.out.println(no); return; } StringBuilder sb = new StringBuilder(); for (int num : paintOrder) { sb.append(num + " "); } System.out.println(yes); System.out.println(sb.toString().substring(0, sb.length() - 1)); } public static void main(String[] args) { FencePainting test = new FencePainting(); int t = test.scanner.nextInt(); for (int i = 0; i < t; i++) { int planks = test.scanner.nextInt(), painters = test.scanner.nextInt(); int[] initialColors = new int[planks]; for (int j = 0; j < planks; j++) { initialColors[j] = test.scanner.nextInt(); } int[] desiredColors = new int[planks]; for (int j = 0; j < planks; j++) { desiredColors[j] = test.scanner.nextInt(); } int[] colors = new int[painters]; for (int j = 0; j < painters; j++) { colors[j] = test.scanner.nextInt(); } test.canPaint(initialColors, desiredColors, colors); } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
24db4e1dd6a821af4ac239d84b21e271
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.StringTokenizer; import java.util.*; public class FencePainting { MyScanner scanner = new MyScanner(); String yes = "YES", no = "NO"; private void canPaint(int[] initial, int[] desire, int[] changes) { Map<Integer, List<Integer>> missing = new HashMap<>(); Map<Integer, Integer> colorOnDesire = new HashMap<>(); int missed = 0; for (int i = 0; i < initial.length; i++) { if (initial[i] != desire[i]) { missing.computeIfAbsent(desire[i], v -> new ArrayList<>()).add(i); missed++; } colorOnDesire.put(desire[i], i); } if (missed > changes.length) { System.out.println(no); return; } List<Integer> paintOrder = new ArrayList<>(); for (int i = 0; i < changes.length; i++) { Integer change = changes[i]; if (missing.containsKey(change)) { int idx = missing.get(change).remove(0); paintOrder.add(idx + 1); if (missing.get(change).isEmpty()) { missing.remove(change); } } else if (missing.size() > 0){ int randomKey = missing.keySet().iterator().next(); paintOrder.add(missing.get(randomKey).get(0) + 1); } else if (colorOnDesire.containsKey(change)) { paintOrder.add(colorOnDesire.get(change) + 1); } else { boolean found = false; for (int j = i + 1; j < changes.length; j++) { if (colorOnDesire.containsKey(changes[j])) { paintOrder.add(colorOnDesire.get(changes[j]) + 1); found = true; break; } } if (!found) { System.out.println(no); return; } } } if (!missing.isEmpty()) { System.out.println(no); return; } StringBuilder sb = new StringBuilder(); for (int num : paintOrder) { sb.append(num + " "); } System.out.println(yes); System.out.println(sb.toString().substring(0, sb.length() - 1)); } public static void main(String[] args) { FencePainting test = new FencePainting(); int t = test.scanner.nextInt(); for (int i = 0; i < t; i++) { int planks = test.scanner.nextInt(), painters = test.scanner.nextInt(); int[] initialColors = new int[planks]; for (int j = 0; j < planks; j++) { initialColors[j] = test.scanner.nextInt(); } int[] desiredColors = new int[planks]; for (int j = 0; j < planks; j++) { desiredColors[j] = test.scanner.nextInt(); } int[] colors = new int[painters]; for (int j = 0; j < painters; j++) { colors[j] = test.scanner.nextInt(); } test.canPaint(initialColors, desiredColors, colors); } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
7b1a1252e148ead12b6a53838a3e5e8a
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public class a{ static int[] count,count1,count2; static boolean[] prime; static Node[] nodes,nodes1,nodes2; static long[] arr; static long[][] cost; static int[] arrInt,darrInt,farrInt; static long[][] dp; static char[] ch,ch1; static long[] darr,farr; static char[][] mat,mat1; static boolean[] vis; static long x,h; static long maxl; static double dec; static long mx = (long)1e8; static String s,s1,s2,s3,s4; static long minl; static long mod = ((long)(1e9))+7; // static int minl = -1; // static long n; static int n,n1,n2,q,r1,c1,r2,c2; static long a; static long b; static long c; static long d; static long y,z; static int m; static int ans; static long k; static FastScanner sc; static String[] str,str1; static Set<Integer> set,set1,set2; static SortedSet<Long> ss; static List<Integer> list,list1,list2,list3; static PriorityQueue<Node> pq,pq1; static LinkedList<Node> ll,ll1,ll2; static Map<Integer,List<Integer>> map1; static Map<Long,Long> map; static StringBuilder sb,sb1,sb2; static int index; static long[] sum; static int[] dx = {0,-1,0,1,-1,1,-1,1}; static int[] dy = {-1,0,1,0,-1,-1,1,1}; // public static void solve(){ // FastScanner sc = new FastScanner(); // // int t = sc.nextInt(); // int t = 1; // for(int tt = 0 ; tt < t ; tt++){ // // s = sc.next(); // // s1 = sc.next(); // n = sc.nextInt(); // m = sc.nextInt(); // // int k = sc.nextInt(); // // sb = new StringBuilder(); // map = new HashMap<>(); // map1 = new HashMap<>(); // // q = sc.nextInt(); // // sb = new StringBuilder(); // // long k = sc.nextLong(); // // ch = sc.next().toCharArray(); // // count = new int[200002]; // // m = sc.nextInt(); // for(int o = 0 ; o < m ; o++){ // int l = sc.nextInt()-1; // int r = sc.nextInt()-1; // } // } // } //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public static void solve(){ Set<Integer> unmatchedSet = new HashSet<>(); Map<Integer,List<Integer>> unmatchedMap = new HashMap<>(); Map<Integer,Integer> matchedMap = new HashMap<>(); count = new int[n+1]; for(int i = 0 ; i < n ; i++){ count[arrInt[i]] = i; if(arrInt[i] != darrInt[i]){ unmatchedSet.add(i); if(!unmatchedMap.containsKey(darrInt[i])) unmatchedMap.put(darrInt[i],new ArrayList<>()); unmatchedMap.get(darrInt[i]).add(i); continue; } if(matchedMap.containsKey(arrInt[i])) continue; matchedMap.put(arrInt[i],i); } // System.out.println(matchedMap.size()); sb = new StringBuilder(); for(int i = 0 ; i < m ; i++){ int c = farrInt[i]; if(unmatchedMap.containsKey(c)){ list = unmatchedMap.get(c); // System.out.println(list.size()+" " + unmatchedSet.size()); int index = list.get(0); list.remove(0); if(list.size() == 0) unmatchedMap.remove(c); sb.append((index+1) + " "); unmatchedSet.remove(index); matchedMap.put(c,index); // System.out.println(list.size()+" " + unmatchedSet.size()); } else if(unmatchedSet.size() != 0){ int index = -1; for(Integer j : unmatchedSet){ index = j; break; } sb.append((index+1) + " "); } else if(matchedMap.containsKey(c)){ int index = -1; index = matchedMap.get(c); sb.append((index+1) + " "); } else{ if((i != m-1)){ boolean flag = false; for(int j = i+1 ; j < m ; j++){ if((matchedMap.containsKey(farrInt[j]))){ sb.append((matchedMap.get(farrInt[j])+1)+" "); flag = true; break; } } if(flag) continue; } System.out.println("NO"); return; } } if(unmatchedSet.size() != 0){ System.out.println("NO"); return; } System.out.println("YES"); System.out.println(sb); } public static void main(String[] args) { sc = new FastScanner(); // Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // int t = 1; // int l = 1; while(t > 0){ // n = sc.nextInt(); // n = sc.nextLong(); // k = sc.nextLong(); // x = sc.nextLong(); // y = sc.nextLong(); // z = sc.nextLong(); // a = sc.nextLong(); // b = sc.nextLong(); // c = sc.nextLong(); // d = sc.nextLong(); // x = sc.nextLong(); // y = sc.nextLong(); // z = sc.nextLong(); // d = sc.nextLong(); n = sc.nextInt(); // n1 = sc.nextInt(); m = sc.nextInt(); // q = sc.nextInt(); // k = sc.nextLong(); // x = sc.nextLong(); // d = sc.nextLong(); // s = sc.next(); // ch = sc.next().toCharArray(); // ch1 = sc.next().toCharArray(); // n = 3; // arr = new long[n]; // for(int i = 0 ; i < n ; i++){ // arr[i] = sc.nextLong(); // } arrInt = new int[n]; for(int i = 0 ; i < n ; i++){ arrInt[i] = sc.nextInt(); } // x = sc.nextLong(); // y = sc.nextLong(); // ch = sc.next().toCharArray(); // m = n; // m = sc.nextInt(); // darr = new long[m]; // for(int i = 0 ; i < m ; i++){ // darr[i] = sc.nextLong(); // } // k = sc.nextLong(); // m = n; darrInt = new int[n]; for(int i = 0 ; i < n ; i++){ darrInt[i] = sc.nextInt(); } farrInt = new int[m]; for(int i = 0; i < m ; i++){ farrInt[i] = sc.nextInt(); } // mat = new long[n][m]; // for(int i = 0 ; i < n ; i++){ // for(int j = 0 ; j < m ; j++){ // mat[i][j] = sc.nextLong(); // } // } // m = n; // mat = new char[n][m]; // for(int i = 0 ; i < n ; i++){ // String s = sc.next(); // for(int j = 0 ; j < m ; j++){ // mat[i][j] = s.charAt(j); // } // } // str = new String[n]; // for(int i = 0 ; i < n ; i++) // str[i] = sc.next(); // nodes = new Node[n]; // for(int i = 0 ; i < n ;i++) // nodes[i] = new Node(sc.nextLong(),sc.nextLong()); solve(); t -= 1; } } public static int log(long n,long base){ if(n == 0 || n == 1) return 0; if(n == base) return 1; double num = Math.log(n); double den = Math.log(base); if(den == 0) return 0; return (int)(num/den); } public 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 (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return (b/gcd(b, a % b)) * a; } public static long mod_inverse(long a,long mod){ long x1=1,x2=0; long p=mod,q,t; while(a%p!=0){ q = a/p; t = x1-q*x2; x1=x2; x2=t; t=a%p; a=p; p=t; } return x2<0 ? x2+mod : x2; } // public static void swap(int i,int j){ // long temp = arr[j]; // arr[j] = arr[i]; // arr[i] = temp; // } static final Random random=new Random(); static void ruffleSortLong(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSortInt(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSortChar(char[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); char temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long binomialCoeff(long n, long k){ long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res = (res*(n - i)); res = (res/(i + 1)); } return res; } static long[] fact; public static long inv(long n){ return power(n, mod-2); } public static void fact(int n){ fact = new long[n+1]; fact[0] = 1; for(int j = 1;j<=n;j++) fact[j] = (fact[j-1]*(long)j)%mod; } public static long binom(int n, int k){ fact(n+1); long prod = fact[n]; prod*=inv(fact[n-k]); prod%=mod; prod*=inv(fact[k]); prod%=mod; return prod; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static class Node{ int first; int second; int third; Node(int f,int s,int t){ this.first = f; this.second = s; this.third = t; } } 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
98d29da78feede9685c4fb74e805bc6b
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.*; /** * * @Har_Har_Mahadev */ public class C { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static HashSet<Integer>[] adj; public static void process() throws IOException { int n = sc.nextInt(),m = sc.nextInt(); int a[] = sc.readArray(n); int b[] = sc.readArray(n); int c[] = sc.readArray(m); adj = new HashSet[n+1]; for(int i=0; i<=n; i++)adj[i] = new HashSet<Integer>(); int req[] = new int[n+1]; int all[] = new int[n+1]; int have[] = new int[n+1]; int indexi[] = new int[n+1]; for(int i=0; i<n; i++) { if(a[i] != b[i]) { adj[b[i]].add(i+1); req[b[i]]++; } all[b[i]]++; indexi[b[i]] = i+1; } for(int e : c)have[e]++; for(int i=1; i<=n; i++) { if(req[i] > have[i]) { println("NO"); return; } } int last = c[m-1]; if(all[last] == 0) { println("NO"); return; } int index = -1; if(adj[last].size() > 0) { int cc = 0; for(int e : adj[last]) { cc = e; } adj[last].remove(cc); index = cc; } else { index = indexi[last]; } println("YES"); for(int i=0; i<m; i++) { if(i == m-1) { print(index+" "); continue; } if(adj[c[i]].size() > 0) { int cc = 0; for(int e : adj[c[i]]) { cc = e; break; } adj[c[i]].remove(cc); print(cc+" "); } else { print(index+" "); } } println(); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lca(long a, long b) { return (a * b) / gcd(a, b); } public static int lca(int a, int b) { return (a * b) / gcd(a, b); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
99c3369168aa4b76ad0db71c15be717f
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
/* ⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀⢠⠞⠉⠙⠲⡀⠀ ⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀⡏⠀⠀Y⠀⠀⢷ ⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀⢸⠀⠀A⠀⠀ ⡇ ⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀⣸⠀⠀S⠀ ⡇ ⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀⢹⠀⠀H⠀⠀ ⡇ ⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀⡇⠀⠀⠀⠀⡼ ⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀⠘⠤⣄⣠⠞⠀ ⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀ ⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀ ⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀ ⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀ */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; // System.out is a PrintWriter // import java.util.Arrays; import java.util.ArrayDeque; import java.util.ArrayList; // import java.util.Collections; // for ArrayDeque sorting mainly import java.util.HashMap; // import java.util.HashSet; // import java.util.Random; import java.util.StringTokenizer; public class C { // class Codechef { public static void main(String[] args) throws IOException { FastScanner scn = new FastScanner(); PrintWriter out = new PrintWriter(System.out); loop: for (int tc = scn.nextInt(); tc > 0; tc--) { int N = scn.nextInt(), K = scn.nextInt(); int[] arr = new int[N] , worker = new int[K]; HashMap<Integer, ArrayDeque<Integer>> valid = new HashMap<>(); for (int i = 0; i < N; i++) { arr[i] = scn.nextInt(); } for (int i = 0, ip = 0; i < N; i++) { ip = scn.nextInt(); if (ip != arr[i]) { if (!valid.containsKey(ip)) { valid.put(ip, new ArrayDeque<>()); } valid.get(ip).addLast(i); } } ArrayList<Integer> extra = new ArrayList<>(); if (valid.size() == 0) { for (int i = 0; i < K; i++) { extra.add(i); worker[i] = scn.nextInt(); } } else { for (int i = 0; i < K; i++) { worker[i] = scn.nextInt(); if (valid.containsKey(worker[i])) { ArrayDeque<Integer> deq = valid.get(worker[i]); int curr = deq.removeFirst(); if (deq.size() == 0) valid.remove(worker[i]); if (extra.size() != 0) { for (int e : extra) { worker[e] = curr; } extra = new ArrayList<>(); } arr[curr] = worker[i]; worker[i] = curr; } else { extra.add(i); } } } if (valid.size() != 0) { out.println("NO"); continue loop; } if (extra.size() != 0) { int last = extra.get(extra.size() - 1); for (int i = 0; i < N; i++) { if (worker[last] == arr[i]) { for (int e : extra) { worker[e] = i; } extra.clear(); break; } } if (extra.size() != 0) { out.println("NO"); continue loop; } } out.println("YES"); for (int i : worker) { out.print((i + 1) + " "); } out.println(); } out.close(); } private static int gcd(int num1, int num2) { int temp = 0; while (num2 != 0) { temp = num1; num1 = num2; num2 = temp % num2; } return num1; } private static int lcm(int num1, int num2) { return (int)((1L * num1 * num2) / gcd(num1, num2)); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
6a99fef81ba9a4e6ee8e894b81e582ba
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 javax.swing.text.html.parser.Entity; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.stream.Collectors; public class Main { static FastScanner sc; static PrintWriter pw; static final long INF = 2000000000l; static final int MOD = 1000000007; static final int N = 1005; static int mul(int a, int b) { return (int) ((a * 1l * b) % MOD); } public static int pow(int a, int n) { int res = -1; while(n != 0) { if((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } public static int inv(int a) { return pow(a, MOD-2); } public static List<Integer> getPrimes() { List<Integer> ans = new ArrayList<>(); boolean[] prime = new boolean[N]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for(int i = 2; i < N; ++i) { if(prime[i]) { ans.add(i); if (i * 1l * i <= N) for (int j = i * i; j < N; j += i) prime[j] = false; } } return ans; } public static long gcd(long a, long b) { if(b == 0) return a; else return gcd(b, a % b); } public static void solve() throws IOException { int n = sc.nextInt(), m = sc.nextInt(); Map<Integer, Queue<Integer>> need = new HashMap<>(); Map<Integer, Integer> can = new HashMap<>(); int[] a = new int[n]; int[] b = new int[n]; for(int i = 0; i < n; ++i) { a[i] = sc.nextInt(); } for(int i = 0 ; i < n; ++i ) { b[i] = sc.nextInt(); can.put(b[i], i); } for(int i = 0; i < n; ++i) { if(a[i] != b[i]) { if(!need.containsKey(b[i])) need.put(b[i], new LinkedList<>()); need.get(b[i]).add(i); } } int[] c = new int[m]; for(int i = 0; i < m; ++i) { c[i] = sc.nextInt(); } int[] ans = new int[m]; int x = 0; for(int i = m - 1; i >= 0; i--) { if(need.containsKey(c[i])) { Integer pos = need.get(c[i]).poll(); ans[i] = pos; x = pos; if(need.get(c[i]).size() == 0) need.remove(c[i]); } else if(i != m - 1) { ans[i] = ans[i + 1]; } else if(can.containsKey(c[i])) { Integer pos = can.get(c[i]); ans[i] = pos; } else { ans[i] = x; } } for(int i = 0; i < m; ++i) a[ans[i]] = c[i]; boolean answer = true; for(int i = 0; i < n; ++i) if(a[i] != b[i]) answer = false; if(!answer) pw.println("NO"); else { pw.println("YES"); for(int i = 0; i < m; ++i) pw.print((ans[i] + 1) + " "); pw.println(); } // pw.println(Arrays.toString(ans)); } public static void main(String[] args) throws IOException { sc = new FastScanner(System.in); pw = new PrintWriter(System.out); int xx = sc.nextInt(); while(xx > 0) { solve(); xx--; } pw.close(); } } class FastScanner { static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() throws IOException { while (!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 double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
abe7c4427d661c40274eb59902862e29
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.util.function.*; public class FencePainting { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int M = io.nextInt(); final int[] A = io.nextIntArray(N, 1); final int[] B = io.nextIntArray(N, 1); final int[] C = io.nextIntArray(M); int[] canPaint = new int[N + 1]; IntList[] mustPaint = new IntList[N + 1]; for (int i = 1; i <= N; ++i) { mustPaint[i] = new IntList(); } for (int i = 1; i <= N; ++i) { if (A[i] == B[i]) { canPaint[B[i]] = i; } else { mustPaint[B[i]].add(i); } } int[] ans = new int[M]; int freePaint = 0; for (int i = M - 1; i >= 0; --i) { if (mustPaint[C[i]].isEmpty()) { if (canPaint[C[i]] > 0) { ans[i] = canPaint[C[i]]; freePaint = canPaint[C[i]]; } else if (freePaint > 0) { ans[i] = freePaint; } else { io.println("NO"); return; } } else { int which = mustPaint[C[i]].removeLast(); ans[i] = which; freePaint = which; } } for (int i = 1; i <= N; ++i) { if (!mustPaint[i].isEmpty()) { io.println("NO"); return; } } io.println("YES"); io.printlnArray(ans); } public static class IntList { public int[] arr; public int pos; public IntList(int capacity) { this.arr = new int[capacity]; } public IntList() { this(10); } public IntList(IntList other) { this.arr = Arrays.copyOfRange(other.arr, 0, other.pos); this.pos = other.pos; } public void add(int x) { if (pos >= arr.length) { resize(arr.length << 1); } arr[pos++] = x; } public void insert(int i, int x) { if (pos >= arr.length) { resize(arr.length << 1); } System.arraycopy(arr, i, arr, i + 1, pos++ - i); arr[i] = x; } public int get(int i) { return arr[i]; } public void set(int i, int x) { arr[i] = x; } public void clear() { pos = 0; } public int size() { return pos; } public boolean isEmpty() { return pos == 0; } public int last() { return arr[pos - 1]; } public int removeLast() { return arr[--pos]; } public boolean remove(int x) { for (int i = 0; i < pos; ++i) { if (arr[i] == x) { System.arraycopy(arr, i + 1, arr, i, --pos - i); return true; } } return false; } public void removeAt(int i) { System.arraycopy(arr, i + 1, arr, i, --pos - i); } public IntList subList(int fromIndex, int toIndexExclusive) { IntList lst = new IntList(); lst.arr = Arrays.copyOfRange(arr, fromIndex, toIndexExclusive); lst.pos = toIndexExclusive - fromIndex; return lst; } public void forEach(IntConsumer consumer) { for (int i = 0; i < pos; ++i) { consumer.accept(arr[i]); } } public int[] toArray() { return Arrays.copyOf(arr, pos); } public static IntList of(int... items) { IntList lst = new IntList(items.length); System.arraycopy(items, 0, lst.arr, 0, items.length); lst.pos = items.length; return lst; } public String join(CharSequence sep) { StringBuilder sb = new StringBuilder(); joinToBuffer(sb, sep); return sb.toString(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); joinToBuffer(sb, ", "); sb.append(']'); return sb.toString(); } private void resize(int newCapacity) { arr = Arrays.copyOf(arr, newCapacity); } private void joinToBuffer(StringBuilder sb, CharSequence sep) { for (int i = 0; i < pos; ++i) { if (i > 0) { sb.append(sep); } sb.append(arr[i]); } } } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.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 nextString() { 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 nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private 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; } 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 printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
fc445460d95fcb1a3720fc6ef118472d
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 CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } void shuffle(long a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; long x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(int a, int b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long b[], int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args) throws IOException { new CodeForces().run(); } void run() throws IOException { in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; // t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { int n = in.nextInt(), m = in.nextInt(); int a[] = new int[n]; int req[] = new int[n]; int c[] = new int[m]; for (int j = 0; j < n; j++) a[j] = in.nextInt(); for (int j = 0; j < n; j++) req[j] = in.nextInt(); for (int j = 0; j < m; j++) c[j] = in.nextInt(); Map<Integer,Deque<Integer>>hm=new HashMap<>(); for(int i=0;i<n;i++) { if(!hm.containsKey(req[i])) hm.put(req[i],new ArrayDeque<>()); if(req[i]!=a[i]) hm.get(req[i]).add(i); } for(int i=0;i<n;i++) { if(hm.get(req[i]).size()==0) hm.get(req[i]).add(i); } int prev=0; for(int i=m-1;i>=0;i--) { if(i==m-1&&!hm.containsKey(c[i])) { o.println("NO");return; } if(!hm.containsKey(c[i])) c[i]=prev; else { if(hm.get(c[i]).size()==1) c[i]=hm.get(c[i]).peek()+1; else c[i]=hm.get(c[i]).poll()+1; a[c[i]-1]=req[c[i]-1]; prev=c[i]; } } for(int i=0;i<n;i++) { if(req[i]!=a[i]) { o.println("NO");return; } } o.println("YES"); for(int i=0;i<m;i++) o.print(c[i]+" "); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
d8943097583cd1d0b7534686baad2df7
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.text.DecimalFormat; import java.util.*; //import sun.jvm.hotspot.runtime.linux_aarch64.LinuxAARCH64JavaThreadPDAccess; public class B { static int mod= 998244353; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(), m=fs.nextInt(); int[] a=fs.readArray(n), b=fs.readArray(n), c=fs.readArray(m); Map<Integer,Stack<Integer>> need=new HashMap<>(); Map<Integer,Integer> waste=new HashMap<>(); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { if(!need.containsKey(b[i])) need.put(b[i],new Stack<>()); need.get(b[i]).push(i); } else { waste.put(a[i], i); } } int done=-1; int ans[]=new int[m]; for(int i=m-1;i>=0;i--) { int clr= c[i]; if(need.containsKey(clr)) { ans[i]=need.get(clr).pop()+1; done= ans[i]; if(need.get(clr).isEmpty()) need.remove(clr); } else if(done!=-1) { ans[i]=done; } else if(waste.containsKey(clr)) { ans[i]=waste.get(clr)+1; if(done==-1) done= ans[i]; } else { out.println("NO"); continue outer; } } if(!need.isEmpty()) { out.println("NO"); continue outer; } out.println("YES"); for(int ele:ans) out.print(ele+" "); out.println(); } out.close(); } static boolean check(int arr[]) { int max=Integer.MIN_VALUE; for(int i=0;i<arr.length;i++) { if(arr[i]<max) { return false; } max=arr[i]; } return true; } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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()); } long[] lreadArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
d4d375d919a3fb86b0619162b04da7f5
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 PAINTS{ //--------------------------INPUT READER--------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { 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 ns() { return next(); } int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);}; public void p(long l) {w.println(l);}; public void p(double d) {w.println(d);}; public void p(String s) { w.println(s);}; public void pr(int i) {w.println(i);}; public void pr(long l) {w.print(l);}; public void pr(double d) {w.print(d);}; public void pr(String s) { w.print(s);}; public void pl() {w.println();}; public void close() {w.close();}; } //------------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), p = sc.ni(); int[] arr = sc.na(n), fin = sc.na(n), ptr = sc.na(p), res = new int[p]; HashMap<Integer, ts<Integer>> hm = new HashMap<>(); for(int i = 0; i < p; i++) { int curr = ptr[i]; if(hm.containsKey(curr)) hm.get(curr).a(i); else { ts<Integer> ts = new ts<>(); ts.a(i); hm.put(curr, ts); } } int lastPaint = ptr[p-1]; int lastIdx = -1; for(int i = 0; i < n; i++) { if(fin[i] == lastPaint) { lastIdx = i; break; } } if(lastIdx == -1) { w.p("NO"); return; } for(int i = 0; i < n; i++) { if(arr[i]!=fin[i]) { if(!hm.containsKey(fin[i])) { w.p("NO"); return; } ts<Integer> curr = hm.get(fin[i]); int idxPtr = curr.f(); curr.r(curr.f()); if(curr.sz()==0) { hm.remove(fin[i]); } res[idxPtr] = i+1; } } for(int i = 0; i < p; i++) { if(res[i] != 0) continue; if(res[p-1] != 0) res[i] = res[p-1]; else res[i] = lastIdx+1; } w.p("YES"); for(int i = 0; i < p; i++) { w.pr(res[i]+" "); } w.pl(); } static class ts <T extends Comparable<T>> { TreeSet<T> ts = new TreeSet<>(); public void a(T elem) {ts.add(elem);} public boolean c(T elem) {return ts.contains(elem);} public int sz() {return ts.size();} public boolean r(T elem) {return ts.remove(elem);} public T f() {return ts.first();} public T l() {return ts.last();} public T ub(T elem) {return ts.higher(elem);} public T lb(T elem) {return ts.ceiling(elem);} public T fl(T elem) {return ts.floor(elem);} public T lo(T elem) {return ts.lower(elem);} } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
2d7098a228cf028cdb97894ee365bf92
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 com.sun.jndi.toolkit.url.GenericURLDirContext; //import com.sun.security.jgss.ExtendedGSSCredential; //import com.sun.source.tree.CompilationUnitTree; import java.io.*; public class Solution { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); static class pair{ int x;int y; pair(int x,int y){ this.x=x;this.y=y; } } static class triplet{ int x; int y; int z; triplet(int x,int y,int z){ this.x=x; this.y=y; this.z=z; } }static class DSU{ int []a=new int[26]; int []rank=new int[26]; void start() { for(int i=0;i<26;i++) { a[i]=i; } } int find(int x) { if(a[x]==x) { return x; } return find(a[x]); } void union(int x,int y) { int xx=find(x); int yy=find(y); a[xx]=yy; } } 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(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int[] sort(int a[]) { int multiplier = 1, len = a.length, max = Integer.MIN_VALUE; int b[] = new int[len]; int bucket[]; for (int i = 0; i < len; i++) if (max < a[i]) max = a[i]; while (max / multiplier > 0) { bucket = new int[10]; for (int i = 0; i < len; i++) bucket[(a[i] / multiplier) % 10]++; for (int i = 1; i < 10; i++) bucket[i] += (bucket[i - 1]); for (int i = len - 1; i >= 0; i--) b[--bucket[(a[i] / multiplier) % 10]] = a[i]; for (int i = 0; i < len; i++) a[i] = b[i]; multiplier *= 10; } return a; } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } int []reverse(int a[]){ int b[]=new int[a.length]; int index=0; for(int i=a.length-1;i>=0;i--) { b[index]=a[i]; } return b; } 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[] 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 solve() { int n=scr.nextInt(); int m=scr.nextInt(); HashMap<Integer,ArrayList<Integer>>hm=new HashMap<>(); int []a=scr.readArray(n); int []b=scr.readArray(n); int c[]=scr.readArray(m); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { ArrayList<Integer>temp=hm.getOrDefault(b[i], new ArrayList<>()); temp.add(i); hm.put(b[i], temp); } } int last=-1; if(hm.containsKey(c[m-1]) && hm.get(c[m-1]).size()>0) { last=hm.get(c[m-1]).get(0); hm.get(c[m-1]).remove(0); if(hm.get(c[m-1]).size()==0) { hm.remove(c[m-1]); } }else { for(int i=0;i<n;i++) { if(c[m-1]==b[i]) { last=i; break; } } } int ans[]=new int[m]; if(last==-1) { out.println("NO"); return; } ans[m-1]=last; for(int i=0;i<m-1;i++) { if( hm.containsKey(c[i]) ==false) { ans[i]=last; }else { ans[i]=hm.get(c[i]).get(0); hm.get(c[i]).remove(0); if(hm.get(c[i]).size()==0) { hm.remove(c[i]); } } } for(Integer i:hm.keySet()) { if(hm.get(i).size()>0) { out.println("NO"); return; } } out.println("YES"); for(int i:ans) { out.print((i+1)+" "); } out.println(); } static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String []args) { int t=scr.nextInt(); while(t-->0) { solve(); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
d9f4de497749f97e125ac17add3b2611
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Coder { static int n,m; static int a[],b[],c[]; static StringBuffer str=new StringBuffer(); static void solve(){ Map<Integer, Deque<Integer>> d=new HashMap<>(); Map<Integer, Integer> map=new HashMap<>(); for(int i=0;i<n;i++){ if(a[i]!=b[i]){ Deque<Integer> dq=d.getOrDefault(b[i], new ArrayDeque<Integer>()); dq.addLast(i+1); d.put(b[i], dq); } map.put(b[i], i+1); } // same case if(d.size()==0){ int ele=c[m-1]; for(int i=0;i<n;i++){ if(b[i]==ele){ str.append("YES\n"); int idx=i+1; for(int j=0;j<m;j++) str.append(idx).append(" "); str.append("\n"); return; } } str.append("NO\n");return; } int ans[]=new int[m]; int prev=-1; for(int i=m-1;i>=0;i--){ Deque<Integer> list=d.get(c[i]); if(list==null && prev==-1 && !map.containsKey(c[i])){ str.append("NO\n"); return; } if(list==null && prev==-1){ ans[i]=map.get(c[i]); prev=ans[i]; }else if(list==null || list.size()==0){ ans[i]=prev; }else{ prev=list.pollFirst(); ans[i]=prev; } } for(Map.Entry<Integer, Deque<Integer>> m:d.entrySet()){ if(m.getValue().size() > 0){ str.append("NO\n");return; } } str.append("YES\n"); for(int i=0;i<m;i++) str.append(ans[i]).append(" "); str.append("\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(bf.readLine().trim()); while(q-->0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); m=Integer.parseInt(s[1]); a=new int[n]; b=new int[n]; c=new int[m]; s=bf.readLine().trim().split("\\s+"); for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]); s=bf.readLine().trim().split("\\s+"); for(int i=0;i<n;i++) b[i]=Integer.parseInt(s[i]); s=bf.readLine().trim().split("\\s+"); for(int i=0;i<m;i++) c[i]=Integer.parseInt(s[i]); solve(); } System.out.print(str); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
622d70a75f018b7e58451b0f6885d755
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 final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static long mod = (long) 1e9 + 7; static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int tt = i(); out: while (tt-- > 0) { int n = i(); int m = i(); int[] a = input(n); int[] b = input(n); int[] c = input(m); PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); int lpp = -1; int lpp2 = -1; for (int i = 0; i < n; i++) { if (b[i] == c[m - 1] && a[i] != c[m - 1]) { lpp = i; } if (b[i] == c[m - 1]) { lpp2 = i; } } if (lpp == -1 && lpp2 == -1) { printNo(); continue; } lpp = lpp == -1 ? lpp2 : lpp; for (int i = 0; i < n; i++) { if (i == lpp) { continue; } if (a[i] != b[i]) { pq.offer(new int[]{i, b[i]}); } } int[] ans = new int[m]; ans[m - 1] = lpp + 1; PriorityQueue<int[]> color = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); for (int i = 0; i < m - 1; i++) { color.offer(new int[]{i, c[i]}); } while (!color.isEmpty()) { int[] cp = color.poll(); if (pq.isEmpty() || pq.peek()[1] > cp[1]) { ans[cp[0]] = lpp + 1; } else if (pq.peek()[1] == cp[1]) { ans[cp[0]] = pq.poll()[0] + 1; } else { printNo(); continue out; } } if (pq.size() > 0) { printNo(); continue; } printYes(); print(ans); } out.flush(); } static boolean isPS(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static int sd(long i) { int d = 0; while (i > 0) { d += i % 10; i = i / 10; } return d; } static int[] leastPrime; static void sieveLinear(int N) { int[] primes = new int[N]; int idx = 0; leastPrime = new int[N + 1]; for (int i = 2; i <= N; i++) { if (leastPrime[i] == 0) { primes[idx++] = i; leastPrime[i] = i; } int curLP = leastPrime[i]; for (int j = 0; j < idx; j++) { int p = primes[j]; if (p > curLP || (long) p * i > N) { break; } else { leastPrime[p * i] = p; } } } } static int lower(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] >= x) { r = m; } else { l = m; } } return r; } static int upper(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] <= x) { l = m; } else { r = m; } } return l; } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static int lowerBound(int A[], int low, int high, int x) { if (low > high) { if (x >= A[high]) { return A[high]; } } int mid = (low + high) / 2; if (A[mid] == x) { return A[mid]; } if (mid > 0 && A[mid - 1] <= x && x < A[mid]) { return A[mid - 1]; } if (x < A[mid]) { return lowerBound(A, low, mid - 1, x); } return lowerBound(A, mid + 1, high, x); } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static void printYes() { out.println("YES"); } static void printNo() { out.println("NO"); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } 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); } } } class BIT { int n; int[] tree; public BIT(int n) { this.n = n; this.tree = new int[n + 1]; } public static int lowbit(int x) { return x & (-x); } public void update(int x) { while (x <= n) { ++tree[x]; x += lowbit(x); } } public int query(int x) { int ans = 0; while (x > 0) { ans += tree[x]; x -= lowbit(x); } return ans; } public int query(int x, int y) { return query(y) - query(x - 1); } } class SegmentTree { long[] t; public SegmentTree(int n) { t = new long[n + n]; Arrays.fill(t, Long.MIN_VALUE); } public long get(int i) { return t[i + t.length / 2]; } public void add(int i, long value) { i += t.length / 2; t[i] = value; for (; i > 1; i >>= 1) { t[i >> 1] = Math.max(t[i], t[i ^ 1]); } } // max[a, b] public long max(int a, int b) { long res = Long.MIN_VALUE; for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) { if ((a & 1) != 0) { res = Math.max(res, t[a]); } if ((b & 1) == 0) { res = Math.max(res, t[b]); } } return res; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = j; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
50a240a36c3879353760084406dfae9b
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.Scanner; public class C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(" "); int cases = scan.nextInt(); while(cases-->0) { int n = scan.nextInt(); int m = scan.nextInt(); int[] a = new int[n+1]; int[] b = new int[n+1]; int[] c = new int[m+1]; ArrayList<Integer> arr[] = new ArrayList[n+1]; for (int i = 1; i <= n; i++) { arr[i] = new ArrayList<>(); } int[] ans = new int[m+1]; for (int i = 1; i <= m; i++) { ans[i] = 0; } for (int i = 1; i <= n; i++) { a[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { b[i] = scan.nextInt(); } for (int i = 1; i <= m; i++) { c[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { if(b[i] != a[i]) { arr[b[i]].add(i); } } int cm = c[m]; int last = -1; int i; if(arr[cm].size()==0) { for (i = 1; i <= n; i++) { if(b[i] == cm) { last = i; break; } } }else { last = arr[cm].get(0); arr[cm].remove(0); } if(last == -1) { System.out.println("NO"); continue; } ans[m] = last; for (int j = 1; j <= m - 1; j++) { if(arr[c[j]].size() > 0) { ans[j] = arr[c[j]].get(0); arr[c[j]].remove(0); }else { ans[j] = last; } } boolean flag = false; for (int j = 1; j <= n; j++) { if(arr[j].size() != 0 ) { flag = true; break; } } if(flag) { System.out.println("NO"); continue; } System.out.println("YES"); for (int j = 1; j <= m; j++) { System.out.print(ans[j]+" "); } System.out.println(); } scan.close(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
fc37ec69003774176191b436fddf6b37
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
//189301019.akshay import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Stack; import java.util.Random; import java.util.Arrays; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Collections; public class C { static class Pair{ int time,plank; Pair(){ } Pair(int t,int p){ time=t; plank=p; } } public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=sc.nextInt(); outer:while(test-->0) { int n=sc.nextInt(),m=sc.nextInt(); int arr[]=new int[n]; int brr[]=new int[n]; int crr[]=new int[m]; HashMap<Integer,Integer> colorsPlank=new HashMap<>(); HashMap<Integer,Queue<Integer>> demand =new HashMap<>(); HashSet<Integer> finalColors=new HashSet<>(); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); for(int i=0;i<n;i++) { brr[i]=sc.nextInt(); colorsPlank.put(brr[i], i+1); finalColors.add(brr[i]); if(arr[i] != brr[i]) { if(!demand.containsKey(brr[i])) { Queue<Integer> q=new LinkedList<>(); demand.put(brr[i], q); } demand.get(brr[i]).add(i+1); } } for(int i=0;i<m;i++) crr[i]=sc.nextInt(); int last =crr[m-1]; if(!finalColors.contains(last)) { ans.append("NO\n"); continue; } int universal=colorsPlank.get(last); if(demand.containsKey(last)) { universal=getLast(demand.get(last)); } else { Queue<Integer> q=new LinkedList<>(); q.add(universal); demand.put(last, q); } int res[]=new int[m]; boolean demandAdded=false; for(int i=0;i<m;i++) { int color =crr[i]; int plank =universal; if(demand.containsKey(color) && !demand.get(color).isEmpty() ) { plank =demand.get(color).poll(); } res[i]=plank; } for(Queue<Integer> demands:demand.values()) { if(!demands.isEmpty()) { ans.append("NO\n"); continue outer; } } ans.append("YES\n"); for(int i:res) { ans.append(i+" "); } ans.append("\n"); } System.out.print(ans); } static int getLast(Queue<Integer> li) { Queue<Integer> m=new LinkedList<>(); int lastM =0; while(!li.isEmpty()) { lastM=li.poll(); m.add(lastM); } while(!m.isEmpty()) { li.add(m.poll()); } return lastM; } static void print(int res[]) { for(int i:res) System.out.println(i+" "); System.out.println(); } 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 void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
9a753a556afc9d754ea6c6c16f801716
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.*; import java.util.*; public class C { public static int find(int []arr,int n,int k) { for(int i=0;i<n;i++) { if(arr[i]==k) { return i+1; } } return -1; } public static void main(String[] args) { FastScanner fs = new FastScanner(); FastOutput fo = new FastOutput(); int testcases = fs.nextInt(); test:for (int tt = 0; tt < testcases; tt++) { //main code int n=fs.nextInt(); int m=fs.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]=fs.nextInt(); for(int i=0;i<n;i++) b[i]=fs.nextInt(); for(int i=0;i<m;i++) c[i]=fs.nextInt(); Map<Integer,Deque<Integer>> map=new HashMap<>(); Map<Integer,Integer> size=new HashMap<>(); int ans[]=new int[m]; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { if(map.get(b[i])==null) { map.put(b[i],new LinkedList<>()); size.put(b[i],0); } map.get(b[i]).add(i+1); size.put(b[i],size.get(b[i])+1); } } if(!map.containsKey(c[m-1]) && find(b,n,c[m-1])==-1) { fo.println("NO"); } else { if(size.get(c[m-1])==null) { ans[m-1]=find(b,n,c[m-1]); } else { ans[m-1]=map.get(c[m-1]).removeFirst(); size.put(c[m-1],size.get(c[m-1])-1); } for(int i=m-2;i>-1;i--) { if(size.get(c[i])==null || size.get(c[i])==0) { ans[i]=ans[i+1]; } else { ans[i]=map.get(c[i]).removeFirst(); size.put(c[i],size.get(c[i])-1); } } for(int key:size.keySet()) { if(size.get(key)>0) { fo.println("NO"); continue test; } } fo.println("YES"); for(int i=0;i<m;i++) fo.print(ans[i]+" "); fo.println(""); } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class FastOutput { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); void print(String str) { try { bw.write(str); bw.flush(); } catch (IOException e) { } } void print(int num) { try { bw.write((num + "")); bw.flush(); } catch (IOException e) { } } void println(String str) { try { bw.write(str + '\n'); bw.flush(); } catch (IOException e) { } } void println(int num) { try { bw.write(num + "" + '\n'); bw.flush(); } catch (IOException e) { } } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
9d5738756c287d33fc5552573202eb85
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 { public static void main(String[] args)throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-->0) { String s[]=(br.readLine()).split(" "); int n=Integer.parseInt(s[0]); int m=Integer.parseInt(s[1]); String s1[]=(br.readLine()).split(" "); int a[]=new int[n]; int col[]=new int[n+1]; for(int i=0;i<n;i++) { a[i]=Integer.parseInt(s1[i]); } String s2[]=(br.readLine()).split(" "); int b[]=new int[n]; int ar2[]=new int[n+1]; for(int i=0;i<n;i++) { b[i]=Integer.parseInt(s2[i]); col[b[i]]++; ar2[b[i]]=i+1; } String s3[]=(br.readLine()).split(" "); int c[]=new int[m]; for(int i=0;i<m;i++) { c[i]=Integer.parseInt(s3[i]); } //int ar[]=new int[n+1];//no. of Bi's LinkedList<Integer> ar[]=new LinkedList[n+1]; for(int i=0;i<=n;i++) { ar[i]=new LinkedList<Integer>(); } for(int i=0;i<n;i++) { if(a[i]!=b[i]) { ar[b[i]].add(i+1); //ar2[b[i]]=i+1; } } int arc[]=new int[n+1]; for(int i=0;i<m;i++) { arc[c[i]]++; } LinkedList<Integer> waste=new LinkedList<Integer>(); boolean ok=true; int x[]=new int[m]; for(int i=1;i<=n;i++) { if((ar[i].size())>arc[i]) { ok=false; break; } else if(col[i]==0 && arc[i]>0) { waste.add(i); } } if(!ok) { pw.println("NO"); continue; } Iterator<Integer> it1=waste.iterator(); while(it1.hasNext()) { int num=(int)(it1.next()); if(num==c[m-1]) { ok=false; break; } else { //x[num]=n; // ar[num].add(-1); ar2[num]=-1; } } if(!ok) { pw.println("NO"); continue; } for(int i=0;i<m;i++) { int num; /*if(x[i]!=0) { continue; }*/ if((ar[c[i]].size())>0) { num=(int)(ar[c[i]].poll()); /* if(num==-1) { ar[c[i]].add(-1); }*/ } else { num=ar2[c[i]]; } x[i]=num; } for(int i=0;i<m;i++) { if(x[i]==-1) { x[i]=x[m-1]; } } pw.println("YES"); for(int i=0;i<m;i++) { pw.print(x[i]+" "); } pw.println(); } pw.flush(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
a1bcd7803e3abbdb2881272cd9b0da51
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 { public static void main(String[] args)throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-->0) { String s[]=(br.readLine()).split(" "); int n=Integer.parseInt(s[0]); int m=Integer.parseInt(s[1]); String s1[]=(br.readLine()).split(" "); int a[]=new int[n]; int col[]=new int[n+1]; for(int i=0;i<n;i++) { a[i]=Integer.parseInt(s1[i]); } String s2[]=(br.readLine()).split(" "); int b[]=new int[n]; int ar2[]=new int[n+1]; for(int i=0;i<n;i++) { b[i]=Integer.parseInt(s2[i]); col[b[i]]++; ar2[b[i]]=i+1; } String s3[]=(br.readLine()).split(" "); int c[]=new int[m]; for(int i=0;i<m;i++) { c[i]=Integer.parseInt(s3[i]); } //int ar[]=new int[n+1];//no. of Bi's LinkedList<Integer> ar[]=new LinkedList[n+1]; for(int i=0;i<=n;i++) { ar[i]=new LinkedList<Integer>(); } for(int i=0;i<n;i++) { if(a[i]!=b[i]) { ar[b[i]].add(i+1); //ar2[b[i]]=i+1; } } int arc[]=new int[n+1]; for(int i=0;i<m;i++) { arc[c[i]]++; } LinkedList<Integer> waste=new LinkedList<Integer>(); boolean ok=true; int x[]=new int[m]; for(int i=1;i<=n;i++) { if((ar[i].size())>arc[i]) { ok=false; break; } else if(col[i]==0 && arc[i]>0) { waste.add(i); } } if(!ok) { pw.println("NO"); continue; } Iterator<Integer> it1=waste.iterator(); while(it1.hasNext()) { int num=(int)(it1.next()); if(num==c[m-1]) { ok=false; break; } else { //x[num]=n; ar[num].add(-1); } } if(!ok) { pw.println("NO"); continue; } for(int i=0;i<m;i++) { int num; /*if(x[i]!=0) { continue; }*/ if((ar[c[i]].size())>0) { num=(int)(ar[c[i]].poll()); if(num==-1) { ar[c[i]].add(-1); } } else { num=ar2[c[i]]; } x[i]=num; } for(int i=0;i<m;i++) { if(x[i]==-1) { x[i]=x[m-1]; } } pw.println("YES"); for(int i=0;i<m;i++) { pw.print(x[i]+" "); } pw.println(); } pw.flush(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
6cb11adef8d47f358c6829e069d3f19d
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastScanner fs = new FastScanner(); FastOutput fo = new FastOutput(); int testcases = fs.nextInt(); for (int tt = 0; tt < testcases; tt++) { //main code int n=fs.nextInt(),m=fs.nextInt(); int init_color[]=new int[n]; int desi_color[]=new int[n]; int painters[]=new int[m]; for(int i=0;i<n;i++) init_color[i]=fs.nextInt(); for(int i=0;i<n;i++) desi_color[i]=fs.nextInt(); for(int i=0;i<m;i++) painters[i]=fs.nextInt(); Map<Integer,Deque<Integer>> map=new HashMap<>(); Map<Integer,Integer> size=new HashMap<>(); for(int i=0;i<n;i++) { if(desi_color[i]!=init_color[i]) { if(map.get(desi_color[i])==null) { map.put(desi_color[i],new LinkedList<>()); size.put(desi_color[i],0); } map.get(desi_color[i]).add(i+1); size.put(desi_color[i],size.get(desi_color[i])+1); } } boolean is=true; int index=-1; for(int i=0;i<n;i++) { if(painters[m-1]==desi_color[i]) { is=false; index=i+1; break; } } if(is) { fo.println("NO"); } else { int ans[]=new int[m]; if(map.get(painters[m-1])==null) { ans[m-1]=index; } else { ans[m-1]=map.get(painters[m-1]).removeFirst(); size.put(painters[m-1],size.get(painters[m-1])-1); } for(int i=m-2;i>-1;i--) { if(map.get(painters[i])==null || size.get(painters[i])==0) { ans[i]=ans[i+1]; } else { ans[i]=map.get(painters[i]).removeFirst(); size.put(painters[i],size.get(painters[i])-1); } } boolean z=false; for(int key:size.keySet()) { if(size.get(key)>0) { z=true; break; } } if(z) { fo.println("NO"); continue; } fo.println("YES"); for(int i=0;i<m;i++) { fo.print(ans[i]+" "); } fo.println(""); } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class FastOutput { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); void print(String str) { try { bw.write(str); bw.flush(); } catch (IOException e) { } } void print(int num) { try { bw.write((num + "")); bw.flush(); } catch (IOException e) { } } void println(String str) { try { bw.write(str + '\n'); bw.flush(); } catch (IOException e) { } } void println(int num) { try { bw.write(num + "" + '\n'); bw.flush(); } catch (IOException e) { } } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
160159bd4289ca55916e9acd38cc8908
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.TreeMap; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Khater */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CFencePainting solver = new CFencePainting(); solver.solve(1, in, out); out.close(); } static class CFencePainting { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int t = 1; t = sc.nextInt(); int tt = 0; while (t-- > 0) { tt++; int n = sc.nextInt(); int m = sc.nextInt(); int[] a = sc.nextIntArr(n); int[] b = sc.nextIntArr(n); int[] c = sc.nextIntArr(m); TreeMap<Integer, TreeSet<Integer>> tm = new TreeMap<>(); StringBuilder ans = new StringBuilder(); int[] idx = new int[n + 1]; Arrays.fill(idx, -1); for (int i = 0; i < n; i++) { idx[b[i]] = idx[b[i]] == -1 ? i + 1 : a[i] == b[i] ? idx[b[i]] : i + 1; if (a[i] == b[i]) continue; if (tm.containsKey(b[i])) { tm.get(b[i]).add(i + 1); } else { tm.put(b[i], new TreeSet<Integer>()); tm.get(b[i]).add(i + 1); } } if (idx[c[m - 1]] == -1) { pw.println("NO"); } else { int k = 0; for (int i = 0; i < m; i++) { if (tm.containsKey(c[i]) && tm.get(c[i]).size() > 0) { a[tm.get(c[i]).first() - 1] = c[i]; ans.append(tm.get(c[i]).pollFirst() + " "); k++; } else { a[idx[c[m - 1]] - 1] = c[i]; ans.append(idx[c[m - 1]] + " "); k++; } } boolean g = true; for (int i = 0; i < n; i++) g &= (a[i] == b[i]); // pw.println(Arrays.toString(a)); // pw.println(Arrays.toString(b)); if (g && k == m) { pw.println("YES"); pw.println(ans); } else { pw.println("NO"); } } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntArr(int n) { try { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } catch (Exception e) { throw new RuntimeException(e); } } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
e09feb4fc025f6f64f2b8ea1bfd93bb0
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 Main { static int i,j,k,n,m,t,y,x,sum; static long mod = 998244353; static FastScanner fs = new FastScanner(); static int[] arr = new int[105]; public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); t = fs.nextInt(); while(t-- >0){ n = fs.nextInt(); m = fs.nextInt(); int k = Math.max(n,m); int[] first = new int[n]; int[] last = new int[n]; int[] options = new int[m]; int[] ansArr = new int[m]; HashSet paints = new HashSet(); HashMap<Integer,Integer> hm = new HashMap<>(); List[] countList = new ArrayList[k+5]; for(i=0;i<=k;i++){ countList[i] = new ArrayList(); } for(i=0;i<n;i++){ first[i] = fs.nextInt(); } for(i=0;i<n;i++){ last[i] = fs.nextInt(); paints.add(last[i]); hm.put(last[i],i); if(first[i]!=last[i]) countList[last[i]].add(i); } List<Integer> headAche = new ArrayList<>(); for (i=0;i<m;i++){ options[i] =fs.nextInt(); if(!countList[options[i]].isEmpty()){ int index = (int) countList[options[i]].remove(countList[options[i]].size()-1); if(headAche.size()>0){ for(j=0;j<headAche.size();j++) ansArr[headAche.get(j)] = index+1; headAche.clear(); } ansArr[i] = index+1; } else if(paints.contains(options[i])){ int index = hm.get(options[i]); if(headAche.size()>0){ for(j=0;j<headAche.size();j++) ansArr[headAche.get(j)] = index+1; headAche.clear(); } ansArr[i] = index+1; } else{ headAche.add(i); } } if(headAche.size() >0 ){ out.println("NO"); } else { int f =0; for(i=0;i<=k;i++){ if(countList[i].size()>0) { f=1; break; } } if(f==1) out.println("NO"); else { out.println("YES"); for(i=0;i<m;i++) out.print(ansArr[i]+" "); out.println(); } } } out.close(); } 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()); } long nextLong() { return Long.parseLong(next()); } } static long exp(long base, long pow) { if (pow==0) return 1; long half=exp(base, pow/2); if (pow%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return a*b%mod; } static long add(long a, long b) { return (a+b)%mod; } static long modInv(long x) { return exp(x, mod-2); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
ff38c34d9fe95bd89333f04e28dfef82
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 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 s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(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] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if (n % 2 == 0) return false; //if not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int MOD=1000000007; static long mpow(long base,long pow) { long res=1; while(pow>0) { if(pow%2==1) { res=(res*base)%MOD; } pow>>=1; base=(base*base)%MOD; } return res; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t = 1; while (t-- > 0) { int n=ri(); int m=ri(); int[] a=rai(n); int[] b=rai(n); int[] c=rai(m); HashSet<Integer>[] toVal=new HashSet[n+1]; HashSet<Integer>[] hasAtA=new HashSet[n+1]; for(int i=0;i<=n;i++) { toVal[i]=new HashSet<>(); hasAtA[i]=new HashSet<>(); } for(int i=0;i<n;i++) { if(a[i]!=b[i]) { toVal[b[i]].add(i+1); } hasAtA[a[i]].add(i+1); } List<Integer> list = new ArrayList<>(); int[] res = new int[m]; for(int index = 0; index < m; index++) { int i = c[index]; if(toVal[i].size() > 0) { int val = -1; for(int j : toVal[i]) { val = j; break; } toVal[i].remove(val); while(list.size()>0) { int ind=list.remove(list.size()-1); res[ind]=val; } res[index] = val; int col = a[val-1]; a[val-1] = i; hasAtA[i].add(val); hasAtA[col].remove(val); } else { if(hasAtA[i].size() <= 0) { list.add(index); } else { int first = -1; for(int j : hasAtA[i]) { first = j; break; } while(list.size() > 0) { int ind = list.remove(list.size()-1); res[ind] = first; } res[index] = first; } } } if(list.size()<=0) { boolean f=true; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { f=false; break; } } if(f) { ans.append("YES\n"); for (int i : res) { ans.append(i).append(" "); assert i!=0; } ans.append("\n"); } else { ans.append("NO\n"); } } else { ans.append("NO\n"); } } out.print(ans.toString()); out.flush(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
43f1af5753232ca427c9008e9f73689a
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 C; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { private int n, t; 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 void Process() throws IOException { Reader scanner = new Reader(); t = scanner.nextInt(); int[] A = new int[100005]; int[] B = new int[100005]; int[] C = new int[100005]; int[] appcol = new int[100006]; int[] tocol = new int[100006]; int[] haspainted = new int[100005]; for (int ii = 0; ii < t; ii++) { n = scanner.nextInt(); int m; m = scanner.nextInt(); HashMap<Integer, Vector<Integer>> Map = new HashMap<Integer, Vector<Integer>>(); for (int i = 0; i < n; i++) { tocol[i] = -1; haspainted[i] = 0; appcol[i] = -1; A[i] = scanner.nextInt(); Map.put(i, new Vector<Integer>()); } appcol[n] = -1; tocol[n] = -1; haspainted[n] = 0; Map.put(n, new Vector<Integer>()); for (int i = 0; i < n; i++) { B[i] = scanner.nextInt(); appcol[B[i]] = i; if (B[i]!=A[i]) { Map.get(B[i]).add(i); } } for (int i = 0; i < m; i++) { C[i] = scanner.nextInt(); } int paintlast = -1; for (int i = m - 1; i >= 0; i--) { if (Map.get(C[i]).size() > 0) { A[Map.get(C[i]).get(0)] = C[i]; tocol[i] = Map.get(C[i]).get(0); if (paintlast == -1) { paintlast = Map.get(C[i]).get(0); } Map.get(C[i]).remove(0); } else { tocol[i] = paintlast; if (appcol[C[i]]!=-1) { tocol[i] = appcol[C[i]]; if (paintlast == -1) { paintlast = tocol[i]; } } } } int cond = 1; for (int i = 0; i < m; i++) { if (tocol[i] == -1) { cond = -1; } } for (int i = 0; i < n; i++) { if (A[i]!=B[i]) { cond = -1; } } if (cond == -1) { System.out.println("NO"); } else { System.out.println("YES"); for (int i = 0; i < m; i++) { System.out.print((tocol[i] + 1) + " "); } System.out.println(); } } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub solution s = new solution(); s.Process(); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
211a1a4c69987eb439ad097d96d058da
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 void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(T-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } int[] brr = new int[n]; for(int i = 0; i < n; i++){ brr[i] = sc.nextInt(); } LinkedList<Integer>[] d = new LinkedList[n+1]; for(int i = 0; i <= n; i++){ d[i] = new LinkedList<>(); } int tot = 0; for(int i = 0; i < n; i++){ if(arr[i] != brr[i]) { d[brr[i]].add(i); tot++; } } int[] crr = new int[m]; for(int i = 0; i < m; i++){ crr[i] = sc.nextInt(); } int last = -1; if(d[crr[m-1]].isEmpty()) { for(int i = 0; i < n; i++){ if(brr[i] == crr[m-1]) { last = i; break; } } } else { last = d[crr[m-1]].peekLast(); } if(last < 0) { sb.append("NO\n"); } else { StringBuilder sb2 = new StringBuilder(); for(int i = 0; i < m; i++){ if(!d[crr[i]].isEmpty()) { int p = d[crr[i]].pollFirst(); sb2.append(p+1+" "); tot--; } else { sb2.append(last+1+" "); } } if(tot > 0) sb.append("NO\n"); else { sb.append("YES\n"); sb.append(sb2); sb.replace(sb.length()-1, sb.length(), "\n"); } } } PrintWriter pw = new PrintWriter(System.out); pw.println(sb.toString().trim()); pw.flush(); } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 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 String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
ca85d0fdf28749ec77d7a674672584ad
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 B { static int diff = 0, same = 1; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(), m = sc.nextInt(); Queue<Integer>[][] adj = new Queue[2][n]; for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) adj[i][j] = new LinkedList(); int[] a = new int[n], b = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt() - 1; TreeSet<Integer> needChange = new TreeSet(); for (int i = 0; i < n; i++) { b[i] = sc.nextInt() - 1; int idx = a[i] != b[i] ? diff : same; adj[idx][b[i]].add(i); if (idx == diff) needChange.add(i); } int[] ans = new int[m]; ans[0] = -1; int[] c = new int[m]; for (int i = 0; i < m; i++) c[i] = sc.nextInt() - 1; int tmp = -1; for (int i = m - 1; i >= 0; i--) { int curr = c[i]; if (needChange.size() == 0) { if (adj[same][curr].size() == 0) { if (tmp == -1) break; ans[i] = tmp; continue; } ans[i] = adj[same][curr].peek(); tmp = ans[i]; } else if (adj[diff][curr].size() > 0) { ans[i] = adj[diff][curr].poll(); adj[same][curr].add(ans[i]); needChange.remove(ans[i]); tmp = ans[i]; } else { if (adj[same][curr].size() == 0) { if (tmp == -1) break; ans[i] = tmp; continue; } ans[i] = adj[same][curr].peek(); tmp = ans[i]; } } if (ans[0] == -1 || needChange.size() > 0) { out.println("NO"); } else { out.println("YES"); for (int x : ans) out.print(x + 1 + " "); out.println(); } } out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready() || st.hasMoreTokens(); } int[] nxtArr(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
4045779a45c68fc33ccbeb1be82aec80
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 { private static final MyWriter writer = new MyWriter(); private static final MyReader scan = new MyReader(); public static void main(String[] args) throws Exception { Main main = new Main(); // main.preProcessing(); int q = scan.nextInt(); while (q-- > 0) main.solve(); writer.close(); } void preProcessing() { } void solve() throws Exception { int n = scan.nextInt(); int m = scan.nextInt(); int[] start = new int[n]; int[] tar = new int[n]; int[] tar2 = new int[n+1]; Arrays.fill(tar2, -1); int[] color2 = new int[m]; LinkedList<Integer>[] color = new LinkedList[n+1]; for (int i = 0; i <= n; i++) { color[i] = new LinkedList<>(); } for (int i = 0; i < n; i++) { start[i] = scan.nextInt(); } for (int i = 0; i < n; i++) { int cur = scan.nextInt(); tar[i] = cur; tar2[cur] = i; } for (int i = 0; i < m; i++) { int cur = scan.nextInt(); color[cur].addLast(i); color2[i] = cur; } boolean beChanged = false; int[] res = new int[m]; Arrays.fill(res, -1); for (int i = 0; i < n; i++) { if (!color[tar[i]].isEmpty()) { beChanged = true; } if (start[i] != tar[i]) { if (!color[tar[i]].isEmpty()) { res[color[tar[i]].pollLast()] = i; } else { writer.println("NO"); return; } } } if (!beChanged) { writer.println("NO"); return; } int pre = -1; for (int i = m-1; i >= 0; i--) { if (res[i] == -1) { if (pre == -1) { if (tar2[color2[i]] != -1) { res[i] = tar2[color2[i]]; pre = res[i]; } else { writer.println("NO"); return; } } else { res[i] = pre; } } else { pre = res[i]; } } writer.println("YES"); for (int i = 0; i < m; i++) { writer.print(res[i] + 1 + " "); } writer.println(""); } } class MyWriter implements Closeable { private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); public void print(Object object) throws IOException { writer.write(object.toString()); } public void println(Object object) throws IOException { writer.write(object.toString());writer.write(System.lineSeparator()); } public void close() throws IOException { writer.close(); } } class MyReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null;}} public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine();if (nextLine == null) return false;tokenizer = new StringTokenizer(nextLine); }return true; } public String nextLine() { tokenizer = new StringTokenizer("");return innerNextLine(); } public String next() { hasNext();return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
992a37853e06599c04a6ee52da9db410
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.Scanner; public class FencePainting { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); k: while (t-- > 0) { int n = scan.nextInt(); int m = scan.nextInt(); int[] a = new int[n + 1]; int[] b = new int[n + 1]; ArrayList<Integer>[] need = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) { a[i] = scan.nextInt(); } for (int i = 1; i <= n; i++) { b[i] = scan.nextInt(); } for (int i = 0; i <= n; i++) { need[i] = new ArrayList<Integer>(); } for (int i = 1; i <= n; i++) { if (b[i] != a[i]) { need[b[i]].add(i); } } int[] c = new int[m + 1]; for (int i = 1; i <= m; i++) { c[i] = scan.nextInt(); } int last = -1; for (int i = 1; i <= n; i++) { if (b[i] == c[m] && a[i] != b[i]) { last = i; } } for (int i = 1; i <= n; i++) { if (b[i] == c[m] && last == -1) { last = i; } } if (last == -1) { System.out.println("NO"); continue k; } int[] pos = new int[n + 1]; int[] res = new int[m + 1]; for (int i = 1; i <= m; i++) { if (pos[c[i]] == need[c[i]].size()) { res[i] = last; a[last] = c[i]; } else { res[i] = need[c[i]].get(pos[c[i]]); a[need[c[i]].get(pos[c[i]])] = c[i]; ++pos[c[i]]; } } for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { System.out.println("NO"); continue k; } } System.out.println("YES"); for (int i = 1; i <= m; i++) { System.out.print(res[i] + " "); } System.out.println(); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
721d0e1eed2702ce3af6c710f149e5c2
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.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Random; public class Main { public static void main(String[] args) throws IOException { Fs = new FastReader(); out = new PrintWriter(System.out); int t = Fs.nextInt(); while (t-- > 0) { int n = Fs.nextInt(), m = Fs.nextInt(); int[] a = Fs.readArray(n), b = Fs.readArray(n), c = Fs.readArray(m); int yeToHoga = c[m - 1]; int f = -1; HashMap<Integer, LinkedList<Integer>> Hm = new HashMap<>(); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { Hm.computeIfAbsent(b[i], key -> new LinkedList<>()).add(i); } if (b[i] == yeToHoga) { f = i; } } if (Hm.containsKey(yeToHoga)) f = Hm.get(yeToHoga).pollLast(); int id[] = new int[m]; if (f != -1) { for (int i = 0; i < m; ++i) { if (!Hm.containsKey(c[i]) || Hm.get(c[i]).isEmpty()) { id[i] = f; } else { id[i] = Hm.get(c[i]).pollLast(); } a[id[i]] = c[i]; } } if (f != -1) { for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { f = -1; break; } } } if (f != -1) { out.println("YES"); for (int i = 0; i < m; ++i) out.print((id[i] + 1) + " "); out.println(); } else out.println("NO"); } Fs.close(); out.close(); } static PrintWriter out; static FastReader Fs; static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length; for (int i = 0; i < n; ++i) { int oi = random.nextInt(n), tmp = a[oi]; a[oi] = a[i]; a[i] = tmp; } Arrays.sort(a); } static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextLine() throws IOException { byte[] buf = new byte[64]; // 64 line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String next() throws IOException { byte[] buf = new byte[64]; // 64 line length int cnt = 0, c; while ((c = read()) != -1) { if (c == ' ' || c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } public int[] readArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = nextInt(); return a; } 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
78ba9505c727601bcd1d41754f17114c
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 prac1500_1700; import java.io.IOException; import java.io.InputStream; import java.util.InputMismatchException; import java.util.*;; public class Painting { public static void main(String[] args) { // TODO Auto-generated method stub InputReader in=new InputReader(System.in); StringBuffer str=new StringBuffer(); Solver p=new Solver(); int t=in.nextInt(); for(int i=0;i<t;i++) { int n=in.nextInt(); int m=in.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]=in.nextInt(); } for(int j=0;j<n;j++) { b[j]=in.nextInt(); } for(int j=0;j<m;j++) { c[j]=in.nextInt(); } p.solve(n, m, a, b, c); } } // class for solving the problem static class Solver { public void solve(int n,int m,int a[],int b[],int c[]) { // int d[]=new int[100001]; HashMap<Integer,Queue<Integer>> pk=new HashMap<>(); int no=c[m-1]; int index=-1; for(int j=0;j<n;j++) { if(b[j]==no&&index==-1) { index=j; } if(b[j]!=a[j]) { Queue<Integer> temp=pk.get(b[j]); if(temp==null) { temp=new LinkedList<>(); pk.put(b[j],temp); } // else { // if(temp.peek()==-1) { // temp.poll(); // } // } temp.add(j); } // else { // //d[b[j]]=j; // Queue<Integer> temp=pk.get(b[j]); // if(temp==null) { // temp=new LinkedList<>(); // pk.put(b[j],temp); // temp.add(-1); // } // } } Queue<Integer> ans=new LinkedList<>(); if(index==-1) { System.out.println("NO"); return; } for(int i=0;i<m;i++) { int t=c[i]; Queue<Integer> temp=pk.get(c[i]); if(temp==null) { Queue<Integer> temp1=pk.get(no); if(temp1!=null) { ans.add(temp1.peek()); } else { ans.add(index); } } else { int z=temp.poll(); ans.add(z); if(temp.size()==0) { pk.remove(c[i]); } } } // for(int i=0;i<m;i++) { // Queue<Integer> find=pk.get(c[i]); // if(find!=null) { // ans.add(find.poll()); // if(find.size()==0) { // pk.remove(c[i]); // } // } // else { // if(i==m-1) { // int x=d[c[i]]; // if(a[x]==c[i]) { // ans.add(x); // } // else { // System.out.println("NO"); // return; // } // } // else { // find=pk.get(c[i+1]); // if(find==null) { // int x=d[c[i+1]]; // if(a[x]!=c[i+1]) { // System.out.println("NO"); // return; // } // else { // ans.add(x); // ans.add(x); // i++; // } // } // } // } // // } if(pk.size()!=0) { System.out.println("No"); return; } System.out.println("YES"); while(!ans.isEmpty()) { System.out.print(ans.poll()+1+" "); } System.out.println(); } // Function to take out all prime no less than n void all_prime_no_less_than_n(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; } } } } // class for taking fast input static class InputReader{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public void skip(int x) { while (x-- > 0) read(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextString() { return next(); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean hasNext() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value != -1; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || 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 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
264a57681541a68db56e3afe96afe3af
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.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.locks.LockSupport; public class Main { static class Task { public static String roundS(double result, int scale) { String fmt = String.format("%%.%df", scale); return String.format(fmt, result); // DecimalFormat df = new DecimalFormat("0.000000"); // double result = Double.parseDouble(df.format(result)); } int rt(int x) { if (x != fa[x]) { int to = rt(fa[x]); dp[x] ^= dp[fa[x]]; fa[x] = to; return to; } return x; } void combine(int x, int y, int val) { int rt1 = rt(x); int rt2 = rt(y); if (rt1 == rt2) return; fa[rt1] = rt2; dp[rt1] = dp[x] ^ dp[y] ^ val; g--; } int fa[], dp[]; int g; static int MAXN = 10000; static Random rd = new Random(348957438574659L); static int[] ch[],val,size,rnd,cnt; static int len=0,rt=0; // return new node, the node below s static int rotate(int s,int d){ // child int x=ch[s][d^1]; // give me grandson ch[s][d^1] = ch[x][d]; // child become father ch[x][d]=s; // update size, update new son first update(s); update(x); return x; } static void update(int s){ size[s] = size[ch[s][0]] + size[ch[s][1]] + cnt[s]; } // 0 for left, 1 for right static int cmp(int x,int num) { if (val[x] == num) return -1; return num < val[x] ? 0 : 1; } static int insert(int s,int num) { if(s==0){ s=++len; val[s]=num;size[s]=1;rnd[s]=rd.nextInt();cnt[s] = 1; }else{ int d=cmp(s,num); if(d!=-1) { ch[s][d]=insert(ch[s][d],num); // father's random should be greater if(rnd[s]<rnd[ch[s][d]]) { s=rotate(s,d^1); }else { update(s); } }else{ ++cnt[s];++size[s]; } } return s; } static int del(int s,int num) { int d = cmp(s, num); if (d != -1) { ch[s][d] = del(ch[s][d], num); update(s); }else if (ch[s][0] * ch[s][1] == 0) { if(--cnt[s]==0){ s = ch[s][0] + ch[s][1]; } } else { int k = rnd[ch[s][0]] < rnd[ch[s][1]] ? 0 : 1; // k points to smaller random value,then bigger one up s = rotate(s, k); // now the node with value num become the child ch[s][k] = del(ch[s][k], num); update(s); } return s; } static int getKth(int s, int k){ int lz = size[ch[s][0]]; if(k>=lz+1&&k<=lz+cnt[s]){ return val[s]; }else if(k<=lz){ return getKth(ch[s][0], k); }else{ return getKth(ch[s][1], k-lz-cnt[s]); } } // value可能不在树中出现,定义为比自己小的数的个数+1 static int getRank(int s,int value){ if(s==0)return 1; if(value==val[s])return size[ch[s][0]] + 1; if(value<val[s])return getRank(ch[s][0],value); return getRank(ch[s][1],value)+size[ch[s][0]] + cnt[s]; } static int getPre(int data){ int ans= -1; int p=rt; while(p>0){ if(data>val[p]){ if(ans==-1 || val[p]>val[ans]) ans=p; p=ch[p][1]; } else p=ch[p][0]; } return ans!=-1?val[ans]:(-2147483647); } static int getNext(int data){ int ans= -1; int p=rt; while(p>0){ if(data<val[p]){ if(ans==-1 || val[p]<val[ans]) ans=p; p=ch[p][0]; } else p=ch[p][1]; } return ans!=-1?val[ans]:2147483647; } static boolean find(int s,int num){ while(s!=0){ int d=cmp(s,num); if(d==-1) return true; else s=ch[s][d]; } return false; } static int ans = -10000000; static boolean findX(int s,int num){ while(s!=0){ if(val[s]<=num){ ans = num; } int d=cmp(s,num); if(d==-1) return true; else { s = ch[s][d]; } } return false; } public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); ot: for(int i=0;i<t;++i){ int n = in.nextInt(); int m = in.nextInt(); int a[] = in.nextArray(n); int b[] = in.nextArray(n); int c[] = in.nextArray(m); int use[] = new int[n+1]; Arrays.fill(use,-1); ArrayDeque li[] = new ArrayDeque[n+1]; for(int j=1;j<=n;++j){ li[j] = new ArrayDeque(); } int nd = 0; for(int j=0;j<n;++j){ if(a[j]==b[j]){ use[a[j]] = j; }else{ li[b[j]].add(j); nd++; } } int idx = -1; boolean ok = true; int f[] = new int[m]; for(int j=m-1;j>=0;--j){ int cur = c[j]; if(li[cur].size()>0){ int pos = (int)li[cur].poll(); f[j] = pos; idx = pos;nd--; }else if(use[cur]>=0){ f[j] = use[cur]; idx = use[cur]; }else if(idx!=-1){ f[j] = idx; }else{ ok = false;break; } } ok &= (nd==0); if(ok){ out.println("YES"); for(int j=0;j<m;++j){ if(j>0){ out.print(" "); } out.print(f[j]+1); } out.println(); }else{ out.println("NO"); } } // while(true) { // int n = in.nextInt(); // // int m =in.nextInt(); // // fa = new int[n]; // dp = new int[n]; // for(int i=0;i<n;++i){ // fa[i] = i; // } // g = n; // int c = 0; // int as[] = new int[n]; // int bs[] = new int[n]; // char xs[] = new char[n]; // // int at = -1; // Set<Integer> st = new HashSet<>(); // // for (int i = 0; i < n; ++i) { // String line = in.next(); // int p = 0; // int a = 0; // while(Character.isDigit(line.charAt(p))){ // a = a*10 + (line.charAt(p)-'0'); p++; // } // char x = line.charAt(p++); // // int b = 0; // while(p<line.length()){ // b = b*10 + (line.charAt(p)-'0'); p++; // } // // as[i] = a; // xs[i] = x; // bs[i] = b; // // if(x=='='){ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]!=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, 0); // } // }else if(x=='<'){ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]>=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, -1); // } // }else{ // int r1 = rt(a); int r2 = rt(b); // if(r1==r2){ // if(dp[a]<=dp[b]){ // c++; // at = i; // } // }else { // combine(a, b, 1); // } // } // // // } // if(g==1||c>=2){ // out.println("Impossible"); // continue; // } // // // for(int xuan: st){ // // // // // } // // // // // // // } } static long mul(long a, long b, long p) { long res=0,base=a; while(b>0) { if((b&1L)>0) res=(res+base)%p; base=(base+base)%p; b>>=1; } return res; } static long mod_pow(long k,long n,long p){ long res = 1L; long temp = k%p; while(n!=0L){ if((n&1L)==1L){ res = mul(res,temp,p); } temp = mul(temp,temp,p); n = n>>1L; } return res%p; } public static double roundD(double result, int scale){ BigDecimal bg = new BigDecimal(result).setScale(scale, RoundingMode.UP); return bg.doubleValue(); } } private static void solve() { InputStream inputStream = System.in; //InputStream inputStream = new FileInputStream(new File("D:\\chrome_download\\travel1.in")); OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { // new Thread(null, () -> solve(), "1", (1 << 10 ) ).start(); solve(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } 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 char nextChar() { return next().charAt(0); } public int[] nextArray(int n) { int res[] = new int[n]; for (int i = 0; i < n; ++i) { res[i] = nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output
PASSED
25333ee3dc43ae3a1ebb5d63a1eb9af7
train_107.jsonl
1612535700
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static int[] a, b, c; static ArrayList<Integer> d; static int dSize; static HashMap<Integer, HashMap<Integer, Integer>> hm; static int[] x; public static void printHashMap() { //HashMap<Integer, HashMap<Integer, Integer>> hm //print hm for (Integer key : hm.keySet()) { int cEle = key.intValue(); HashMap<Integer, Integer> hs = hm.get(cEle); System.out.print(cEle + " -> ["); for (Integer key2 : hs.keySet()) { int i = key2.intValue(); int cInd = hs.get(i); System.out.print(i + " -> {" + cInd + "}, "); } System.out.print("], "); } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(in.readLine()); while (T-- >0) { boolean poss = true; String[] str = (in.readLine()).split(" "); int N = Integer.parseInt(str[0]); int M = Integer.parseInt(str[1]); a = new int[N]; b = new int[N]; HashMap<Integer, Integer> bMap = new HashMap<>(); //bMap = {b[i], i}, so the values will range from 0 to N-1 //and if multiple b[i]'s equal each other, then only the highest i //will be contained as a value in bMap //bMap.get(ele) gives you the highest indexed i such that b[i] = ele c = new int[M]; d = new ArrayList<>(); dSize = 0; hm = new HashMap<>(); HashMap<Integer, Integer> hs; int hsSize = 0; x = new int[M]; Arrays.fill(x, -1); str = (in.readLine()).split(" "); for (int i=0; i<N; i++) { a[i] = Integer.parseInt(str[i]); } str = (in.readLine()).split(" "); for (int i=0; i<N; i++) { b[i] = Integer.parseInt(str[i]); if (b[i] != a[i]) { d.add(i); dSize++; } bMap.put(b[i], i); } str = (in.readLine()).split(" "); int curSize = 0; for (int i=0; i<M; i++) { c[i] = Integer.parseInt(str[i]); if (hm.containsKey(c[i])) { hs = hm.get(c[i]); curSize = hs.size(); } else { hs = new HashMap<>(); curSize = 0; } hs.put(curSize, i); hm.put(c[i], hs); } if (M < dSize) { poss = false; System.out.println("NO"); continue; } int realI = -1; //printHashMap(); int lastSetI = -1; for (int ind=0; ind<dSize; ind++) { realI = d.get(ind); //hs = hm.get(b[realI]); if (!hm.containsKey(b[realI])) { //means that no elements of c are equal to b[i] //and since we already know that all i we cover have //a[i] != b[i] //this means that there's no way to paint the current a[i] //to make it equal to b[i] //therefore we should break and print "NO" poss = false; break; } hs = hm.get(b[realI]); hsSize = hs.size(); //hs is a hasmap //if a pair of hm is {9, hs} where hs = {{0,2},{1,5}} //this represents the fact that c[2] = 9 and c[5] = 9 //arbitrarily? choose to remove the last pair in hs int cInd = hs.get(hs.size()-1); hs.remove(hs.size()-1); if (hsSize == 1) { hm.remove(b[realI]); } else { hm.put(b[realI], hs); } x[cInd] = realI; if (cInd > lastSetI) { lastSetI = cInd; } } //System.out.println("x after real needed assigning: x=" + Arrays.toString(x)); int realLastSetI = lastSetI; for (int i=0; i<M; i++) { if (x[i] == -1) { if (i < lastSetI) { //lastSetI is the last index such that x[lastSetI] != -1 //in other words, x[lastSetI] is assigned above //technically, we don't need i to have < lastSetI //if i < lastSetI, then there is also at least one element of b //such that b ele = c[i] x[i] = x[lastSetI]; if (i > realLastSetI) { realLastSetI = i; } continue; } //x[i] = lastI; //if we have extra painters //that we end up not needing to use (in assigning x ele's above) //Let's call E the set of all those extra painters that also //have no needed painters to the right of them. //Then, if for at least one element e of E there are no element indices k in the b array //such that b[k] = e //Then, and only then, is poss false /* if (!bMap.containsKey(c[i])) { poss = false; break; } */ if (!bMap.containsKey(c[i])) { continue; } int k = bMap.get(c[i]); x[i] = k; if (i > realLastSetI) { realLastSetI = i; } } } for (int i=M-1; i>-1; i--) { if (x[i] == -1) { if (i < realLastSetI) { x[i] = x[realLastSetI]; continue; } poss = false; break; } } if (!poss) { System.out.println("NO"); continue; //continue to next test case } //int lastI = x[d[dSize-1]]; System.out.println("YES"); for (int i=0; i<M; i++) { System.out.print(x[i]+1); if (i < M-1) { System.out.print(" "); } } System.out.println(); } } } /* 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 */
Java
["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"]
2 seconds
["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"]
null
Java 8
standard input
[ "brute force", "constructive algorithms", "greedy" ]
a350430c707bb18a146df9f80e114f45
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.
1,600
For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).
standard output