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
sequencelengths
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
bd260c0c396466f639e8b0a82fc19390
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (<) and the digit three (3). After applying the code, a test message looks like that: <3word1<3word2<3 ... wordn<3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.*; public class Main { void solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] list = new String[n]; for( int i = 0; i < n; ++i ) { list[i] = sc.next(); } String letter = sc.next(); String heart = "<3"; int i = 0; int p = 0; while( p < letter.length() && i < list.length ) { int j; j = 0; while( j < heart.length() && p < letter.length() ) { if( heart.charAt(j) == letter.charAt(p) ) { ++j; } ++p; } j = 0; while( j < list[i].length() && p < letter.length() ) { if( list[i].charAt(j) == letter.charAt(p) ) { ++j; } ++p; } if( j == list[i].length() ) { ++i; } } int j; j = 0; while( j < heart.length() && p < letter.length() ) { if( heart.charAt(j) == letter.charAt(p) ) { ++j; } ++p; } boolean flag = i == list.length && j == 2; System.out.println( flag ? "yes" : "no" ); sc.close(); } public static void main(String[] args) { new Main().solve(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
30966e88da4aa7067d0c29cbdca67c7d
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; import java.util.*; public class code1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while (--t>=0) { int n=sc.nextInt(); long x=sc.nextInt(); long[]z=new long[n]; for (int i=0;i<n;i++) { z[i]=sc.nextInt(); long v=1; long sum=0; if (z[i]>x) { for (int j=0;j*x<z[i];j++) { v=j; } sum=x*(v+1)-z[i]; z[i]=sum; } else { sum=x*v-z[i]; z[i]=sum;} } Arrays.sort(z); int j=0; long sum=0; long max=0; for (int i=0;i<n-1;i++) { if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) { j++; } else { if (i==n-2&&z[i]==z[i+1]) j++; sum=z[i]+j*x+1; if (z[i]!=z[i+1]&&i==n-2) if (sum<z[i+1]+1) sum=z[i+1]+1; if (z[i]==0&&z[i+1]==0) { sum=0; } if (max <sum) { max=sum; } j=0; } } if (n==1&&z[0]!=0) max=z[0]+1; System.out.println(max); } }}
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
065b6642c11e2cac7e489ba8cfc1757f
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class ZeroArray { public long n; public long k; public long[] a; public ZeroArray(long n, long k, long[] a) { this.n = n; this.k = k; this.a = a; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); long N = scan.nextLong(); for (long i = 0; i < N; i++) { long n = scan.nextLong(); long k = scan.nextLong(); long[] a = new long[(int) n]; for (long j = 0; j < n; j++) { a[(int) j] = scan.nextLong(); } ZeroArray p = new ZeroArray(n, k, a); System.out.println(p.solve()); } } public long solve() { // Calculate dif array long[] dif = new long[(int) n]; PriorityQueue<Long> pq = new PriorityQueue<>(); for (int i = 0; i < dif.length; i++) { dif[i] = (a[i] % k) == 0 ? 0 : k - (a[i] % k); pq.add(dif[i]); } long x = 0; long numMoves = 0; while (!pq.isEmpty()) { long nextdif = pq.poll(); if (nextdif != 0) { numMoves += nextdif - x; numMoves++; x = nextdif + 1; Long skipdif = pq.peek(); long m = 1; while (skipdif != null && skipdif == nextdif) { long movedif = pq.poll(); pq.add(movedif + m * k); m++; skipdif = pq.peek(); } } } return numMoves; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
9f6af1f4878c9ecbebf9bf6ab4d39fd1
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String commandLineArgument[]){ FS input = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = input.nextInt(); while(tc --> 0){ int n = input.nextInt(); long k = input.nextLong(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; ++i){ a.add(input.nextLong()); } Collections.sort(a); Map<Long , Boolean> highestStep = new TreeMap<>(); Map<Long, Long> sameTracker = new TreeMap<>(); long maxValue = 0; for(int i = 0; i < n; ++i){ long value = a.get(i); if(value % k != 0){ Long curNext = sameTracker.get(value); if(curNext == null){ curNext = ((long)Math.ceil((double)value/(double)k)) * k; } long req = curNext - value; while(highestStep.containsKey(req)){ curNext += k; req = curNext - value; } highestStep.put(req , true); sameTracker.put(value , curNext); maxValue = Math.max(maxValue , req); } } if(maxValue != 0){ ++maxValue; } out.println(maxValue); } out.close(); } } class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
45130fcb8fbda5bbb9da276caa5f1721
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String commandLineArgument[]){ FS input = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = input.nextInt(); while(tc --> 0){ int n = input.nextInt(); long k = input.nextLong(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; ++i){ a.add(input.nextLong() % k); } Map<Long, Long> sameTracker = new TreeMap<>(); long maxValue = 0; for(int i = 0; i < n; ++i){ long value = a.get(i); if(value != 0){ Long curNext = sameTracker.getOrDefault(value , 0l); ++curNext; sameTracker.put(value , curNext); maxValue = Math.max(maxValue , (curNext * k) - value); } } if(maxValue != 0){ ++maxValue; } out.println(maxValue); } out.close(); } } class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
9ee6773d7f340c76c459d936647faa86
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String commandLineArgument[]){ FS input = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = input.nextInt(); while(tc --> 0){ int n = input.nextInt(); long k = input.nextLong(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; ++i){ a.add(input.nextLong()); } Collections.sort(a); Map<Long , Boolean> highestStep = new TreeMap<>(); Map<Long, Long> sameTracker = new TreeMap<>(); long maxValue = 0; for(int i = 0; i < n; ++i){ long value = a.get(i); if(value % k != 0){ Long curNext = sameTracker.get(value); if(curNext == null){ curNext = ((long)Math.ceil((double)value/(double)k)) * k; } long req = curNext - value; while(highestStep.containsKey(req)){ curNext += k; req = curNext - value; } highestStep.put(req , true); sameTracker.put(value , curNext); maxValue = Math.max(maxValue , req); } } if(maxValue != 0){ ++maxValue; } out.println(maxValue); } out.close(); } } class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
ed43b3e4c5b5a79bd70116dba94ce6be
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String commandLineArgument[]){ FS input = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = input.nextInt(); while(tc --> 0){ int n = input.nextInt(); long k = input.nextLong(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; ++i){ a.add(input.nextLong()); } Map<Long , Boolean> highestStep = new TreeMap<>(); Map<Long, Long> sameTracker = new TreeMap<>(); long maxValue = 0; for(int i = 0; i < n; ++i){ long value = a.get(i); if(value % k != 0){ Long curNext = sameTracker.get(value); if(curNext == null){ curNext = ((long)Math.ceil((double)value/(double)k)) * k; } long req = curNext - value; while(highestStep.containsKey(req)){ curNext += k; req = curNext - value; } highestStep.put(req , true); sameTracker.put(value , curNext); maxValue = Math.max(maxValue , req); } } if(maxValue != 0){ ++maxValue; } out.println(maxValue); } out.close(); } } class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
b73033a8544287dfb2b8cde3d910b85c
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String commandLineArgument[]){ FS input = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = input.nextInt(); while(tc --> 0){ int n = input.nextInt(); long k = input.nextLong(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; ++i){ a.add(input.nextLong() % k); } Collections.sort(a); Map<Long, Long> sameTracker = new TreeMap<>(); long maxValue = 0; for(int i = 0; i < n; ++i){ long value = a.get(i); if(value != 0){ Long curNext = sameTracker.getOrDefault(value , 0l); ++curNext; sameTracker.put(value , curNext); maxValue = Math.max(maxValue , (curNext * k) - value); } } if(maxValue != 0){ ++maxValue; } out.println(maxValue); } out.close(); } } class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
4c5268f0dd57643528b83027c6ec88c8
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class stone { public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); BufferedWriter w = new BufferedWriter(new PrintWriter(System.out)); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n = sc.nextInt(); long k = sc.nextLong(); long a[] = new long[n]; //long b[] = new long[(int)k]; TreeMap <Long,Long> b1=new TreeMap(); long moves = 0; long min=Long.MAX_VALUE; for (int j = 0; j < n; j++) { a[j] = sc.nextLong(); long rem = a[j] % k; if(rem==0) { if(b1.containsKey((long)0)) b1.put((long)0,b1.get((long)0)+1); else b1.put((long)0,(long)1); } else if(b1.containsKey(k-rem)) { b1.put(k-rem,b1.get(k-rem)+1); } else { b1.put(k-rem,(long)1); } } // w.write(b1+"\n"); if(b1.size()==1&&b1.containsKey((long)0)) { moves=0; } else { boolean flag = false; for (long x = 0; !flag; ) { // System.out.println("X: "+x+" moves "+moves); //System.out.println(b1); if (b1.size() > 0) { if (x!=0&&b1.containsKey(x)) { if (b1.get(x) > 1) { moves++; b1.put(x + k, b1.get(x) - 1); b1.remove(x); x++; } else if (b1.get(x) == 1) { moves++; b1.remove(x); x++; } } else { if(x==0) { b1.remove((long)0); } moves+=b1.firstKey()-x; x=b1.firstKey(); continue; } } else { flag = true; } } } w.write(moves+"\n"); } w.close(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws NumberFormatException,IOException,NullPointerException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt()throws NumberFormatException,IOException,NullPointerException { return Integer.parseInt(next()); } long nextLong()throws NumberFormatException,IOException,NullPointerException { return Long.parseLong(next()); } double nextDouble()throws NumberFormatException,IOException,NullPointerException { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
c1644528b2b3834021ca6ea0ae2564f4
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; public class codeforces { public static void main(String[] args) { scanner in = new scanner(); int t = in.nextInt(); for(int i = 0;i<t;i++) { int n = in.nextInt(); long k = in.nextInt(); long[] a = new long[n+1]; long max = 0; long no = 0; int[] count = new int[n+1]; for(int j = 1;j<=n;j++) { a[j] = in.nextInt(); if(a[j]%k==0) a[j] = 0; else { a[j] = k-(a[j]%k); } } Arrays.sort(a); for(int j = 1;j<=n;j++) { if(a[j]!=0) { if(a[j]==a[j-1]) count[j] = count[j-1]+1; else count[j] = 1; } else count[j] = 0; if(max<=count[j]) { max = count[j]; no = a[j]; } } long ans = no; if(no!=0) ans = no+(max-1)*k+1; System.out.println(ans); } } } class scanner { BufferedReader br ; StringTokenizer st; public scanner() { br = new BufferedReader(new InputStreamReader(System.in)); st = null; } 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()); } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
f773cccb3b419416517ebdcdaac2edba
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class CodeForcesZeroRemainderArray { public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t--!=0) { String s[]=br.readLine().split(" "); int n=Integer.parseInt(s[0]); long k1=Long.parseLong(s[1]); BigInteger k=new BigInteger(s[1]); String st[]=br.readLine().split(" "); HashMap<BigInteger,BigInteger> hm= new HashMap<>(); BigInteger count; BigInteger one=new BigInteger("1"); BigInteger ans=new BigInteger("0"); for(int i=0;i<n;i++) { long x=Long.parseLong(st[i]); if(x % k1==0) continue; long ke= (k1-(x % k1)); String bs=""; bs+=ke; BigInteger key=new BigInteger(bs); //System.out.println(key+" "+ke); if(hm.containsKey(key)) { count=hm.get(key); count=count.add(one); hm.replace(key, count); } else hm.put(key, one); BigInteger val=hm.get(key).subtract(one); val=val.multiply(k); key=(val.add(key)); if(key.add(one).compareTo(ans)==1) ans=key.add(one); } System.out.println(ans); } } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
e25f1251b100ee0ac531900020db8071
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
//package com.company; //import com.sun.tools.corba.se.idl.StructEntry; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main{ public static void main(String[] args) throws IOException { BufferedReader read=new BufferedReader(new InputStreamReader(System.in)); String s=read.readLine(); int t=Integer.parseInt(s); for(int i=0;i<t;i++){ String st=read.readLine(); String[] ss=st.split(" "); int n=Integer.parseInt(ss[0]); int k=Integer.parseInt(ss[1]); String sd=read.readLine(); String[] sds=sd.split(" "); // System.out.println(sds.length); int[] arr=new int[n]; ArrayList<Integer> flag=new ArrayList<Integer>(); for(int j=0;j<n;j++){ arr[j]=Integer.parseInt(sds[j]); arr[j]=arr[j]%k; if(arr[j]==0) flag.add(1); } Arrays.sort(arr); int maxf=0; int prev=-1; int maxn=Integer.MAX_VALUE; int curr=0; HashSet<Integer> ab=new HashSet<Integer>(); for(int j=0;j<n;j++) { // System.out.println(arr[j]+" "+curr+" "+maxf); if(arr[j]!=0){ ab.add(arr[j]); if(arr[j]!=prev) { if(maxf<curr) {maxf=curr; maxn=prev;} else if(maxf==curr&&arr[j]<maxn) maxn=prev; curr=1; prev=arr[j]; } else curr++; } } if(curr>maxf) {maxf=curr; maxn=arr[n-1];} if(curr==maxf&&arr[n-1]<maxn) maxn=arr[n-1]; // System.out.println(maxf+" "+maxn); if(ab.size()>=1) { BigInteger ans=BigInteger.valueOf((maxf-1)); ans= ans.multiply(BigInteger.valueOf(k)); ans= ans.add(BigInteger.valueOf((k-maxn)+1)); System.out.println(ans); } else System.out.println(0); // int steps=arr[n-1]+1; // System.out.println((maxf-1)*k+(k-maxn)+1); // System.out.println(Arrays.toString(arr)); } } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
eedaa6c0fbefe93caafdfaa60e98acd1
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*;; import java.util.StringTokenizer; public class zra2 { public static void main(String args[]) { FastReader s=new FastReader(); int t=s.nextInt(); int i,n,k,arr[],x,sum=0,num=0,temp[],max=-1,count=0; while(t>0) { --t; n=s.nextInt(); k=s.nextInt(); arr=new int[n]; temp=new int[n]; for(i=0;i<n;i++) { arr[i]=s.nextInt(); sum=sum+arr[i]%k; if(arr[i]%k==0) temp[i]=0; else temp[i]=k-(arr[i]%k); } if(sum==0) { System.out.println(sum); sum=0; continue; } Arrays.sort(temp); for(i=n-1;i>0;i--) { if(temp[i]==0) continue; if(temp[i]==temp[i-1]) count++; else { if(count>max) { max=count; num=temp[i]; } count=0; } } if(count>max && n>1) { max=count; num=temp[i+1]; } if(n==1) { max=0; num=temp[0]; } System.out.println((long)max*k+(long)(num+1)); count=0; max=-1; sum=0; } } } 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
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
87dd52b569e5ea1e8e7d58bdd7399d3e
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; public class Main { static Scanner scan = new Scanner(System.in); public static void solve() throws Exception { //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //StringTokenizer tk = new StringTokenizer(in.readLine()); int n = scan.nextInt(); long k = scan.nextLong(); int[] nums = new int[n]; long sum =0; HashMap<Long,Long> map = new HashMap<>(); HashSet<Long> h = new HashSet<>(); for(int i=0;i<n;i++) { nums[i]=scan.nextInt(); if(nums[i]%k!=0) { long v = k-nums[i]%k; if(map.containsKey(v)) { map.replace(v,map.get(v)+1); h.add(v+(map.get(v)-1)*k); } else { map.put(v,(long)1); h.add(v); } } } long x=0; /* while(true) { if(map.size()==0) break; if(map.containsKey(x)) { int a = map.get(x)-1; if(a!=0) map.put(x+k,a); map.remove(x); x+=1; } else x+=1; } */ Iterator v = h.iterator(); long max = -1; while(v.hasNext()) { long a = (long)v.next(); max = Math.max(a,max); } System.out.println(max+1); } public static void main(String[] args) throws Exception { if(scan.hasNext()) { int t= scan.nextInt(); //int t= 1; outer:while(t-->0) { solve(); } } } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
e51d42fd55cce0faa66e4a03ca8043ad
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class ZeroRemainderArray{ public static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char)c; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } static long func(long a[],long size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i<size;i++){ maxc=Math.max(a[i],maxc+a[i]); max1=Math.max(maxc,max1); } return max1; } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } public static void main(String args[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader sc = new FastReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[n]; int temp[] = new int[n]; HashMap<Integer,Integer> hm = new HashMap<>(); for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int div = 0; for(int i = 0; i < n; i++) { temp[i] = a[i]%k; if(temp[i] == 0) { div++; } temp[i] = k - temp[i]; } if(div == n) { w.println(0); continue; } Arrays.sort(temp); int max = 1,count = 1, maxelement = 0; for(int i = n-1; i >= 0; i--) { if(temp[i] != k) { maxelement = temp[i]; break; } } for(int i = 0; i < n-1; i++) { if(temp[i] == k) { break; } if(temp[i] == temp[i+1]) { count++; if(max <= count) { max = count; maxelement = temp[i]; } } else { count = 1; } } long x = max - 1; long ans = x*k; ans += maxelement; w.println(++ans); } w.close(); } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
eb4b98bc0d0912fa7e1e2842649264aa
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
/* 盗图小能手 ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import javax.sound.midi.Soundbank; import java.io.*; import java.util.*; public class D { /** * TODO 提交的时候一定要设置为 true */ boolean submitFlag = true; private void run() throws IOException { //TODO 从这里开始读入题目输入 int numberOfTestcases = nextInt(); for (int t = 0; t < numberOfTestcases; t++) { int n = nextInt(); int k = nextInt(); long ans = -1; // 余数累计 Map<Integer, Long> map = new HashMap<>(); for (int i = 0; i < n; i++) { int data = nextInt(); int key = data % k; if (key != 0) { Long orDefault = map.getOrDefault(key, 0L); orDefault++; map.put(key, orDefault); ans = Math.max(ans, orDefault * k - key); } } System.out.println(ans + 1); } } // main以及输入输出======================================================================== public static void main(String[] args) throws IOException { D solution = new D(); solution.setReader(); solution.run(); solution.reader.close(); } /** * 注意不同题目这里路径要设一下 * * @throws IOException */ private void setReader() throws IOException { if (submitFlag) { reader = new BufferedReader(new InputStreamReader(System.in)); } else { String path = this.getClass().getResource("").getPath(); File file = new File(path); while (!file.getName().equals("target")) { file = file.getParentFile(); } String moduleName = file.getParentFile().getName(); String projectPath = System.getProperty("user.dir"); String modulePath = projectPath + "/" + moduleName + "/src/main/java/"; String className = this.getClass().getName().replace(".", "\\"); reader = new BufferedReader(new FileReader(modulePath + className + "input.txt")); } } BufferedReader reader; StringTokenizer tokenizer; int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String next() throws IOException { return nextToken(); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
dbdacb5495e5f6740b42ce13b9e07822
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test_case = scanner.nextInt(); for(int i = 0; i < test_case; ++i) { int n = scanner.nextInt(); long k = scanner.nextLong(); long res = 0L; long max_value = 0L; long max_k = 0; HashMap<Long,Long> hm = new HashMap<>(); for(int j = 0; j < n; ++j) { long temp = scanner.nextLong(); temp %= k; long value = 1L; if(temp != 0){ if(hm.containsKey(temp)) { value = hm.get(temp) + 1; hm.remove(temp); } hm.put(temp, value); if(value > max_value) { max_value = value; max_k = k-temp; } else if(value == max_value) max_k = Math.max(max_k, k-temp); } } if(max_value > 1) { res = ((max_value-1)*k) + (max_k); } else if(max_value == 1) { res = max_k; } //hallar el minimo con el nuemro de ciclos y fin. if(res > 0)++res; System.out.println(res); } } } // 2 1 1 2 3 1 5 4 2 3
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
8c0e322c144beec5b6f11d1546306f6f
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import javafx.util.Pair; import java.math.*; import java.math.BigInteger; import java.util.LinkedList; import java.util.Queue; public final class CodeForces { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static boolean set[]; static Pair<Integer,Integer> seg[]; static int A[]; public static void main(String args[])throws IOException { int T=i(); while(T-->0) { int N=i(); long K=l(); HashMap<Long,Integer> mp=new HashMap<>(); boolean f=true; for(int i=0; i<N; i++) { long a=l(); if(a%K!=0) { f=false; a=K-a%K; if(mp.containsKey(a)) { mp.put(a,mp.get(a)+1); } else mp.put(a,1); } } long key=0,freq=0; long max=0,mf=0; for(Map.Entry<Long,Integer> entry: mp.entrySet()) { key=entry.getKey(); freq=entry.getValue(); if(mf==freq) { max=Math.max(key,max); } if(freq>mf) { mf=freq; max=key; } } if(!f) System.out.println((mf-1)*K+max+1); else System.out.println(0); } } 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 String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { set=new boolean[N+1]; g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) g.add(new ArrayList<Integer>()); } static void DFS(int N,int d) { set[N]=true; d++; for(int i=0; i<g.get(N).size(); i++) { int c=g.get(N).get(i); if(set[c]==false) { DFS(c,d); } } } 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 long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } //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
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
753be82a1fbb07d13155dc2338041a0d
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; 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 boolean[] sieveOfEratosthenes(int n) { boolean[] prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p]) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } public static int[] lowestPrimeFactors(int n) { int tot = 0; int[] lpf = new int[n + 1]; int u = n + 32; double lu = Math.log(u); int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)]; for (int i = 2; i <= n; i++) lpf[i] = i; for (int p = 2; p <= n; p++) { if (lpf[p] == p) primes[tot++] = p; int tmp; for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) { lpf[tmp] = primes[i]; } } return lpf; } static int bsearch(List<Integer> list,int x) { int l=0,r=list.size()-1; while(l<=r) { int mid=(l+r)/2; if(list.get(mid)>=x) { if(mid==0) { return mid; } else if(list.get(mid-1)<x) { return mid; } else { r=mid-1; } } else { l=mid+1; } } return -1; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t= 1; while (t-- > 0) { int n=ri();int k=ri(); int[] arr=rai(n); // int[] dp=new int[k]; TreeMap<Integer,Integer> map=new TreeMap<>(); for(int i:arr) { int val=i%k; if(val!=0) { val=k-val; map.put(val,map.getOrDefault(val,0)+1); } } long max=Integer.MIN_VALUE; int val=0; for(int i:map.keySet()) { // System.out.println(i+" "+map.get(i)); if(map.get(i)>=max) { max = Math.max(map.get(i), max); val=i; } } long count=max*k; if(max==Integer.MIN_VALUE) { count=0; ans.append(count).append("\n"); } else { ans.append(count-(k-val-1)).append("\n"); } } out.print(ans.toString()); out.flush(); } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
980137190cb961cfba10d6d00ab78edd
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
//package src; import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.StringBuilder; import java.math.BigInteger; public class idk { static Scanner scn = new Scanner(System.in); static int mod = 1000000007; public static void main(String args[]) { int t = scn.nextInt(); while (t-- > 0) { //Stack<Integer>stk=new Stack<Integer>(); int n=scn.nextInt(),k=scn.nextInt(); Long arr[]=new Long[n]; boolean bl=true; for(int i=0;i<n;i++) { arr[i]=scn.nextLong(); } for(int i=0;i<n;i++) { if(arr[i]%k!=0) { bl=false; break; } } if(bl) System.out.println("0"); else { HashMap<Long,Long>map=new HashMap<>(); Arrays.parallelSort(arr); long ans=0,max=0,c=0; for(int i=0;i<n;i++) { if(arr[i]%k!=0) { if(map.containsKey(arr[i]%k)) { map.put(arr[i]%k, map.get(arr[i]%k)+k); max=Math.max(max,map.get(arr[i]%k)); } else { map.put(arr[i]%k, Math.abs(k-(arr[i]%k))); max=Math.max(max,Math.abs(k-arr[i]%k)); } } } System.out.println(max+1); } } } public static boolean word(String arr[],String str,String ori,HashMap<String, Integer>map,int i,int j) { if(j==str.length()) { //System.out.println(str); if(str.length()==0) return true; else return false; } String re=ori.substring(i,j); System.out.println(re); if(map.containsKey(re)) { String res=ori.substring(j); return word(arr, str,ori, map, j, j); } return word(arr, str,ori, map, i, j+1); } public static int decode(String str, int i, int[] memo) { if (i == 0) { return 1; } int ind = str.length() - i; if (str.charAt(ind) == '0') return 0; if (memo[i] != -1) return memo[i]; int res = decode(str, i - 1, memo); if (i >= 2 && Integer.parseInt(str.substring(ind, ind + 2)) <= 26) { res += decode(str, i - 2, memo); } memo[i] = res; return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static String factorial(int n) { BigInteger fac = new BigInteger("1"); for (int i = 1; i <= n; i++) { fac = fac.multiply(BigInteger.valueOf(i)); } return fac.toString(); } } class pair { int a; int b; pair(int x, int y) { this.a = x; this.b = y; } } class sorting implements Comparator<pair> { @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub int x=o1.a-o2.a,y=o1.b-o2.b; if(y==0) { if(x>=0) return -1; else return 1; } else if(y>0) return -1; else return 1; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
dce796cfd1b8b07fab3e7ea211d4af5a
train_000.jsonl
1593354900
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.
256 megabytes
//package src; import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.StringBuilder; import java.math.BigInteger; public class idk { static Scanner scn = new Scanner(System.in); static int mod = 1000000007; public static void main(String args[]) { int t = scn.nextInt(); while (t-- > 0) { //Stack<Integer>stk=new Stack<Integer>(); int n=scn.nextInt(),k=scn.nextInt(); Long arr[]=new Long[n]; boolean bl=true; for(int i=0;i<n;i++) { arr[i]=scn.nextLong(); } for(int i=0;i<n;i++) { if(arr[i]%k!=0) { bl=false; break; } } if(bl) System.out.println("0"); else { TreeMap<Long,Long>map=new TreeMap<>(); Arrays.parallelSort(arr); long ans=0,max=0,c=0; for(int i=0;i<n;i++) { if(arr[i]%k!=0) { if(map.containsKey(arr[i]%k)) { map.put(arr[i]%k, map.get(arr[i]%k)+k); max=Math.max(max,map.get(arr[i]%k)); } else { map.put(arr[i]%k, Math.abs(k-(arr[i]%k))); max=Math.max(max,Math.abs(k-arr[i]%k)); } } } System.out.println(max+1); } } } public static boolean word(String arr[],String str,String ori,HashMap<String, Integer>map,int i,int j) { if(j==str.length()) { //System.out.println(str); if(str.length()==0) return true; else return false; } String re=ori.substring(i,j); System.out.println(re); if(map.containsKey(re)) { String res=ori.substring(j); return word(arr, str,ori, map, j, j); } return word(arr, str,ori, map, i, j+1); } public static int decode(String str, int i, int[] memo) { if (i == 0) { return 1; } int ind = str.length() - i; if (str.charAt(ind) == '0') return 0; if (memo[i] != -1) return memo[i]; int res = decode(str, i - 1, memo); if (i >= 2 && Integer.parseInt(str.substring(ind, ind + 2)) <= 26) { res += decode(str, i - 2, memo); } memo[i] = res; return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static String factorial(int n) { BigInteger fac = new BigInteger("1"); for (int i = 1; i <= n; i++) { fac = fac.multiply(BigInteger.valueOf(i)); } return fac.toString(); } } class pair { int a; int b; pair(int x, int y) { this.a = x; this.b = y; } } class sorting implements Comparator<pair> { @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub int x=o1.a-o2.a,y=o1.b-o2.b; if(y==0) { if(x>=0) return -1; else return 1; } else if(y>0) return -1; else return 1; } }
Java
["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"]
2 seconds
["6\n18\n0\n227\n8"]
NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.
Java 8
standard input
[ "two pointers", "sortings", "math" ]
a8b4c115bedda3847e7c2e3620e3e19b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
1,400
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.
standard output
PASSED
2cfd0e064b6b2f3f89783b8063318627
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.util.Scanner; public class a { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); if(n==12) { String s = in.next(); String a[]=s.split(":"); int b1=Integer.valueOf(a[0]); int b2=Integer.valueOf(a[1]); if(b1>12) { b1 = b1 % 10; } if(b1==0)b1 = b1+10; if(b2>59) { b2 = b2%10; } if(b1<10&&b2<10){ System.out.print(0); System.out.print(b1); System.out.print(":"); System.out.print(0); System.out.print(b2); } if(b1>=10&&b2>=10){ //System.out.print(0); System.out.print(b1); System.out.print(":"); //System.out.print(0); System.out.print(b2); } if(b1<10&&b2>=10){ System.out.print(0); System.out.print(b1); System.out.print(":"); //System.out.print(0); System.out.print(b2); } if(b1>=10&&b2<10){ //System.out.print(0); System.out.print(b1); System.out.print(":"); System.out.print(0); System.out.print(b2); } } if(n==24) { String s = in.next(); String a[]=s.split(":"); int b1=Integer.valueOf(a[0]); int b2=Integer.valueOf(a[1]); if(b1>23) { b1 = b1 % 10; } if(b2>59) { b2 = b2%10; } if(b1<10&&b2<10){ System.out.print(0); System.out.print(b1); System.out.print(":"); System.out.print(0); System.out.print(b2); } if(b1>=10&&b2>=10){ //System.out.print(0); System.out.print(b1); System.out.print(":"); //System.out.print(0); System.out.print(b2); } if(b1<10&&b2>=10){ System.out.print(0); System.out.print(b1); System.out.print(":"); //System.out.print(0); System.out.print(b2); } if(b1>=10&&b2<10){ //System.out.print(0); System.out.print(b1); System.out.print(":"); System.out.print(0); System.out.print(b2); } } } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
31275330b5d5b8986ab3f1b6b64a0642
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.text.DecimalFormat; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); DecimalFormat f = new DecimalFormat("00"); int n = scan.nextInt(); String[] split = scan.next().split("[:]"); int h = Integer.parseInt(split[0]), m = Integer.parseInt(split[1]); if(n==12)while(h > 12) h-=10; if(n==24)while(h >= 24) h-=10; while(m >= 60) m-=10; if(n == 12 && h == 0)h++; System.out.println(f.format(h)+":"+f.format(m)); } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
c6dd2feaf0447d48049efc50d9f138ab
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.util.*; import java.math.*; public class clockA { public static void main(String args[]) { Scanner in = new Scanner(System.in); int format = in.nextInt(); in.nextLine(); String time = in.nextLine(); int hour1 = Integer.parseInt(time.substring(0, 1)); int hour2 = Integer.parseInt(time.substring(1, time.indexOf(":"))); int minute1 = Integer.parseInt(time.substring(time.indexOf(":") + 1, time.indexOf(":") + 2)); int minute2 = Integer.parseInt(time.substring(time.indexOf(":") + 2)); int hour = hour1*10 + hour2; int minute = minute1*10 + minute2; if(format == 12) { if(hour <= 12 && minute < 60 && hour > 0) { //correct format System.out.println(hour1 + "" + hour2 + ":" + minute1 + "" + minute2); } else { if(hour1 == 0 && hour2 == 0) { hour2 = 3; } if(hour > 12) { hour1 = 0; } if(hour > 12 && hour2 == 0) { hour1 = 1; } if(minute >= 60) { minute1 = 4; } System.out.println(hour1 + "" + hour2 + ":" + minute1 + "" + minute2); } } else if(format == 24) { if(hour < 24 && minute < 60) { //correct format System.out.println(hour1 + "" + hour2 + ":" + minute1 + "" + minute2); } else { if(hour >= 24) { hour1 = 0; } if(minute >= 60) { minute1 = 4; } System.out.println(hour1 + "" + hour2 + ":" + minute1 + "" + minute2); } } } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
3542c3c85040dd5672a3b28b761b461a
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static int dist(String s, String s2) { int c = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s2.charAt(i)) c++; } return c; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); String s = sc.next(); int min = 4; String ans = ""; if (n == 12) { for (int i = 0; i <= 1; i++) { if (i == 0) for (int j = 1; j <= 9; j++) { for (int k = 0; k < 6; k++) { for (int l = 0; l <= 9; l++) { int t = dist(s, i + "" + j + ":" + k + l); if (t < min) { min = t; ans = i + "" + j + ":" + k + l; } } } } else for (int j = 0; j < 3; j++) { for (int k = 0; k < 6; k++) { for (int l = 0; l <= 9; l++) { int t = dist(s, i + "" + j + ":" + k + l); if (t < min) { min = t; ans = i + "" + j + ":" + k + l; } } } } } } else for (int i = 0; i <= 2; i++) { if (i < 2) for (int j = 0; j <= 9; j++) { for (int k = 0; k < 6; k++) { for (int l = 0; l <= 9; l++) { int t = dist(s, i + "" + j + ":" + k + l); if (t < min) { min = t; ans = i + "" + j + ":" + k + l; } } } } else for (int j = 0; j < 4; j++) { for (int k = 0; k < 6; k++) { for (int l = 0; l <= 9; l++) { int t = dist(s, i + "" + j + ":" + k + l); if (t < min) { min = t; ans = i + "" + j + ":" + k + l; } } } } } out.println(ans); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int 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 boolean ready() throws IOException { return br.ready(); } } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
cc11f0a04642ab9a9c3a0989bd22a2b5
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int format = Integer.parseInt(br.readLine()); String orig = br.readLine(); if(format == 12) { int min = 5; String minStr = ""; for(int i = 1; i <= 12; i++) for(int j = 0; j <= 59; j++) { String strHrs = Integer.toString(i); if(strHrs.length() == 1) strHrs = "0" + strHrs; String minHrs = Integer.toString(j); if(minHrs.length() == 1) minHrs = "0" + minHrs; String time = strHrs + ":" + minHrs; int count = 0; for(int k = 0; k < 5; k++) if(orig.charAt(k) != time.charAt(k)) count++; if(count < min) { min = count; minStr = time; } } pw.println(minStr); } else { int min = 5; String minStr = ""; for(int i = 0; i <= 23; i++) for(int j = 0; j <= 59; j++) { String strHrs = Integer.toString(i); if(strHrs.length() == 1) strHrs = "0" + strHrs; String minHrs = Integer.toString(j); if(minHrs.length() == 1) minHrs = "0" + minHrs; String time = strHrs + ":" + minHrs; int count = 0; for(int k = 0; k < 5; k++) if(orig.charAt(k) != time.charAt(k)) count++; if(count < min) { min = count; minStr = time; } } pw.println(minStr); } br.close(); pw.close(); System.exit(0); } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
e11a54273fd3016f12bcc43ade67b70e
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Easy { static long mod = (long) Math.pow(10, 9) + 7; static long[][] memo = new long[65][5]; public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); String[] curr = sc.nextLine().split(":"); int hour = Integer.parseInt(curr[0]); int min = Integer.parseInt(curr[1]); if (min > 59) { min = 10 + min%10; } if (n == 24) { if (hour > 29) { hour = 10 + hour % 10; } else if (hour > 23) { hour = 20; } } else { if (hour > 19) { hour = hour % 10; if (hour == 0) hour = 10; } else if (hour > 12) { hour = 10; } else if (hour == 0) hour = 1; } out.println((hour < 10 ? "0" : "") + hour + ":" + (min < 10 ? "0" : "") + min); out.close(); } public static class Node { HashMap<Integer, Integer> edges; public Node() { edges = new HashMap<>(); } } public static long[] matPow(int pow) { long[] mat = new long[4]; if (memo[pow][0] != 0) { mat[0] = memo[pow][1]; mat[1] = memo[pow][2]; mat[2] = memo[pow][3]; mat[3] = memo[pow][4]; return mat; } long[] temp = matPow(pow - 1); long[] temp2 = matPow(pow - 1); mat[0] = (temp[0]*temp2[0] + temp[1]*temp2[2]) % mod; mat[1] = (temp[0]*temp2[1] + temp[1]*temp2[3]) % mod; mat[2] = (temp[2]*temp2[0] + temp[3]*temp2[2]) % mod; mat[3] = (temp[2]*temp2[1] + temp[3]*temp2[3]) % mod; memo[pow][0] = 1; memo[pow][1] = mat[0]; memo[pow][2] = mat[1]; memo[pow][3] = mat[2]; memo[pow][4] = mat[3]; return mat; } public static PrintWriter out; /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String out.println(result); // print via PrintWriter */ public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
5f9a3f1133ac2730d10f5b1389d179e4
train_000.jsonl
1475330700
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); long start = System.currentTimeMillis(); long fin = System.currentTimeMillis(); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; void run() { int n = sc.nextInt(); String[] s = sc.next().split(":"); int H = Integer.valueOf(s[0]); int M = Integer.valueOf(s[1]); if (n == 12) { if (H > 12) H = H % 10 == 0 ? 10 : H % 10; else if(H == 0) H = 1; if (M > 59) M %= 10; } else { if (H >= 24) H %= 10; if (M > 59) M %= 10; } s[0] = H < 10 ? "0" + H : H + ""; s[1] = M < 10 ? "0" + M : M + ""; System.out.println(s[0] +":" +s[1]); } public static void main(String[] args) { new Main().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } boolean inner(int h, int w, int limH, int limW) { return 0 <= h && h < limH && 0 <= w && w < limW; } void swap(int[] x, int a, int b) { int tmp = x[a]; x[a] = x[b]; x[b] = tmp; } // find minimum i (k <= a[i]) int lower_bound(int a[], int k) { int l = -1; int r = a.length; while (r - l > 1) { int mid = (l + r) / 2; if (k <= a[mid]) r = mid; else l = mid; } // r = l + 1 return r; } // find minimum i (k < a[i]) int upper_bound(int a[], int k) { int l = -1; int r = a.length; while (r - l > 1) { int mid = (l + r) / 2; if (k < a[mid]) r = mid; else l = mid; } // r = l + 1 return r; } long gcd(long a, long b) { return a % b == 0 ? b : gcd(b, a % b); } long lcm(long a, long b) { return a * b / gcd(a, b); } boolean palindrome(String s) { for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { return false; } } return true; } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } int[] nextIntArray(int n) { int[] in = new int[n]; for (int i = 0; i < n; i++) { in[i] = nextInt(); } return in; } int[][] nextInt2dArray(int n, int m) { int[][] in = new int[n][m]; for (int i = 0; i < n; i++) { in[i] = nextIntArray(m); } return in; } double[] nextDoubleArray(int n) { double[] in = new double[n]; for (int i = 0; i < n; i++) { in[i] = nextDouble(); } return in; } long[] nextLongArray(int n) { long[] in = new long[n]; for (int i = 0; i < n; i++) { in[i] = nextLong(); } return in; } char[][] nextCharField(int n, int m) { char[][] in = new char[n][m]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { in[i][j] = s.charAt(j); } } return in; } } }
Java
["24\n17:30", "12\n17:30", "24\n99:99"]
1 second
["17:30", "07:30", "09:09"]
null
Java 8
standard input
[ "implementation", "brute force" ]
88d56c1e3a7ffa94354ce0c70d8e958f
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.
1,300
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
standard output
PASSED
4631ac48995d3c36c0efdb8ad2edc1fd
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int[] mas = new int[n]; List<Integer> masL = new ArrayList<>(); int max=0; int temp=0; for(int i=0;i<n;i++) { mas[i]=sc.nextInt(); if(i==0 && i==n-1) { masL.add(i); } if(i==0) continue; if(mas[i-1]<mas[i]) { temp=i; } if(mas[i-1]>mas[i] && temp!=-1) { masL.add(i-1); temp=-1; } if(i==n-1 && temp!=-1) { masL.add(i); } } for(int i=0;i<masL.size();i++) { int p1=0,p2=0; for(int j=masL.get(i)-1; j>=0; j--) { if(mas[j]>mas[j+1]) { break; } p1++; } for(int j=masL.get(i)+1;j<n ; j++) { if(mas[j]>mas[j-1]) { break; } p2++; } if(p1+p2+1>max) max = p1+p2+1; } System.out.println(max); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
25ab93089418dcb29851ec7621977153
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.*; public class PetyaAndCountryside { static int countIrrigated(int[] h, int pos) { int cnt = 1; // (pos, n) for (int i = pos + 1; i < h.length && h[i] <= h[i - 1]; ++i) ++cnt; // [0, pos) for (int i = pos - 1; i >= 0 && h[i] <= h[i + 1]; --i) ++cnt; return cnt; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; ++i) { h[i] = in.nextInt(); } int ans = 0; for (int i = 0; i < n; ++i) { ans = Math.max(ans, countIrrigated(h, i)); } System.out.println(ans); in.close(); System.exit(0); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
c97daba411f32206703364e1374ea9d8
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.Scanner; public class PetyaAndCountryside { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int m = 0; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n; i++) { int t = 1; for (int j = i + 1; j < n; j++) { if (a[j] > a[j - 1]) break; t++; } for(int j = i - 1 ; j >= 0 ; j--){ if(a[j] > a[j + 1]) break; t++; } m = Math.max(m, t); } System.out.println(m); in.close(); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
6e4a2e7412210d756ee9cb500e7676d9
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.Scanner; public class B_66_Petya_And_Countryside { public static void main(String[] args){ Scanner input=new Scanner(System.in); int n=input.nextInt(); int[] height=new int[n]; int i; for(i=0;i<n;i++) height[i]=input.nextInt(); int max=0; int count=0; int j; for(i=0;i<n;i++){ count=1; //System.out.println("i=="+i); inner: for(j=i-1;j>=0;j--) if(height[j+1]>=height[j]){ count++; //System.out.println("j=="+j+" count=="+count); } else break inner; inner: for(j=i+1;j<n;j++) if(height[j-1]>=height[j])count++; else break inner; if(count>max) max=count; } System.out.println(max); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
0ef7b32f3d22d69cd2fb11763c6518ee
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.Scanner; public class B { static int n; static int a[]; public static void main(String[] args) { Scanner sc= new Scanner(System.in); n=sc.nextInt(); a=new int [n]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } int max=0; for (int i = 0; i < a.length; i++) { max=Math.max(max, tek(i)); } System.out.println(max); } private static int tek(int i) { int cnt=1; for (int j = i; j <n-1; j++) { if(a[j]>=a[j+1]) cnt++; else break; } for (int j = i; j >=1; j--) { if(a[j]>=a[j-1]) cnt++; else break; } return cnt; } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
88f90c241e7ca2431e41542e32638c7d
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PetyaandCountryside { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int index = Integer.parseInt(br.readLine().trim()); String[] s = br.readLine().trim().split("\\s+"); int[] arr = new int[index]; for (int i = 0; i < s.length; i++) arr[i] = Integer.parseInt(s[i]); int left = 0, right = 0, counter = 0; int result = 0; for (int i = 0; i < arr.length; i++) { left = arr[i]; right = arr[i]; counter = 1; for (int j = i + 1; j < arr.length; j++) { if (right >= arr[j]) { counter++; right = arr[j]; } else break; } for (int j = i - 1; j >= 0; j--) { if (left >= arr[j]) { counter++; left = arr[j]; } else break; } if (counter > result) result = counter; } System.out.println(result); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
ae6bec3c6a5ef9dd1f4ff06830e02b0a
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author alex */ 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int[] h = IOUtils.readIntArray(in, n); int ans = 0; for (int i = 0; i < n; ++i) { int s = 1; int l = i - 1; int r = i + 1; while (r < n && h[r] <= h[r - 1]) { ++s; ++r; } while (l >= 0 && h[l] <= h[l + 1]) { ++s; --l; } if (s > ans) { ans = s; } } out.println(ans); } } 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 InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } class IOUtils { public static int[] readIntArray(InputReader in, int n) { int[] ret = new int[n]; for (int i = 0; i < n; ++i) ret[i] = in.readInt(); return ret; } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
d4d14e2f1e1349a7d32cc79629438766
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.*; import java.lang.reflect.Array; import java.math.*; import java.io.*; import java.awt.*; import java.awt.geom.AffineTransform; public class Main{ static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(System.out); static final int maxn = (int)1e3 + 11; static int inf = (1<<30) - 1; static int n,m,l,r,t,b; static int a[] = new int[maxn]; public static void main(String[] args) throws Exception { n = in.nextInt(); a[0] = inf; a[n+1] = inf; for (int i=1; i<=n; i++) { a[i] = in.nextInt(); } int max = -inf; for (int i=1; i<=n; i++) { int t = doleft(i) + doright(i)+1; max = Math.max(t,max); } out.println(max); out.close(); } private static int doleft(int p) { int cnt=0; for (int i=p; i>=1; i--) { if (a[i]<a[i-1]) break; cnt++; } return cnt; } private static int doright(int p) { int cnt=0; for (int i=p; i<=n; i++) { if (a[i]<a[i+1]) break; cnt++; } return cnt; } } class Pair implements Comparable<Pair> { int l,r; public Pair (int l, int r) { this.l = l; this.r = r; } @Override public int compareTo(Pair p) { if (this.l<p.l) { return -1; } else if (this.l == p.l) { return 0; } else { return 1; } } @Override public boolean equals(Object p) { if (((Pair)p).l == this.l && ((Pair)p).r==this.r) { return true; } return false; } } class FastReader { BufferedReader bf; StringTokenizer tk = null; String DELIM = " "; public FastReader(BufferedReader bf) { this.bf = bf; } public String nextToken() throws Exception { if(tk==null || !tk.hasMoreTokens()) { tk = new StringTokenizer(bf.readLine(),DELIM); } return tk.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
c4e3df32dd4d173bd3f9cf9b7921daf6
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.Scanner; public class PetyaCountryside { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int maxLen = 0; for (int i = 0; i < n; i++) { int count = 1; for (int j = i; j < n - 1; j++) if (a[j] >= a[j + 1]) count++; else break; for (int j = i; j > 0; j--) if (a[j] >= a[j - 1]) count++; else break; maxLen = Math.max(maxLen, count); } System.out.println(maxLen); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
42f2f3f0fb093f912c6de9d41b55bc5d
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Reader.init(System.in); int n = Reader.nextInt() ; int [] a = new int [n] ; int temp = 0 , counter = 1 , max = 0 ; for (int i=0 ; i<a.length ; i++) a[i]=Reader.nextInt() ; for (int i=0 ; i<a.length ; i++) { temp = a[i] ; for (int j=i+1 ; j<a.length ; j++) { if (a[j]<=temp) counter++; else break ; temp=a[j]; } temp = a[i] ; for (int j=i-1 ; j>=0 ; j--) { if (a[j]<=temp) counter++ ; else break ; temp=a[j] ; } max = Math.max(counter,max); counter = 1 ; } System.out.println(max); } public static int BS(int[] a, int key) { int low = 0; int high = a.length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (mid == key ) { return a[mid]; } else if (mid < key) { low = mid + 1; } else { high = mid - 1; } } return -1; } public static boolean isSorted(int[] a) { int i; for (i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { continue; } else { return false; } } return true; } public static void fill(int[] a) throws Exception { for (int i = 0; i < a.length; i++) { a[i] = Reader.nextInt(); } } static void s(int[] cc) { for (int e = 0; e < cc.length; e++) { for (int q = e + 1; q < cc.length; q++) { if (cc[e] < cc[q]) { int g = cc[e]; cc[e] = cc[q]; cc[q] = g; } } } } static boolean isPalindrome(String s) { int n = s.length(); for (int i = 0; i < (n / 2) + 1; ++i) { if (s.charAt(i) != s.charAt(n - i - 1)) { return false; } } return true; } } class MyPair { String s ; int x; int y; public MyPair(String s,int x, int y) { this.s = s; this.x = x; this.y = y; } } 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()); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
0c437a519fa41d204af2bb51aa96025e
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
/* * @author Sane */ import java.util.*; import java.util.Scanner; import java.io.File; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.BigInteger; import javax.management.StringValueExp; public class TheMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), ans = 0; int[] height = new int[n]; for (int i = 0; i < n; ++i) height[i] = sc.nextInt(); for (int i = 0; i < n; ++i) { int tmp = 1; for (int j = i; j >= 0; --j) if (j-1 >=0 && height[j-1] <= height[j]) ++tmp; else break; for (int j = i; j < n; ++j) if (j+1 < n && height[j+1] <= height[j]) ++tmp; else break; ans = Math.max(ans, tmp); } System.out.println(ans); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
995d172278aa12f682ab29fb005b297d
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.awt.Point; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class Solution implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Solution(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (System.getProperty("ONLINE_JUDGE") != null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); out = new PrintWriter(System.out); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } // solution void solve() throws IOException { int n = readInt(); int[] mas = new int[n]; for(int i = 0; i<n; i++) mas[i] = readInt(); int answer = 0; for(int i = 0; i<n;i++) { int L = i; for(int l = i - 1;l>=0;l--){ if(mas[l] > mas[l+1]) break; L = l; } int R = i; for(int r = i + 1;r<n; r++){ if(mas[r] > mas[r-1]) break; R = r; } answer = Math.max(answer, R - L + 1); } out.println(answer); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
ff4c7a87041c996357326b3d8a276868
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.*; import java.io.*; public class Main{ BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ while (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int []a = new int[n]; for(int i = 0; i < n; ++i) a[i] = nextInt(); int ret = 1; for(int i = 0; i < n; ++i) { int at = i, ans = 1; while(at > 0 && a[at] >= a[at-1]) { at--; ans++; } at = i; while(at + 1 < n && a[at] >= a[at + 1]) { at++; ans++; } // System.out.println(i + " " + ans); ret = Math.max(ret, ans); } out.println(ret); out.close(); } public static void main(String args[]) throws Exception{ new Main().run(); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
f5c1c89295b5175de50b720db8f62b44
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.*; import java.util.*; public class j { public static void main(String aa[])throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); int v=0,l=0,n=0,k=0,p=0,j=0,m=0,t=1,i=0; String s,r; p=Integer.parseInt(b.readLine()); int d[]; d=new int[p]; s=b.readLine(); StringTokenizer c=new StringTokenizer(s); for(i=0;i<p;i++) d[i]=Integer.parseInt(c.nextToken()); for(i=0;i<p;i++) { m=d[i]; n=d[i]; for(j=i-1;j>=0&&k==0;j--) { if(d[j]<=m) { t++; m=d[j]; } else k=1; } for(j=i+1;j<p&&l==0;j++) { if(d[j]<=n) { t++; n=d[j]; } else l=1; } if(t>v) v=t; k=0; l=0; t=1; } System.out.print(v); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
4ee404b2b7194d8496349c8c8dd69cf2
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.*; public class Main { static int height[],n; public static void main(String[] args) { Scanner s = new Scanner(System.in); n = s.nextInt(); height = new int[n]; for (int i = 0;i < n;i++) height[i] = s.nextInt(); int max = 0,res; for (int i=0;i < n;i++) { res = testLeft(i) + testRight(i) + 1; if (res > max) max = res; } System.out.println(max); }//main public static int testRight(int ind) { int max=0,c=0; if (ind < n) max = height[ind]; for (int i = ind + 1;i < n;i++) if (max >= height[i]) { max = height[i]; c++; } else return c; return c; } public static int testLeft(int ind) { int max=0,c=0; if (ind > -1) max = height[ind]; for (int i=ind - 1;i >-1;i--) if (max >= height[i]) { max = height[i]; c++; } else return c; return c; } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
9eda1d87ae0cf4f998dae59385c12b62
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Task66B { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int[] a = new int[n]; StringTokenizer st = new StringTokenizer(in.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } int[] c = new int[n]; c[0] = 1; int p0 = a[0]; for (int i = 1; i < n; i++) { if (p0 >= a[i]) { c[0]++; p0 = a[i]; } else { break; } } int res = c[0]; for (int i = 1; i < n; i++) { c[i] = 1; if (a[i] == a[i - 1]) { c[i] = c[i - 1]; } else { int p = a[i]; for (int j = i + 1; j < n; j++) { if (p >= a[j]) { c[i]++; p = a[j]; } else { break; } } if (a[i] > a[i - 1]) { c[i] += c[i - 1]; } } if (res < c[i]) { res = c[i]; } } System.out.println(res); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
2966e59ffe2462dfb160a194e4a7d2eb
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String ar[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); } int max = -1; for (int i = 0; i < arr.length; i++) { int cnt = 1; for (int j = i - 1; j >= 0; j--) { if (arr[j] <= arr[j + 1]) cnt++; else break; } for (int j = i + 1; j < arr.length; j++) { if (arr[j] <= arr[j - 1]) cnt++; else break; } max = Math.max(max, cnt); } System.out.println(max); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
ab4eac628f3f2354de516738dcf53561
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class aa { public static class node { } public static void solve() { } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder q = new StringBuilder(); int n=Integer.parseInt(in.readLine()); String y[]=in.readLine().split(" "); int a[]=new int[n]; for (int i = 0; i < y.length; i++) { a[i]=Integer.parseInt(y[i]); } int max=0; for (int i = 0; i < a.length; i++) { int ans=1; for (int j = i-1; j>=0; j--) { if(a[j]<=a[j+1]) ans++; else break; } for (int j = i+1; j < a.length; j++) { if(a[j]<=a[j-1]) ans++; else break; } if(ans>max) max=ans; } System.out.println(max); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
a3ae8c9fff08d9d316b0e2d57eed8447
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PetyaAndCountryside { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] l = br.readLine().split("\\s+"); int[] arr = new int[n]; for (int i=0;i<n;i++) arr[i] = Integer.parseInt(l[i]); int max = Integer.MIN_VALUE; for (int i=0;i<n;i++){ int temp = 1; for (int j=i-1;j>=0;j--) { if (arr[j] <= arr[j+1]) temp++; else break; } for (int j=i+1;j<n;j++) { if (arr[j] <= arr[j-1]) temp++; else break; } if (temp > max) max = temp; } System.out.println(max); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
f6d2cdb6cc5fd4c0daab861ddf163f2e
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
/** * Created by icon on 1/20/2016. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class CF66B { public static void main(String[] args) { InputStream input = System.in; OutputStream output = System.out; Reader in = new Reader(input); PrintWriter out = new PrintWriter(output); Task magician = new Task(); magician.solve(in, out); out.close(); } static class Task { public void solve(Reader in, PrintWriter out) { int n = in.nextInt(); int height[] = new int[n + 1]; int res = 0, count; for(int i = 1; i <= n; i++) { height[i] = in.nextInt(); } for(int i = 1; i <= n; i++) { count = 1; int j = i + 1, previous = height[i]; while(j <= n && height[j] <= previous) { count++; previous = height[j]; j++; } j = i - 1; previous = height[i]; while(j > 0 && height[j] <= previous) { count++; previous = height[j]; j--; } res = Math.max(res, count); } out.print(res); } } static class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
d13bd267806dfc917291ac8d96dbad57
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
/** * Created by icon on 1/20/2016. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class CF66B { public static void main(String[] args) { InputStream input = System.in; OutputStream output = System.out; Reader in = new Reader(input); PrintWriter out = new PrintWriter(output); Task magician = new Task(); magician.solve(in, out); out.close(); } static class Task { public void solve(Reader in, PrintWriter out) { int n = in.nextInt(); int height[] = new int[n + 2]; int left[] = new int[n + 2]; int right[] = new int[n + 2]; int res = 0; height[0] = height[n + 1] = left[0] = right[n + 1] = 0; for(int i = 1; i <= n; i++) { height[i] = in.nextInt(); left[i] = (height[i] >= height[i - 1])? left[i - 1] + 1 : 1; } for(int i = n; i > 0; i--) { right[i] = (height[i] >= height[i + 1])? right[i + 1] + 1 : 1; res = Math.max(right[i] + left[i], res); } out.print(res - 1); } } static class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
5db5af555bb040569006e4a507481b1a
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author KHALED */ public class PetyaandCountryside { public static void main(String[] args) { MyScanner scan=new MyScanner(); int n=scan.nextInt(); int[]arr=new int[n]; for (int i = 0; i < n; i++) { arr[i]=scan.nextInt(); } int max=0; for (int i = 0; i < n; i++) { int count=1; if(i+1<n) { for (int j = i+1; j < n; j++) { if(arr[j]<=arr[j-1]) count++; else break; } } if(i-1>=0) { for (int j = i-1; j >= 0; j--) { if(arr[j]<=arr[j+1]) count++; else break; } } max=Math.max(max, count); } System.out.println(max); } } class MyScanner { static BufferedReader br; static 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()); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
e181b084cc8db85599cb62645bc7d4e6
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Main { private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter pw; void solve() throws IOException { int n = nextInt(); int h[] = new int[n]; for (int i = 0 ; i < n; ++i) h[i] = nextInt(); if (n == 1) { pw.print("1"); return; } int max = 0; int res = 0; for (int i = 0; i < n; ++i) { for (int j = i - 1; j >= 0; --j) { if (h[j] <= h[j + 1]) { ++res; } else break; } for (int j = i + 1; j < n; ++j) { if (h[j] <= h[j - 1]) { ++res; } else break; } ++res; if (max < res) max = res; res = 0; } pw.print(max); } public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); solve(); pw.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Main().run(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
45331bc38acf7a2880353701e761e50c
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ public void solve(int testNumber, InputReader in, PrintWriter out){ int n=in.nextInt(); int[] base=new int[n]; int[] counts=new int[n]; for(int i=0;i<n;i++) base[i]=in.nextInt(); int total=Integer.MIN_VALUE; for(int i=0;i<n;i++){ counts[i]=countLeft(i,base)+countRight(i,base)+1; } Arrays.sort(counts); out.println(counts[counts.length-1]); } public int countLeft(int index,int[] a){ int count=0; for(int i=index-1;i>=0;i--){ if(index>=0 && a[i]<=a[index--])count++; else break; } return count; } public int countRight(int index,int[] a){ int count=0; for(int i=index+1;i<a.length;i++){ if(index<a.length && a[i]<=a[index++])count++; else break; } return count; } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
9f70692b1f905aff3088a6922ea3c5d3
train_000.jsonl
1299513600
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ public void solve(int testNumber, InputReader in, PrintWriter out){ int n=in.nextInt(); int[] base=new int[n]; int[] tmpCounts=new int[n]; int[] counts=new int[n]; for(int i=0;i<n;i++) base[i]=in.nextInt(); for(int i=base.length-2;i>=0;i--) if(base[i]>=base[i+1]){ if(tmpCounts[i+1]==0)tmpCounts[i]++; else tmpCounts[i]+=tmpCounts[i+1]+1; counts[i]=tmpCounts[i]; } Arrays.fill(tmpCounts,0); for(int i=1;i<base.length;i++) if(base[i]>=base[i-1]){ if(tmpCounts[i-1]==0)tmpCounts[i]++; else tmpCounts[i]+=tmpCounts[i-1]+1; counts[i]+=tmpCounts[i]; } Arrays.sort(counts); out.println(counts[counts.length-1]+1); } public void showState(int[] a){ for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } }
Java
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
2 seconds
["1", "3", "6"]
null
Java 7
standard input
[ "implementation", "brute force" ]
5d11fa8528f1dc873d50b3417bef8c79
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
1,100
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
standard output
PASSED
02cb98df0b2bbcf8ab0fdf74fc044f1a
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int[][] graph = new int[n][n]; for (int i = 0; i < n; i++) Arrays.fill(graph[i], Integer.MAX_VALUE); for (int k = 0; k < m; k++) { int i = sc.nextInt() - 1, j = sc.nextInt() - 1, len = sc.nextInt(); graph[i][j] = len; graph[j][i] = len; } sc.close(); // floyd[i][j] = shortest path from i to j int[][] floyd = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) floyd[i][j] = graph[i][j]; floyd[i][i] = 0; } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (floyd[i][k] < Integer.MAX_VALUE && floyd[k][j] < Integer.MAX_VALUE) floyd[i][j] = Math.min(floyd[i][j], floyd[i][k] + floyd[k][j]); // edge[i][j] = number of i's adjacent edges on shortest path to j int[][] edge = new int[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (graph[i][k] < Integer.MAX_VALUE && graph[i][k] + floyd[k][j] == floyd[i][j]) edge[i][j]++; int[][] res = new int[n][n]; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { for (int k = 0; k < n; k++) if (floyd[i][k] + floyd[k][j] == floyd[i][j]) res[i][j] += edge[k][j]; System.out.print(res[i][j] + " "); } } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
90a9819b0018ee2276fa0725bb680f07
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.io.BufferedReader; import java.io.IOError; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Div2TaskE { FastIO scan = new FastIO(); PrintWriter cout = new PrintWriter(System.out); void run() { int n = scan.nextInt(); int m = scan.nextInt(); int[][] graph = new int[n][n]; int[][] floyd = new int[n][n]; int[][] count = new int[n][n]; int[][] answer = new int[n][n]; final int INF = 1000000000; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { floyd[i][j] = INF; graph[i][j] = INF; } floyd[i][i] = 0; } for (int i = 0; i < m; i++) { int u = scan.nextInt() - 1; int v = scan.nextInt() - 1; int w = scan.nextInt(); graph[u][v] = graph[v][u] = w; floyd[u][v] = floyd[v][u] = w; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (floyd[k][j] == INF || floyd[i][k] == INF) continue; floyd[i][j] = Math.min(floyd[i][j], floyd[i][k] + floyd[k][j]); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (floyd[k][j] == INF || floyd[i][j] == INF) continue; if (graph[i][k] + floyd[k][j] == floyd[i][j]) { // System.out.println(k + " " +i + " " + j); count[i][j]++; } } } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = 0; k < n; k++) { if (floyd[i][k] == INF || floyd[i][k] == INF || floyd[i][j] == INF) continue; if (floyd[i][k] + floyd[k][j] == floyd[i][j]) { answer[i][j] += count[k][j]; } } } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { System.out.print(answer[i][j] + " "); } } System.out.println(); } public static void main(String args[]) { new Div2TaskE().run(); } public class FastIO { BufferedReader br; StringTokenizer st; PrintWriter out; public FastIO(InputStream in, OutputStream o) { br = new BufferedReader(new InputStreamReader(in)); out = new PrintWriter(new OutputStreamWriter(o)); eat(""); } public FastIO() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); eat(""); } public void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new IOError(e); } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); 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 void print(String ans) { out.print(ans); out.flush(); } public void println(String ans) { out.println(ans); out.flush(); } public void close() { out.close(); } } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
4e159e4ad43a9a0b48a1a7ac369aa5b4
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Main mainClass = new Main(); } BufferedReader in; PrintWriter out; StringTokenizer st; public String next() throws Exception { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public Main() throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.close(); } static class Edge { int u, v, d; } public final static int INF = 1 << 29; int [][] runFloyd(int n, ArrayList<Edge> graph) { int [][] d = new int[n][n]; for (int i = 0 ; i < n ; i++) { Arrays.fill(d[i], INF); d[i][i] = 0; } for (Edge e : graph) { d[e.u][e.v] = e.d; d[e.v][e.u] = e.d; } /* for (int i = 0 ; i < n ; i ++) { for (int j = 0 ; j < n ; j ++) { out.print(d[i][j] + " "); } out.println(); } */ for (int k = 0 ; k < n ; k++) { for (int i = 0; i < n ; i++) { for (int j = 0 ; j < n ; j++) { if (d[i][k] < INF && d[k][j] < INF && d[i][k] + d[k][j] < d[i][j]) { d[i][j] = d[i][k] + d[k][j]; //out.println("updating " + i + "," + j + " with " + d[i][j] + " through " + k); } } } } return d; } int n, m; ArrayList<Edge> graph; public void solve() throws Exception { n = nextInt(); m = nextInt(); graph = new ArrayList<Edge>(); for (int i = 0 ; i < m ; i++) { Edge e = new Edge(); e.u = nextInt()-1; e.v = nextInt()-1; e.d = nextInt(); graph.add(e); } int [][] d = runFloyd(n, graph); /* for (int i = 0 ; i < n ; i ++) { for (int j = 0 ; j < n ; j ++) { out.print(d[i][j] + " "); } out.println(); } */ int [][] nextCount = new int[n][n]; for (int x = 0 ; x < n ; x ++) { for (Edge e : graph) { if (d[x][e.u] < INF && d[x][e.v] < INF && d[x][e.u] == d[x][e.v] + e.d) { // that means v is next node on shortest path from x to u nextCount[e.u][x] ++; } if (d[x][e.u] < INF && d[x][e.v] < INF && d[x][e.v] == d[x][e.u] + e.d) { // that means v is next node on shortest path from x to u nextCount[e.v][x] ++; } } } int ans[][] = new int [n][n]; for (int s = 0 ; s < n ; s ++) { for (int t = s + 1 ; t < n ; t ++) { if (d[s][t] >= INF) continue; for (int v = 0 ; v < n ; v ++) { if (d[s][v] >= INF || d[v][t] >= INF) continue; // if v is on shortest path between s and t, // increment ans[s][t] by nextCount[v][s] + nextCount[v][t] if (d[s][v] + d[v][t] == d[s][t]) { ans[s][t] += nextCount[v][s] + nextCount[v][t]; } } } } for (int s = 0 ; s < n ; s ++) { for (int t = s + 1 ; t < n ; t ++) { out.print( (ans[s][t]/2) + " " ); } } out.println(); } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
dccda442e3ffe0dd43be3b616dbefa31
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.util.*; import java.io.*; public class Main{ BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ while(str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } final static int oo = Integer.MAX_VALUE/2; int n,m; int [][]g; public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); n = nextInt(); m = nextInt(); g = new int[n][n]; for(int i=0;i<n;i++){ Arrays.fill(g[i], oo); } for(int i=0;i<m;i++){ int a = nextInt()-1, b = nextInt()-1; int cost = nextInt(); g[a][b] = g[b][a] = cost; } /* for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(g[i][j] + " " ); } System.out.println(); } System.out.println(); */ int [][]d = new int[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ d[i][j] = g[i][j]; } d[i][i] = 0; } /* for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(g[i][j] + " " ); } System.out.println(); } */ for(int k=0;k<n;k++){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if (d[i][k] < oo && d[k][j] < oo){ d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]); } } } } int c[][] = new int[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ for(int k=0;k<n;k++){ if (d[i][j] < oo && g[i][k] < oo && d[k][j] < oo){ if (g[i][k] + d[k][j] == d[i][j]){ c[i][j]++; } } } } } int r[][] = new int[n][n]; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if (d[i][j] < oo){ for(int k=0;k<n;k++){ if (d[i][k] < oo && d[k][j] < oo){ if (d[i][j] == d[i][k] + d[k][j]){ r[i][j]+=c[k][j]; } } } } } } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ out.print(r[i][j] + " "); } } out.println(); out.close(); } public static void main(String args[]) throws Exception{ new Main().run(); } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
07618055a64be6753044f4903dff54fd
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); String[] strNM = line.split(" "); int vNumber = Integer.parseInt(strNM[0]); int eNumber = Integer.parseInt(strNM[1]); int[][] roads = new int[vNumber][vNumber]; int[][] matrixOfDistances = new int[vNumber][vNumber]; int[][] edges = new int[vNumber][vNumber]; for(int i = 0; i < eNumber; i++) { line = scanner.nextLine(); strNM = line.split(" "); int cityA = Integer.parseInt(strNM[0]) - 1; int cityB = Integer.parseInt(strNM[1]) - 1; int distance = Integer.parseInt(strNM[2]); matrixOfDistances[cityA][cityB] = distance; matrixOfDistances[cityB][cityA] = distance; roads[cityA][cityB] = distance; roads[cityB][cityA] = distance; } for(int k = 0; k < vNumber; k++) { for(int i = 0; i < vNumber; i++) { for(int j = 0; j < vNumber; j++) { if(i == j) { continue; } if(matrixOfDistances[i][k] == 0 || matrixOfDistances[k][j] == 0) { continue; } if(matrixOfDistances[i][j] == 0) { matrixOfDistances[i][j] = matrixOfDistances[i][k] + matrixOfDistances[k][j]; } else if(matrixOfDistances[i][j] > matrixOfDistances[i][k] + matrixOfDistances[k][j]) { matrixOfDistances[i][j] = matrixOfDistances[i][k] + matrixOfDistances[k][j]; } } } } for(int i = 0; i < vNumber; i++) { for(int j = 0; j < vNumber; j++) { for(int k = 0; k < vNumber; k++) { if(i == j) { continue; } if(roads[i][k] == 0) { continue; } if(matrixOfDistances[i][j] == roads[i][k] + matrixOfDistances[k][j]) { edges[i][j]++; } } } } for(int i = 0; i < vNumber; i++) { for(int j = i + 1; j < vNumber; j++) { int roadsOnTheWay = edges[i][j]; if(roadsOnTheWay != 0) { for(int k = 0; k < vNumber; k++) { if(i == k || j == k) { continue; } if(matrixOfDistances[i][j] == matrixOfDistances[i][k] + matrixOfDistances[k][j]) { roadsOnTheWay += edges[k][j]; } } } System.out.print(roadsOnTheWay); System.out.print(" "); } } } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
0b190f0b1f53d38fe86eec2e7b35599f
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
//Author: net12k44 import java.io.*; import java.util.*; //public class Main{//} void print(int a[][]){ for(int d[]: a){ for(int x: d) out.print(x+" "); out.println(); } } private void solve() { int n = in.nextInt(), m = in.nextInt(); int a[][] = new int[n][n], c[][] = new int[n][n]; final int oo = 1000000000; for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) c[j][i] = c[i][j] = oo; while (m-- > 0){ int i = in.nextInt()-1, j = in.nextInt()-1, k = in.nextInt(); a[i][j] = a[j][i] = k; c[i][j] = c[j][i] = k; } for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if (c[i][k] + c[k][j] < c[i][j]) c[i][j] = c[i][k] + c[k][j]; int f[][] = new int[n][n]; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if (i!=j) for(int k = 0; k < n; ++k) if (k!=i && c[i][k] == a[i][k] && c[i][k] + c[k][j] == c[i][j]) f[i][j]++; int kq[][] = new int[n][n]; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if (i!=j) for(int k = 0; k < n; ++k) if (c[i][k] + c[k][j] == c[i][j]) kq[i][j] += f[k][j]; for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) out.print( kq[i][j]+" " ); } public static void main (String[] args) throws java.lang.Exception { long startTime = System.currentTimeMillis(); out = new PrintWriter(System.out); new Main().solve(); //out.println((String.format("%.2f",(double)(System.currentTimeMillis()-startTime)/1000))); out.close(); } static PrintWriter out; static class in { static BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) ) ; static StringTokenizer tokenizer = new StringTokenizer(""); static String next() { while ( !tokenizer.hasMoreTokens() ) try { tokenizer = new StringTokenizer( reader.readLine() ); } catch (IOException e){ throw new RuntimeException(e); } return tokenizer.nextToken(); } static int nextInt() { return Integer.parseInt( next() ); } } ////////////////////////////////////////////////// }//
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
a812e79f2a6875e01c0d079f32d5de95
train_000.jsonl
1397376000
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s &lt; t) find the number of roads that lie on at least one shortest path from s to t.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Solver { public static void main(String[] Args) throws NumberFormatException, IOException { new Solver().Run(); } PrintWriter pw; StringTokenizer Stok; BufferedReader br; public String nextToken() throws IOException { while (Stok == null || !Stok.hasMoreTokens()) { Stok = new StringTokenizer(br.readLine()); } return Stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } int n; int[][] edges; int[][] lenWay; int[][] kolEdges; public void Run() throws NumberFormatException, IOException { //br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); n=nextInt(); edges=new int[n][n]; lenWay=new int[n][n]; kolEdges=new int[n][n]; for (int i=0; i<n; i++){ Arrays.fill(edges[i], Integer.MAX_VALUE/3); Arrays.fill(lenWay[i],Integer.MAX_VALUE/3); lenWay[i][i]=0; } int buf=nextInt(); for (int i=0; i<buf; i++){ int zn1=nextInt()-1; int zn2=nextInt()-1; int len=nextInt(); edges[zn1][zn2]=len; edges[zn2][zn1]=len; lenWay[zn2][zn1]=len; lenWay[zn1][zn2]=len; } for (int k=0; k<n; k++){ for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ lenWay[i][j]=Math.min(lenWay[i][j], lenWay[i][k]+lenWay[k][j]); } } } for (int k=0; k<n; k++){ for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ if (edges[i][k]<Integer.MAX_VALUE/3 && lenWay[i][j]==edges[i][k]+lenWay[k][j]) kolEdges[i][j]++; } } } for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ int result=0; for (int k=0; k<n; k++){ if (lenWay[i][j]==lenWay[i][k]+lenWay[k][j]) result+=kolEdges[k][j]; } pw.print(result); pw.print(' '); } } pw.println(); pw.flush(); pw.close(); } }
Java
["5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4"]
4 seconds
["1 4 1 2 1 5 6 1 2 1"]
null
Java 7
standard input
[ "dp", "graphs", "shortest paths" ]
ec1a4656256b09cf1c6050977fbc6cb9
The first line of the input contains integers n, m (2 ≤ n ≤ 500, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ li ≤ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
2,500
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
standard output
PASSED
e6139d4cba54a893852bf12ac8e514f3
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; /** * Created by Darren on 14-9-29. */ public class Main { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { new Main().run(); } void run() throws IOException { int n = in.nextInt(); LinearSpace x = new LinearSpace(n), y = new LinearSpace(n); x.adjust(y); if (!x.sameSpace(y)) out.println(-1); else { out.println(x.getOperationSize()+y.getOperationSize()); x.outputOperations(false); y.outputOperations(true); } out.flush(); } class LinearSpace { final int MAX_SHIFT = 30; int[] data; int n; int rank; List<Operators> operations; public LinearSpace(int n) throws IOException { this.n = n; rank = 0; data = new int[n]; for (int i = 0; i < n; i++) data[i] = in.nextInt(); operations = new ArrayList<Operators>(); gaussian(); } void gaussian() { rank = 0; operations.clear(); for (int shift = MAX_SHIFT; shift >= 0; shift--) { int mainRowID = -1; for (int i = rank; i < n; i++) { if (((data[i] >> shift) & 1) == 1) { mainRowID = i; break; } } if (mainRowID != -1) { if (mainRowID != rank) swap(mainRowID, rank); for (int j = rank+1; j < n; j++) { if (((data[j] >> shift) & 1) == 1) eliminate(j, rank); } rank++; } } } void swap(int x, int y) { operations.add(new Operators(x, y)); operations.add(new Operators(y, x)); operations.add(new Operators(x, y)); data[x] ^= data[y]; data[y] ^= data[x]; data[x] ^= data[y]; } void eliminate(int x, int y) { operations.add(new Operators(x, y)); data[x] ^= data[y]; } void adjust(final LinearSpace another) { int maxRank = Math.max(this.rank, another.rank); int shift = MAX_SHIFT; for (int i = 0; i < rank; i++) { while (((data[i] >> shift) & 1) == 0) shift--; for (int j = 0; j < maxRank; j++) { if (((data[j] >> shift) & 1) != ((another.data[j] >> shift) & 1)) eliminate(j, i); } } } boolean sameSpace(final LinearSpace another) { int maxRank = Math.max(this.rank, another.rank); for (int i = 0; i < maxRank; i++) { if (data[i] != another.data[i]) return false; } return true; } int getOperationSize() { return operations.size(); } void outputOperations(boolean reverse) { if (reverse) { for (int i = operations.size()-1; i >= 0; i--) { out.print(operations.get(i).x + 1); out.print(' '); out.println(operations.get(i).y + 1); } } else { for (int i = 0; i < operations.size(); i++) { out.print(operations.get(i).x + 1); out.print(' '); out.println(operations.get(i).y + 1); } } } class Operators { int x; int y; public Operators(int x, int y) { this.x = x; this.y = y; } } } static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String nextToken() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } String readLine() throws IOException { return reader.readLine(); } int nextInt() throws IOException { return Integer.parseInt( nextToken() ); } long nextLong() throws IOException { return Long.parseLong( nextToken() ); } double nextDouble() throws IOException { return Double.parseDouble( nextToken() ); } } }
Java
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
83ca23fb2fd6c48e26ef07c4d69403fc
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; /** * CodeForces 472F - Design Tutorial: Change the Goal * Created by Darren on 14-9-29. * O(n^2) time and O(n^2) space. * * Tag: linear algebra, xor */ public class Main { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { new Main().run(); } void run() throws IOException { int n = in.nextInt(); LinearSpace x = new LinearSpace(n), y = new LinearSpace(n); if (!x.transformable(y)) out.println(-1); else { out.println(x.getOperationSize()+y.getOperationSize()); x.outputOperations(false); y.outputOperations(true); } out.flush(); } // Class LinearSpace class LinearSpace { final int MAX_SHIFT = 31; // Regular int range int[] data; int n; int rank; // Rank of the linear space List<Operands> operations; // Used to store history of XOR operations public LinearSpace(int n) throws IOException { this.n = n; rank = 0; data = new int[n]; for (int i = 0; i < n; i++) data[i] = in.nextInt(); operations = new ArrayList<Operands>(); gaussian(); } // Conduct Gaussian elimination void gaussian() { rank = 0; operations.clear(); for (int shift = MAX_SHIFT; shift >= 0; shift--) { // Find the index of the value whose bit at the given shift is one; this value is // used as a base of the linear space int mainRowID = -1; for (int i = rank; i < n; i++) { if (((data[i] >> shift) & 1) == 1) { mainRowID = i; break; } } // Such value exists if (mainRowID != -1) { if (mainRowID != rank) // Swap the value such that it is on the left hand side // togher with otherbases swap(mainRowID, rank); // Clear the 1's at the given shift for the remaining values for (int j = rank+1; j < n; j++) { if (((data[j] >> shift) & 1) == 1) eliminate(j, rank); } rank++; } } } // Swap data[p] and data[q] void swap(int p, int q) { operations.add(new Operands(p, q)); operations.add(new Operands(q, p)); operations.add(new Operands(p, q)); data[p] ^= data[q]; data[q] ^= data[p]; data[p] ^= data[q]; } // data[p] ^= data[q] void eliminate(int p, int q) { operations.add(new Operands(p, q)); data[p] ^= data[q]; } boolean transformable(final LinearSpace another) { if (rank < another.rank) return false; // Adjust the bases to those of another linear space. int shift = MAX_SHIFT; for (int i = 0; i < rank; i++) { // Find data[i] such that its bit at the given shift is 1 while (((data[i] >> shift) & 1) == 0) shift--; // Align the bases to those of the other space at the given shift for (int j = 0; j < rank; j++) { if (((data[j] >> shift) & 1) != ((another.data[j] >> shift) & 1)) eliminate(j, i); } } // If the other space is the same with, or a subspace of, this space, // their first maxRank elements should be the same after the adjustment. for (int i = 0; i < rank; i++) { if (data[i] != another.data[i]) return false; } return true; } int getOperationSize() { return operations.size(); } // Output operation history from oldest to newest if reverse is false, // or from newest to oldest if otherwise void outputOperations(boolean reverse) { if (reverse) { for (int i = operations.size()-1; i >= 0; i--) { out.print(operations.get(i).p + 1); out.print(' '); out.println(operations.get(i).q + 1); } } else { for (int i = 0; i < operations.size(); i++) { out.print(operations.get(i).p + 1); out.print(' '); out.println(operations.get(i).q + 1); } } } // Class Operators: (p,q) means x_p ^= x_q class Operands { int p; int q; public Operands(int p, int q) { this.p = p; this.q = q; } } } static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String nextToken() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } String readLine() throws IOException { return reader.readLine(); } int nextInt() throws IOException { return Integer.parseInt( nextToken() ); } long nextLong() throws IOException { return Long.parseLong( nextToken() ); } double nextDouble() throws IOException { return Double.parseDouble( nextToken() ); } } }
Java
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
ffc18328ff7b8050b344e7b91343a3f5
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; 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); new Task().solve(1,in,out); out.close(); } } class Task { void solve(int ca,InputReader in,PrintWriter out) { int n = in.nextInt(); int[] A = new int[n]; for (int i = 0; i < n; ++ i) { A[i] = in.nextInt(); } ArrayList<Pair> listA = new ArrayList<Pair>(); gauss(A,listA); int[] B = new int[n]; for (int i = 0; i < n; ++ i) { B[i] = in.nextInt(); } ArrayList<Pair> listB = new ArrayList<Pair>(); gauss(B,listB); int na = 0; for ( ; na<n && A[na]>0; ++ na); int nb = 0; for ( ; nb<n && B[nb]>0; ++ nb); for (int i = 0,r = 0; i < nb; ++ i) { int value = B[i]; int p = -1; for ( ; r < na; ++ r) { if ((A[r]&-A[r]) == (value&-value)) { p = r ++; break; } } if (p == -1) { out.println(-1); return ; } value ^= A[p]; for (int j = p+1; j<na && value>0; ++ j) { if ((value&-value) == (A[j]&-A[j])) { value ^= A[j]; listA.add(new Pair(p,j)); } } if (value != 0) { out.println(-1); return ; } A[p] = B[i]; } for (int i = 0; i < nb; ++ i) { for (int j = 0; j < na; ++ j) { if (B[i] == A[j]) { if (i != j) { A[i] ^= A[j]; A[j] ^= A[i]; A[i] ^= A[j]; listA.add(new Pair(i,j)); listA.add(new Pair(j,i)); listA.add(new Pair(i,j)); } break; } } } for (int i = nb; i < na; ++ i) { A[i] ^= A[i]; listA.add(new Pair(i,i)); } out.println((listA.size() + listB.size())); for (Pair t : listA) { t.a ++; t.b ++; out.println(t.a + " " + t.b); } for (int i = listB.size()-1; i >= 0; -- i) { Pair t = listB.get(i); t.a ++; t.b ++; out.println(t.a + " " + t.b); } } void gauss(int[] A,ArrayList<Pair> list) { int row = 0; int n = A.length; while (row<n) { int mx = (1<<31)-1,p = -1; for (int i = row; i < n; ++ i) { int t = A[i]&-A[i]; if (t>0 && t<mx) { mx = t; p = i; } } if (p == -1) break; if (row != p) { A[p] ^= A[row]; A[row] ^= A[p]; A[p] ^= A[row]; list.add(new Pair(p,row)); list.add(new Pair(row,p)); list.add(new Pair(p,row)); } for (int i = row+1; i < n; ++ i) { if ((A[i]&-A[i]) == mx) { A[i] ^= A[row]; list.add(new Pair(i,row)); } } ++ row; } } class Pair { int a,b; Pair(int a,int b) { this.a = a; this.b = b; } } } 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
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
0930f6b56a69b3694e780a723aaadc51
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /** * @author Pavel Mavrin */ public class F { private int[] s; private int k; private int[] x; private int zzz; private void solve() throws IOException { int n = nextInt(); x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } int[] y = new int[n]; for (int i = 0; i < n; i++) { y[i] = nextInt(); } s = new int[50]; k = 0; for (int i = 0; i < n; i++) { if (x[i] > 0 && !canMake(x[i])) { s[k++] = i; } } for (int i = 0; i < n; i++) { if (!canMake(y[i])) { // System.err.println(y[i]); out.println(-1); return; } } for (int i = 0; i < n; i++) { boolean ok = true; for (int j = 0; j < k; j++) { if (s[j] == i) ok = false; } if (ok) { move(i, i); canMake(y[i]); for (int j = 0; j < k; j++) { if (bit(zzz, j)) { move(i, s[j]); } } } } int[] a = new int[k]; for (int i = 0; i < k; i++) { canMake(y[s[i]]); a[i] = zzz; } // for (int i = 0; i < k; i++) { // for (int i = 0; i ) // } int c1 = mn; for (int i = 0; i < k; i++) { for (int t = i; t < k; t++) { if (bit(a[t], i)) { if (t != i) { move(s[i], s[t]); a[i] ^= a[t]; } for (int j = 0; j < k; j++) { if (i != j && bit(a[j], i)) { move(s[j], s[i]); a[j] ^= a[i]; } } break; } } } int c2 = mn; for (int i = 0; i < k; i++) { if (!bit(a[i], i)) { for (int j = 0; j < k; j++) { if (bit(a[j], i)) { move(s[j], s[i]); } } move(s[i], s[i]); } } // // for (int i = 0; i < mn; i++) { // x[moves[0][i]] ^= x[moves[1][i]]; // } // for (int i = 0; i < n; i++) { // if (x[i] != y[i]){ // System.out.println(Arrays.toString(x) + " " + Arrays.toString(y)); // break; // } // } // System.out.println(ll); out.println(mn); for (int i = 0; i < c1; i++) { out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1)); x[moves[0][i]] ^= x[moves[1][i]]; } // out.println(); for (int i = c2; i < mn; i++) { out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1)); x[moves[0][i]] ^= x[moves[1][i]]; } // out.println(); for (int i = c2 - 1; i >= c1; i--) { out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1)); x[moves[0][i]] ^= x[moves[1][i]]; } for (int i = 0; i < n; i++) { if (x[i] != y[i]) { throw new RuntimeException(); } } } int[][] moves = new int[2][1000000]; int mn; private void move(int i, int j) { moves[0][mn] = i; moves[1][mn] = j; mn++; } private boolean canMake(int z) { int[] q = new int[k + 1]; int[] w = new int[k + 1]; boolean[] zz = new boolean[k + 1]; for (int i = 0; i < k; i++) { q[i] = x[s[i]]; w[i] = (1 << i); } q[k] = z; for (int j = 0; j < 31; j++) { int t = 0; while (t < k && (zz[t] || !bit(q[t], j))) t++; if (t < k) { zz[t] = true; for (int i = 0; i <= k; i++) { if (i != t && bit(q[i], j)) { q[i] ^= q[t]; w[i] ^= w[t]; } } } } zzz = w[k]; return q[k] == 0; } private boolean bit(int i, int j) { return ((i >> j) & 1) == 1; } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter out = new PrintWriter(System.out); String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } public static void main(String[] args) throws IOException { new F().run(); } private void run() throws IOException { solve(); out.close(); } }
Java
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
c37aeddfd3af07b95f32af8965f1aff2
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Collection; 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); PrintWriter out = new PrintWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } } class TaskF { private static final int BIT = 30; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] source = new int[n]; int[] target = new int[n]; for (int i = 0; i < n; ++i) source[i] = in.nextInt(); for (int i = 0; i < n; ++i) target[i] = in.nextInt(); List<int[]> moveFromSource = gauss(source); List<int[]> moveFromTarget = gauss(target); List<int[]> moves = new ArrayList<>(); moves.addAll(moveFromSource); for (int i = 0; i < n; ++i) { int last = i; for (int bit = 0; bit < BIT; ++bit) if ((source[i] & 1 << bit) != (target[i] & 1 << bit)) { while (last < n && Integer.numberOfTrailingZeros(source[last]) < bit) ++last; if (last < n && Integer.numberOfTrailingZeros(source[last]) == bit) { source[i] ^= source[last]; moves.add(new int[]{i, last}); } else { out.println(-1); return; } ++last; } } Collections.reverse(moveFromTarget); moves.addAll(moveFromTarget); out.println(moves.size()); for (int[] move : moves) { out.print(move[0] + 1); out.print(' '); out.println(move[1] + 1); } } private List<int[]> gauss(int[] a) { int n = a.length; List<int[]> moves = new ArrayList<>(); for (int bit = 0, last = 0; bit < BIT; ++bit) { boolean found = false; for (int x = last; x < n; ++x) if ((a[x] & 1 << bit) != 0) { if (x != last) { a[x] ^= a[last]; a[last] ^= a[x]; a[x] ^= a[last]; moves.add(new int[]{x, last}); moves.add(new int[]{last, x}); moves.add(new int[]{x, last}); } found = true; break; } if (!found) continue; for (int x = last + 1; x < n; ++x) if ((a[x] & 1 << bit) != 0) { a[x] ^= a[last]; moves.add(new int[]{x, last}); } ++last; } return moves; } } class InputReader { private BufferedReader reader; private 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
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
c33ac1fe8aedc5cdec2febfe91800788
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Collection; 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); PrintWriter out = new PrintWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } } class TaskF { private static final int BIT = 30; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] source = new int[n]; int[] target = new int[n]; for (int i = 0; i < n; ++i) source[i] = in.nextInt(); for (int i = 0; i < n; ++i) target[i] = in.nextInt(); int[] importantBitOfSource = new int[BIT]; int[] importantBitOfTarget = new int[BIT]; List<int[]> moveFromSource = new ArrayList<>(); List<int[]> moveFromTarget = new ArrayList<>(); gauss(source, importantBitOfSource, moveFromSource); gauss(target, importantBitOfTarget, moveFromTarget); List<int[]> moves = new ArrayList<>(); moves.addAll(moveFromSource); for (int i = 0; i < n; ++i) { int last = i; for (int bit = 0; bit < BIT; ++bit) if ((source[i] & 1 << bit) != (target[i] & 1 << bit)) { while (last < n && Integer.numberOfTrailingZeros(source[last]) < bit) ++last; if (last < n && Integer.numberOfTrailingZeros(source[last]) == bit) { source[i] ^= source[last]; moves.add(new int[]{i, last}); } else { out.println(-1); return; } ++last; } } Collections.reverse(moveFromTarget); moves.addAll(moveFromTarget); out.println(moves.size()); for (int[] move : moves) { out.print(move[0] + 1); out.print(' '); out.println(move[1] + 1); } } private void gauss(int[] a, int[] importantBit, List<int[]> move) { int n = a.length; Arrays.fill(importantBit, -1); for (int bit = 0, last = 0; bit < BIT; ++bit) { boolean found = false; for (int x = last; x < n; ++x) if ((a[x] & 1 << bit) != 0) { if (x != last) { a[x] ^= a[last]; a[last] ^= a[x]; a[x] ^= a[last]; move.add(new int[]{x, last}); move.add(new int[]{last, x}); move.add(new int[]{x, last}); } found = true; break; } if (!found) continue; importantBit[bit] = last; for (int x = last + 1; x < n; ++x) if ((a[x] & 1 << bit) != 0) { a[x] ^= a[last]; move.add(new int[]{x, last}); } ++last; } } } class InputReader { private BufferedReader reader; private 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
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
7f505c0ec7670724a6446a6e96fbfff9
train_000.jsonl
1411918500
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
256 megabytes
import java.io.IOException; import java.util.InputMismatchException; import java.util.ArrayList; import java.io.OutputStream; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author George Marcus */ 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); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } } class TaskF { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = in.nextInt(); } ArrayList<Integer> aMoves = new ArrayList<Integer>(); int[] aBase = gauss(A, aMoves); ArrayList<Integer> bMoves = new ArrayList<Integer>(); int[] bBase = gauss(B, bMoves); ArrayList<Integer> changeMoves = new ArrayList<Integer>(); int[] aux = new int[N]; boolean ok = true; for (int i = 0; i < N && ok; i++) { int p = 0; int b = bBase[i]; int a = 0; for (int j = i; j < N && aBase[j] != 0 && a != b; ) { int bit = Integer.bitCount(Integer.lowestOneBit(aBase[j]) - 1); if ((b & (1 << bit)) != (a & (1 << bit))) { a ^= aBase[j]; aux[p++] = j; } else { j++; } } if (a == b) { if (p > 0) { if (aux[0] != i) { addSwapMoves(aux[0], i, changeMoves); int ax = aBase[0]; aBase[0] = aBase[aux[0]]; aBase[aux[0]] = ax; } for (int j = 1; j < p; j++) { changeMoves.add(i); changeMoves.add(aux[j]); } } else { changeMoves.add(i); changeMoves.add(i); } } else { ok = false; } } if (!ok) { out.println(-1); } else { int totalMoves = (aMoves.size() + bMoves.size() + changeMoves.size()) / 2; out.println(totalMoves); for (int i = 0; i < aMoves.size(); i += 2) { out.println((aMoves.get(i) + 1) + " " + (aMoves.get(i + 1) + 1)); } for (int i = 0; i < changeMoves.size(); i += 2) { out.println((changeMoves.get(i) + 1) + " " + (changeMoves.get(i + 1) + 1)); } for (int i = bMoves.size() - 2; i >= 0; i -= 2) { out.println((bMoves.get(i) + 1) + " " + (bMoves.get(i + 1) + 1)); } } } private int[] gauss(int[] A, ArrayList<Integer> moves) { int N = A.length; int[] base = new int[N]; System.arraycopy(A, 0, base, 0, N); int k = 0; for (int j = 0; j < 30; j++) { int i = k; while (i < N && (base[i] & (1 << j)) == 0) { i++; } if (i == N) { continue; } if (i != k) { int aux = base[i]; base[i] = base[k]; base[k] = aux; addSwapMoves(i, k, moves); } for (int p = k + 1; p < N; p++) { if ((base[p] & (1 << j)) > 0) { base[p] ^= base[k]; moves.add(p); moves.add(k); } } k++; } return base; } private void addSwapMoves(int a, int b, ArrayList<Integer> moves) { moves.add(a); moves.add(b); moves.add(b); moves.add(a); moves.add(a); moves.add(b); } } 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 InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(nextString()); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
Java
["2\n3 5\n6 0", "5\n0 0 0 0 0\n1 2 3 4 5", "3\n4 5 6\n1 2 3", "3\n1 2 3\n4 5 6"]
2 seconds
["2\n1 2\n2 2", "-1", "5\n3 1\n1 2\n2 2\n2 3\n3 1", "-1"]
NoteAssignment a ^= b denotes assignment a = a ^ b, where operation "^" is bitwise XOR of two integers.
Java 7
standard input
[ "constructive algorithms", "math", "matrices" ]
dcb55324681dd1916449570d6bc64e47
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
2,700
If there is no solution, output -1. If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
standard output
PASSED
242a9bd48de464f8d276104a7ddc60d9
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Review4 { public static void main(String[] args) { Scanner UserInput = new Scanner(System.in); int num = UserInput.nextInt(); ArrayList<Integer> list = new ArrayList<>(num); ArrayList<Integer> sorted = new ArrayList<>(num); for (int i = 0; i < num; i++) { int temp = UserInput.nextInt(); list.add(temp); sorted.add(temp); } Collections.sort(sorted); int count1 = 0; int count2 = num - 1; while ((count1 < num) && (list.get(count1).equals(sorted.get(count1)))) count1++; if (count1 == num) { System.out.println("yes"); System.out.println(1 + " " + 1); System.exit(0); } while ((count2 >= 0) && (list.get(count2).equals(sorted.get(count2)))) count2--; ArrayList<Integer> last = new ArrayList<>(num); for (int i = 0; i < count1; i++) last.add(list.get(i)); for (int i = count2; i >= count1; i--) last.add(list.get(i)); for (int i = count2 + 1; i < num; i++) last.add(list.get(i)); for (int i = 0; i < num; i++) { if (!last.get(i).equals(sorted.get(i))) { System.out.println("no"); System.exit(0); } } System.out.println("yes"); System.out.println((count1 + 1) + " " + (count2 + 1)); UserInput.close(); } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
226a2434fb7912c9b7a63df4375a3352
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); try { int size = scanner.nextInt (); Integer[] raw = new Integer[size]; Integer[] sorted = new Integer[size]; for (int index = 0; index < size; index++) { // O(N) raw[index] = scanner.nextInt (); sorted[index] = raw[index]; } Arrays.sort (sorted, new Comparator<Integer> () { // O (NlogN) @Override public int compare (Integer number1, Integer number2) { return number1.compareTo (number2); } }); int left = -1, right = -1; for (int index = 0; index < size; index++) { // O (N) if (sorted[index] != raw[index]) { if (left == -1) { left = index; } else { right = index; } } } if (left == -1 && right == -1) { System.out.println ("yes"); System.out.println ("1 1"); return; } for (int indexLeft = left, indexRight = right; indexLeft < indexRight; indexLeft++, indexRight--) { // O (right - 1) int swap = raw[indexLeft]; raw[indexLeft] = raw[indexRight]; raw[indexRight] = swap; } for (int index = 0; index < size; index++) { // O (right - 1) if (!sorted[index].equals (raw[index])) { System.out.println ("no"); return; } } System.out.println ("yes"); System.out.println ((left + 1) + " " + (right + 1)); return; } catch (Exception ex) { ex.printStackTrace (); } finally { scanner.close (); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
fa15ccf36cba3b60094cde8716f77f96
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class One1 { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); long[] a = new long[n]; for(int i = 0;i<n;i++) { a[i] = Long.parseLong(st.nextToken()); } int s=1; int e=1; boolean can = true; for(int i =0;i+1<n && can;i++) { if(a[i] > a[i+1]) { if(s!= 1 || e!=1) { can = false; break; } s = i+1; e= i+2; int k; for(k = i+1;k+1<n;k++) { if(k==n-2) { //if((a[i] >a[k+1])) //can = false; if((i!=0)&&(a[k+1] < a[i-1])) { can = false; } } if(a[k] < a[k+1]) { if((a[i] >a[k+1])) can = false; if((i!=0)&&(a[k] < a[i-1])) { can = false; } break; } e = k+2; } i = k; } } Arrays.sort(a); for(int i = 0;i+1<n;i++) { if(a[i] == a[i+1]) { can = false; break; } } if(can) { System.out.println("yes"); System.out.println(s + " " + e); } else { System.out.println("no"); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
fea9f91384a5040524f3ee09fad76450
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class NewClass { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a, int p, int r) { if( p < r ) { int q = (p + r) / 2; mergeSort(a, p, q); mergeSort(a, q + 1, r); merge(a, p, q, r); } } static void merge(int[] a, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] L = new int[n1+1], R = new int[n2+1]; for(int i = 0; i < n1; i++) L[i] = a[p + i]; for(int i = 0; i < n2; i++) R[i] = a[q + 1 + i]; L[n1] = R[n2] = INF; for(int k = p, i = 0, j = 0; k <= r; k++) if(L[i] <= R[j]) a[k] = L[i++]; else a[k] = R[j++]; } public static boolean sieve(int n){ int a[] = new int[n+1]; for (int i = 2; i <= n; i++) a[i]=1; for (int i = 2; i <=Math.sqrt(n); i++) { if (a[i]==1) { for (int k = 2; i*k <=n; k++) { a[i*k]=0; } } } return a[n]==1; } public static boolean zero(long f){ long w=f; int co=0; while (w>0) { if (w%2==0) { co++; w/=2; } if (co>1) { return false; } } return true; } public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); OutputWriter or = new OutputWriter(System.out); int n = in.nextInt(),co=0; int[] a=new int[n],b=new int[n]; List<Integer> s= new ArrayList(); for (int i = 0; i < n; i++)a[i]=b[i]=in.nextInt(); Arrays.sort(b); for (int i = 0; i < n; i++) { if (a[i]!=b[i]) { co++; s.add(i+1); } } int q=0,w=0; if (co!=0) { q=s.get(0); w=s.get(s.size()-1); } boolean z=true; for (int i = q-1; i < w-1; i++) { if (a[i]<a[i+1]) { z=false; break; } } if (z) { if (co==0) { or.print("yes"+"\n"+"1 1"); } else{ or.print("yes"+"\n"+s.get(0)+" "+s.get(s.size()-1)); } } else or.print("no"); or.flush(); } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) { throw new InputMismatchException(); } if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
f21add6b5a37fb09257043dc6c1e2c50
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.Scanner; public class HelloWorld{ public static void main(String []args){ int n,a[]; Scanner in = new Scanner(System.in); n=in.nextInt(); a=new int[n]; for(int i=0;i<n;i++)a[i]=in.nextInt(); int count=0,i1=0,i2=0,max=0,min=0,m1=0,m2=0; if(n<2){ System.out.println("yes"); System.out.println(1+" "+1); } else{ for(int i=1;i<n-1;i++){ if(a[i]<a[i-1]&&a[i]<a[i+1]){ count++; min=a[i]; m2=a[i+1]; i2=i; } if(a[i]>a[i-1]&&a[i]>a[i+1]){ count++; max=a[i]; m1=a[i-1]; i1=i; } } if(count==0){ if(a[0]>a[1]){ System.out.println("yes"); System.out.println(1+" "+n); } else if(a[0]<a[1]){ System.out.println("yes"); System.out.println(1+" "+1); } else System.out.println("no"); } if(count==1){ if(a[0]>a[1]&&m2>a[0]){ System.out.println("yes"); System.out.println(1+" "+(i2+1)); } else if(a[n-1]<a[n-2]&&m1<a[n-1]){ System.out.println("yes"); System.out.println((i1+1)+" "+n); } else System.out.println("no"); } if(count==2){ if(max<m2&&min>m1){ System.out.println("yes"); System.out.println((i1+1)+" "+(i2+1)); } else System.out.println("no"); } if(count>2)System.out.println("no"); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
977ae249bc66a48cd2c7b8020e36b8be
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.math.*; import java.util.*; public class BruteForce { public static Scanner in =new Scanner(System.in); /* public static String getB(int n,int l){ StringBuilder build=new StringBuilder(); while(n>0){ if(n%2==0) build.append(0); else build.append(1); n/=2; } if(build.toString().length()<l){ int dif=Math.abs(build.toString().length()-l); for(int i=1;i<=dif;i++) build.append("0"); } return build.reverse().toString(); } public static int getN(String a){ StringBuilder build=new StringBuilder(a); String z=build.reverse().toString(); int sum=0; for(int i=0;i<z.length();i++){ int c=Integer.parseInt(z.charAt(i)+""); int ans=(int) (c*Math.pow(2,i)); sum+=ans; } return sum; } public static int bs(int[]z,int x){ int ans=-1; int l=0; int r=z.length-1; while(l<=r&&r<z.length){ int mid=l+(r-l)/2; if(z[mid]==x){ ans=mid; break; } else if(z[mid]>x){ r=mid-1; } else if(z[mid]<x){ l=mid+1; } } return ans; } public static long getLCM(LinkedList<Integer> list){ long lcm=1; for(int i=0;i<list.size();i++){ lcm=Algo.lcm(lcm,list.get(i)); } return lcm; }*/ public static boolean check(int[]a,int[]b){ boolean ans=true; for(int i=0;i<a.length;i++) if(a[i]!=b[i]){ ans=false; break; } return ans; } public static void main(String[] args){ int n=in.nextInt(); int[]z=new int[n]; int[]test=new int[n]; for(int i=0;i<n;i++){ z[i]=in.nextInt(); test[i]=z[i]; } Arrays.sort(test); int idx=-1;int j=-1; for(int i=0;i<n;i++){ int curZ=z[i]; int curT=test[i]; if(curZ!=curT){ idx=i; break; } } for(int i=n-1;i>=0;i--){ int curZ=z[i]; int curT=test[i]; if(curZ!=curT){ j=i; break; } } if(idx==-1&&j==-1){ System.out.println("yes"); System.out.println("1 1"); } else{ ArrayList<Integer>list=new ArrayList<Integer>(); for(int i=j;i>=idx;i--) list.add(z[i]); int index=0; for(int i=idx;i<=j;i++) z[i]=list.get(index++); if(check(z,test)){ System.out.println("yes"); System.out.println((idx+1)+" "+(j+1)); } else System.out.println("no"); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
e3cd162916e50b00fa9f76d8d8b9179f
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.Scanner; public class SorttheArray451b { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[]x=new int[n]; boolean tt = true; int z=0; int b=0; for (int i = 0; i < n; i++) { x[i]=in.nextInt(); } for (int i = 0; i <n-1; i++) { if (x[i]>x[i+1]) { z=i+1;break; }} for (int j=n-1; j>0;j--) { if (x[j]<x[j-1]) { b=j+1;break; } } if(z==0||b==0){ System.out.println("yes"); System.out.println("1 1"); return; } for (int i = z-1,j=b-1;i<j; i++,j--) { int temp = x[i]; x[i]=x[j]; x[j]=temp; } for (int i = 0; i < x.length-1; i++) { if (x[i]>x[i+1]) { tt=false;break; } } if (tt) { System.out.println("yes"); System.out.println(z+" "+b); }else System.out.println("no"); } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
7caa42b83fd5265410190d859287892d
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author uuu */ public class SorttheArray451b { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[]x=new int[n]; int[]y=new int[n]; int[]d=new int[n]; boolean tt = true; ArrayList<Integer> z=new ArrayList<>(); ArrayList<Integer> f=new ArrayList<>(); for (int i = 0; i < n; i++) { x[i]=in.nextInt(); y[i]=x[i]; d[i]=x[i]; } Arrays.sort(y); for (int i = 0,j=n-1; i <n-1; i++,j--) { if (x[i]>x[i+1]) { z.add(i+1); } if (x[j]<x[j-1]) { f.add(j+1); } } if (z.size()==0||f.size()==0) { System.out.println("yes"); System.out.println("1 1"); return; } for (int i = z.get(0)-1,j=f.get(0)-1;i<j; i++,j--) { int temp = d[i]; d[i]=d[j]; d[j]=temp; } for (int i = 0; i < x.length; i++) { if (y[i]!=d[i]) { tt=false;break; } } if (tt) { System.out.println("yes"); System.out.println(z.get(0)+" "+f.get(0)); }else System.out.println("no"); } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
8106152794dd0928acaeebb11721ea9e
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
/* package whatever; // 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 final class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String s1[] = br.readLine().split(" "); int a[] = new int[n]; for(int i = 0; i < n ; i++) { a[i] = Integer.parseInt(s1[i]); } int l = 0; int f = 0; int v = 0; int q = 0; inner: for(int i = 0 ;i < n - 1 ; i++){ //System.out.println(i); if(a[i] < a[i+1]) continue; else { if(v == 0){ v++; l = i; outer: for(int j = i ; j < n-1 ; j++){ //System.out.println(j); if(a[j] > a[j+1]){ i = j; f = j+1; continue; } else{ f = j; i = j - 1; //System.out.println("here"); Arrays.sort(a,l,f+1); //System.out.println("here2222"); break outer; } } Arrays.sort(a,l,f+1); } else { q = 1; System.out.println("no"); break inner; } } } if( q==0) { if (l == 0){ System.out.println("yes"); System.out.println((l+1) + " "+(f+1)); } //System.out.printf("%s\n",Arrays.toString(a)); if (l > 0 && (a[l] > a[l - 1])){ System.out.println("yes"); System.out.println((l+1) + " "+(f+1)); //System.out.printf("%s\n",Arrays.toString(a)); } if(l > 0 && (a[l] < a[l-1])) System.out.println("no"); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
186e1a469f3e5c9a4c26d2f945e97e21
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class B451 { 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) { //code by sushant kumar FastReader in=new FastReader(); int n=in.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=in.nextLong(); } int l=-1; int r=-1; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { l=i; break; } } for(int i=n-1;i>=1;i--) { if(arr[i]<arr[i-1]) { r=i; break; } } if(l==-1&&r==-1) { System.out.println("yes"); System.out.print(1+" "+1); } else if(l+r==-1) { System.out.println("no"); } else { int flag=1; if(l!=0) { if(arr[r]<arr[l-1]) { flag=0; } } if(flag==1&&r!=n-1) { if(arr[l]>arr[r+1]) flag=0; } if(flag==1) { for(int i=l;i<=r-1;i++) { if(arr[i]<arr[i+1]) { flag=0; break; } } } if(flag==1) { System.out.println("yes"); System.out.print(l+1+" "+(r+1)); } else System.out.println("no"); } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
216b9b87544c43d25cceabe7cea512ec
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.Scanner; public class CF451B { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] dat = new int[n]; for (int i = 0; i < dat.length; i++) { dat[i]=s.nextInt(); } if(n==1) { System.out.println("yes\n1 1"); }else { int from = -1; int to = -1; boolean sorted = false; boolean impossible=false; int maxfound=0; for (int i = 0; i < dat.length-1; i++) { if(dat[i]>dat[i+1]) { if(sorted) { impossible=true; break; }else { if(from==-1) { maxfound=dat[i]; from=i; to=i; }else { to++; } } }else if(from!=-1&&!sorted) { to=i; sorted=true; }else if(maxfound>dat[i]) { impossible=true; break; } } if(to==n-2&&dat[n-1]<dat[n-2]) { to++; }else if(to==n-2&&dat[n-1]<maxfound){ impossible=true; } if(from>0) { if(dat[from-1]>dat[to]) { impossible=true; } } if(from==-1&&to==-1) { from=0; to=0; } if(impossible) { System.out.println("no"); }else { System.out.println("yes\n"+(from+1)+" "+(to+1)); } } } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
0f4995ffe775cfc7baba834d0e0fef31
train_000.jsonl
1406215800
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
256 megabytes
import java.util.*; public class Main { void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining e * lements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void main(String[] args) { // TODO Auto-generated method stub Main m = new Main(); Scanner g = new Scanner(System.in); int s = g.nextInt(); long[] arr = new long[s]; long[] ar = new long[s]; int t=0; int t1=0; int c=0; int c1,c2; c1=c2=0; boolean b= true; for(int i=0;i<s;i++){ arr[i] = g.nextInt(); ar[i ]= arr[i]; } //ar = arr; m.sort(arr,0,s-1); for(int i=0;i<s;i++){ if(arr[i] != (ar[i])){ t=i; //System.out.println (t); break; } } for(int i=s-1;i>0;i--){ if(arr[i] != ar[i]){ t1=i; //System.out.println (t1); break; } } //System.out.println (t); //System.out.println (t1); c = t1-t; c1 = t+1; c2 = t1+1; for(int i=t; t1>=i;i++,t1--){ long temp = ar[i] ; ar[i] = ar[t1]; ar[t1] = temp; } for(int i=0;i<s;i++){ //System.out.println (ar[i]); } for(int i=0;i<s;i++){ if(ar[i]!=arr[i]){ b = false; } } if(b){ if(c1<c2){ System.out.println ("yes"); System.out.println (c1+" "+c2); }else{ System.out.println ("yes"); System.out.println (c2+" "+c1); } }else{ System.out.println ("no"); } //System.out.println((max)); } }
Java
["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"]
1 second
["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"]
NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
Java 8
standard input
[ "implementation", "sortings" ]
c9744e25f92bae784c3a4833c15d03f4
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
1,300
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
standard output
PASSED
f86b60f9afb5f928b12e9fde708c17b4
train_000.jsonl
1600094100
You are given an array $$$a$$$ consisting of $$$n$$$ integers. We denote the subarray $$$a[l..r]$$$ as the array $$$[a_l, a_{l + 1}, \dots, a_r]$$$ ($$$1 \le l \le r \le n$$$).A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array $$$[1, 2, 2, 2, 1, 1, 2, 2, 2]$$$ has three good subarrays: $$$a[1..6] = [1, 2, 2, 2, 1, 1]$$$; $$$a[2..4] = [2, 2, 2]$$$; $$$a[7..9] = [2, 2, 2]$$$. Calculate the number of good subarrays of the given array $$$a$$$.
512 megabytes
//package ecr95; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class G { InputStream is; PrintWriter out; String INPUT = ""; void solve() { // 2:45 int n = ni(); int[] a = na(n); Random gen = new Random(); long[] zob = new long[2*(n+1)]; for(int i = 0;i < 2*(n+1)-1;i++)zob[i] = gen.nextLong(); int[][] b = makeBuckets(a, n); int[] bp = new int[n+1]; int[] f = new int[n+1]; Map<Long, Integer> map = new HashMap<>(); long h = 0; map.merge(h, 1, Integer::sum); long ans = 0; int p = 0; long[] hs = new long[n+1]; for(int i = 0;i < n;i++){ f[a[i]]++; if(f[a[i]] == 1){ h ^= zob[a[i]]; }else if(f[a[i]] == 2){ h ^= zob[a[i]] ^ zob[a[i]+n]; }else{ f[a[i]] = 0; h ^= zob[a[i]+n]; } hs[i+1] = h; bp[a[i]]++; if(bp[a[i]] >= 4){ while(p <= b[a[i]][bp[a[i]]-4]){ map.merge(hs[p], -1, Integer::sum); p++; } } ans += map.getOrDefault(h, 0); map.merge(h, 1, Integer::sum); } out.println(ans); } public static int[][] makeBuckets(int[] a, int sup) { int n = a.length; int[][] bucket = new int[sup+1][]; int[] bp = new int[sup+1]; for(int i = 0;i < n;i++)bp[a[i]]++; for(int i = 0;i <= sup;i++)bucket[i] = new int[bp[i]]; for(int i = n-1;i >= 0;i--)bucket[a[i]][--bp[a[i]]] = i; return bucket; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new G().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 char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["9\n1 2 2 2 1 1 2 2 2", "10\n1 2 3 4 1 2 3 1 2 3", "12\n1 2 3 4 3 4 2 1 3 4 2 1"]
5 seconds
["3", "0", "1"]
null
Java 11
standard input
[ "data structures", "two pointers", "hashing", "divide and conquer" ]
fc91e26a2cae7ef51f61114e4bf428eb
The first line contains one integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le n$$$).
2,500
Print one integer — the number of good subarrays of the array $$$a$$$.
standard output
PASSED
d4805203dc0a7f53ec2b07111672bd78
train_000.jsonl
1468137600
Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her!
256 megabytes
import java.io.*; import java.util.*; public class TreeofLife { public static void main(String args[])throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); //int m = Integer.parseInt(st.nextToken()); Graph nerve = new Graph(n); for(int i=0;i<n-1;i++) { st = new StringTokenizer(br.readLine()); int v = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken()); nerve.addEdge(v-1,w-1); } System.out.println(nerve.lifepath()); } } class Graph { ArrayList<Integer> adj[]; public Graph(int v) { adj = (ArrayList<Integer>[])new ArrayList[v]; for(int i=0;i<v;i++) adj[i] = new ArrayList<Integer>(); } public void addEdge(int v, int w) { adj[w].add(v); adj[v].add(w); } public int V() { return adj.length; } public int lifepath() { int count=0; for(int i=0;i<adj.length;i++) { count+=((adj[i].size()-1)*adj[i].size())/2; } return count; } }
Java
["4\n1 2\n1 3\n1 4", "5\n1 2\n2 3\n3 4\n3 5"]
2 seconds
["3", "4"]
NoteIn the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Java 11
standard input
[]
fdd50853348b6f297a62a3b729d8d4a5
The first line of the input contains a single integer n – the number of vertices in the tree (1 ≤ n ≤ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≤ a &lt; b ≤ n). It is guaranteed that the input represents a tree.
1,300
Print one integer – the number of lifelines in the tree.
standard output
PASSED
5c91fbc7395f5d53d12632f6c53b0159
train_000.jsonl
1468137600
Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her!
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); Vector<Integer> graph[]; graph = new Vector[110000]; for(int i=0;i<110000;++i){ graph[i]=new Vector<>(); } boolean []visit=new boolean[110000]; Arrays.fill(visit, false); for(int i=0;i<n-1;++i){ int x=in.nextInt(), y=in.nextInt(); graph[x].add(y); graph[y].add(x); } int ans = 0; for(int i=1;i<=n;++i){ int deg = graph[i].size(); ans += ((deg*(deg-1))/2); } System.out.println(ans); } }
Java
["4\n1 2\n1 3\n1 4", "5\n1 2\n2 3\n3 4\n3 5"]
2 seconds
["3", "4"]
NoteIn the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Java 11
standard input
[]
fdd50853348b6f297a62a3b729d8d4a5
The first line of the input contains a single integer n – the number of vertices in the tree (1 ≤ n ≤ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≤ a &lt; b ≤ n). It is guaranteed that the input represents a tree.
1,300
Print one integer – the number of lifelines in the tree.
standard output
PASSED
071ec6a480ff06d632363e5b520b1982
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); ArrayList<String[]> a = new ArrayList<>(); for(int i=0; i<t; i++) { int n = sc.nextInt(); String[] s = new String[n]; for(int j=0; j<n; j++) s[j] = sc.next(); a.add(s); } for(int i=0; i<t; i++) System.out.println(check(a.get(i))); sc.close(); } private static String check(String[] s) { int n = s.length; HashMap<Character, Integer> hmap = new HashMap<>(); for(int i=0; i<n; i++) { for(char c : s[i].toCharArray()) hmap.put(c, hmap.getOrDefault(c, 0) + 1); } for(Map.Entry<Character, Integer> entry : hmap.entrySet() ) { if(entry.getValue() %n != 0) return "NO"; } return "YES"; } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
3a508d60613bd5f8c5272aec91649c0f
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); ArrayList<String[]> a = new ArrayList<>(); for(int i=0; i<t; i++) { int n = sc.nextInt(); String[] s = new String[n]; for(int j=0; j<n; j++) s[j] = sc.next(); a.add(s); } for(int i=0; i<t; i++) System.out.println(check(a.get(i))); sc.close(); } private static String check(String[] s) { int n = s.length; int[] a = new int[26]; for(int i=0; i<n; i++) { for(char c : s[i].toCharArray()) { int num = c; a[num-97]++; } } for(int num : a) { if(num %n != 0) return "NO"; } return "YES"; } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
bfadce30097e58355bfe010314746d34
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; public class JugglingLetters { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); scanner.nextLine(); int letters[] = new int[26]; boolean cando = true; for (int l = 0; l < n; l++) { String str = scanner.nextLine(); for (int i = 0; i < str.length(); ++i) { letters[str.charAt(i) - 'a']++; } } for (int i = 0; i < 26; ++i) { if (letters[i] % n != 0) cando = false; } if (cando) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
f948ffda64b5d596f2993185cae6d40d
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
//package com.cf.r666; import java.io.BufferedReader; import java.io.File; 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.Arrays; import java.util.StringTokenizer; public class A { FastScanner in; PrintWriter out; public static void main(String[] args) { new A().solve(); } private void solve() { in = new FastScanner(System.in); out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] count = new int[32]; for (int j = 0; j < n; j++) { in.next().chars().forEach(c -> count[c - 'a']++); } if (Arrays.stream(count).allMatch(c -> c % n == 0)) out.println("YES"); else out.println("NO"); } out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
56c1150ca5e473a44224697ae5b013a2
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.Scanner; public class JugglingLetters { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); int numero, diferente, m = 1; numero = entrada.nextInt(); for (int i = 0; i < numero; i++) { int n = entrada.nextInt(); entrada.nextLine(); String nombre="",nuevo; boolean comparar = true; for (int j = 0; j < n; j++) { String palabras = entrada.nextLine(); nombre = nombre + palabras; } nuevo = nombre; while (comparar == true) { int cont=0; for (int k = 0; k < nombre.length(); k++) { if (nombre.charAt(0) == nombre.charAt(k)) { cont = cont + 1; if(cont==nombre.length()){ nuevo=""; } nuevo = nuevo.replace(String.valueOf(nombre.charAt(k)), ""); } } if (cont % n != 0) { comparar = false; break; } if(nuevo==""){ break; } nombre = nuevo; } if(comparar==true){ System.out.println("YES"); } else{ if (comparar==false){ System.out.println("NO");} } } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
3f54e3ccf08ff078246ebea3025e8b8a
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class test { public static void main(String args[]) { try{ Scanner sc= new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int a[] = new int[256]; boolean check = true; int n = sc.nextInt(); int b = n; while(n-->0) { String s = sc.next(); for(int i=0;i<s.length();i++) { a[(int)s.charAt(i)]++; } } for(int i=0;i<256;i++) { if(a[i]%b!=0) { check=false; } } if(check==true) { System.out.println("YES"); } else System.out.println("NO"); } } catch( Exception e){ return;} } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
37b1a4d0091a4e763bb50089b3b10eaf
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.text.DecimalFormat; import java.util.stream.LongStream; import java.util.stream.IntStream; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ Naveen problem = new Naveen(sc); problem.solve(out,sc); } out.flush(); } } class Naveen { int n; String s; Naveen(FastScanner sc) { n = sc.nextInt(); // arr = sc.arrayInt(n); } void solve(PrintWriter out,FastScanner sc) { int[] arr = new int[26]; int i; for(i =0;i<n;i++){ s = sc.next(); for(int j =0;j<s.length();j++) arr[s.charAt(j)-'a']++; } int j; for(j =0;j<26;j++){ if(arr[j]%n!=0){ break; } } if(j==26){ out.println("YES"); }else{ out.println("NO"); } } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buffer[ptr++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public int[] arrayInt(int N) { int[] array = new int[N]; for (int i = 0; i < N; i++) { array[i] = nextInt(); } return array; } public long[] arrayLong(int N) { long[] array = new long[N]; for (int i = 0; i < N; i++) { array[i] = nextLong(); } return array; } public double[] arrayDouble(int N) { double[] array = new double[N]; for (int i = 0; i < N; i++) { array[i] = nextDouble(); } return array; } public String[] arrayString(int N) { String[] array = new String[N]; for (int i = 0; i < N; i++) { array[i] = next(); } return array; } public int randomInt() { Random r = new Random(); int value = r.nextInt((int) 1e6); System.out.println(value); return value; } public int[] randomInt(int N) { int[] array = new int[N]; Random r = new Random(); for (int i = 0; i < N; i++) { array[i] = r.nextInt((int) 1e6); } System.out.println(Arrays.toString(array)); return array; } } class My { public static long lower(long arr[],long 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 void ans(boolean b) { System.out.println(b ? "Yes" : "No"); } static void ANS(boolean b) { System.out.println(b ? "YES" : "NO"); } static String sort(String s) { char[] ch = s.toCharArray(); Arrays.sort(ch); return String.valueOf(ch); } static String reverse(String s) { return new StringBuilder(s).reverse().toString(); } static int[] reverse(int[] array) { for (int i = 0; i < array.length / 2; i++) { int temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static long[] reverse(long[] array) { for (int i = 0; i < array.length / 2; i++) { long temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static double[] reverse(double[] array) { for (int i = 0; i < array.length / 2; i++) { double temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static String[] reverse(String[] array) { for (int i = 0; i < array.length / 2; i++) { String temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static char[] reverse(char[] array) { for (int i = 0; i < array.length / 2; i++) { char temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static long min(long... numbers) { Arrays.sort(numbers); return numbers[0]; } static int min(int... numbers) { Arrays.sort(numbers); return numbers[0]; } static double min(double... numbers) { Arrays.sort(numbers); return numbers[0]; } static long max(long... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static int max(int... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static double max(double... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static int sum(long number) { int sum = 0; while (number > 0) { sum += number % 10; number /= 10; } return sum; } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
d4e25c838cf0214c05bbc8280394443b
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.text.DecimalFormat; import java.util.stream.LongStream; import java.util.stream.IntStream; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ Naveen problem = new Naveen(sc); problem.solve(out,sc); } out.flush(); } } class Naveen { int n; String s; Naveen(FastScanner sc) { n = sc.nextInt(); // arr = sc.arrayInt(n); } void solve(PrintWriter out,FastScanner sc) { int[] arr = new int[26]; int i; for(i =0;i<n;i++){ s = sc.next(); for(int j =0;j<s.length();j++) arr[s.charAt(j)-'a']++; } for(i =0;i<26;i++){ if(arr[i]%n!=0){ break; } } if(i==26){ out.println("YES"); }else{ out.println("NO"); } } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buffer[ptr++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public int[] arrayInt(int N) { int[] array = new int[N]; for (int i = 0; i < N; i++) { array[i] = nextInt(); } return array; } public long[] arrayLong(int N) { long[] array = new long[N]; for (int i = 0; i < N; i++) { array[i] = nextLong(); } return array; } public double[] arrayDouble(int N) { double[] array = new double[N]; for (int i = 0; i < N; i++) { array[i] = nextDouble(); } return array; } public String[] arrayString(int N) { String[] array = new String[N]; for (int i = 0; i < N; i++) { array[i] = next(); } return array; } public int randomInt() { Random r = new Random(); int value = r.nextInt((int) 1e6); System.out.println(value); return value; } public int[] randomInt(int N) { int[] array = new int[N]; Random r = new Random(); for (int i = 0; i < N; i++) { array[i] = r.nextInt((int) 1e6); } System.out.println(Arrays.toString(array)); return array; } } class My { public static long lower(long arr[],long 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 void ans(boolean b) { System.out.println(b ? "Yes" : "No"); } static void ANS(boolean b) { System.out.println(b ? "YES" : "NO"); } static String sort(String s) { char[] ch = s.toCharArray(); Arrays.sort(ch); return String.valueOf(ch); } static String reverse(String s) { return new StringBuilder(s).reverse().toString(); } static int[] reverse(int[] array) { for (int i = 0; i < array.length / 2; i++) { int temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static long[] reverse(long[] array) { for (int i = 0; i < array.length / 2; i++) { long temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static double[] reverse(double[] array) { for (int i = 0; i < array.length / 2; i++) { double temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static String[] reverse(String[] array) { for (int i = 0; i < array.length / 2; i++) { String temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static char[] reverse(char[] array) { for (int i = 0; i < array.length / 2; i++) { char temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return array; } static long min(long... numbers) { Arrays.sort(numbers); return numbers[0]; } static int min(int... numbers) { Arrays.sort(numbers); return numbers[0]; } static double min(double... numbers) { Arrays.sort(numbers); return numbers[0]; } static long max(long... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static int max(int... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static double max(double... numbers) { Arrays.sort(numbers); return numbers[numbers.length - 1]; } static int sum(long number) { int sum = 0; while (number > 0) { sum += number % 10; number /= 10; } return sum; } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
e2cfd1e99d857e887deed427b500d2ba
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.io.*; // for handling input/output import java.util.*; // contains Collections framework public class Solution { public static void main (String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); Map<Character, Integer> mp= new HashMap<>(); for(int i=0; i<n; i++){ String s = in.next(); for(int j=0; j<s.length(); j++){ char c = s.charAt(j); mp.put(c, mp.getOrDefault(c, 0)+1); } } boolean b = true; for(int i : mp.values()){ if(i%n != 0){ b = false; break; } } if(b) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
291f6956662fb7586d1925177e34f805
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF_A_1397 { public static void main(String[] args) throws IOException { long t = readLong(); TestLoop: for (int __ = 0; __ < t; __++) { long n = readLong(); if (n == 0) { System.out.println("YES"); continue; } int[] chCounts = new int[26]; for (int i = 0; i < n; i++) { char[] str = br.readLine().toCharArray(); for (char c: str) { chCounts[c - 'a']++; } } for (int count: chCounts) { if (count != 0 && count % n != 0) { System.out.println("NO"); continue TestLoop; } } System.out.println("YES"); } } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static long readLong() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return Long.parseLong(st.nextToken()); } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
e25b49b18ea26ff61225a581742359a0
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
//Codeforces 1397A import java.util.Scanner; public class CF1397A { static final Scanner SC = new Scanner(System.in); public static void main(String[] args) { int tests = SC.nextInt(); for (int t = 0; t < tests; ++t) { int numStrings = SC.nextInt(); String[] strings = new String[numStrings]; for (int i = 0; i < numStrings; ++i) strings[i] = SC.next(); boolean canEqualize = isEqualizingPossible(strings); out(canEqualize); } } // Checks and returns whether or not all the strings passed are composed of the same characters(same count as well) static boolean isEqualizingPossible(String[] strings) { int[] totalOccurrences = new int[26]; for (int i = 0; i < strings.length; ++i) { int[] charOccurrences = characterCount(strings[i]); for (int j = 0; j < totalOccurrences.length; ++j) totalOccurrences[j] += charOccurrences[j]; } for (int i = 0; i < totalOccurrences.length; ++i) if (totalOccurrences[i] % strings.length != 0) return false; return true; // Can share characters equally among all strings } // Returns an array with count of character occurrences in the string static int[] characterCount(String s) { int[] charCount = new int[26]; for (int i = 0; i < s.length(); ++i) charCount[s.charAt(i)-'a'] += 1; return charCount; } // Prints output corresponding to condition static void out(boolean condition) { if (condition) System.out.println("Yes"); else System.out.println("No"); } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
44a5cd30bc6fc78222e96a3661897ff4
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[30]; for (int j = 0; j < n; j++) { String s = sc.next(); for (int i = 0; i < s.length(); i++) { int index = s.charAt(i) - 97; arr[index]++; } } int flag = 0; for (int i = 0; i < 27; i++) { if (arr[i] != 0) { if (arr[i] % n != 0) { flag = 1; break; } } } if (flag == 1) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
21906a1eff54dd18c4f095a24e50f49a
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; import java.io.*; public class juggling { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); for(int i = 0; i < t; i++) { int n = Integer.parseInt(in.readLine()); HashMap<Character, Integer> freqs = new HashMap<Character, Integer>(); for(int j = 0; j < n; j++) { String s = in.readLine(); for(int k = 0; k < s.length(); k++) { char c = s.charAt(k); Integer oldcount = freqs.get(c); if(oldcount == null) { oldcount = 0; } freqs.put(c, oldcount + 1); } } boolean res = true; for(char c : freqs.keySet()) { int count = freqs.get(c); if(count % n != 0) { res = false; break; } } out.println(res ? "YES":"NO"); } in.close(); out.close(); } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
35639e7506aead5dc44ed35aa7993361
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.Scanner; public class Codeforcse { public static void main(String[] args) { Scanner o=new Scanner(System.in); int t=o.nextInt(); while(t>0) { int n=o.nextInt(); int ar[]=new int[26]; boolean done=false; for(int i=0;i<n;i++) { String s=o.next(); for(int j=0;j<s.length();j++) { ar[s.charAt(j)-'a']++; } } boolean ans =true; for(int i=0;i<26;i++) { if(ar[i]%n!=0) { ans=false; break; } } if(ans) { System.out.println("YES"); } else { System.out.println("NO"); } t--; } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
f3cc6101a3df4048b3035c8e8b28841d
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; import java.io.*; public class Main{ 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() ); } } public static void main(String[] args)throws IOException{ Reader.init(System.in); int t=Reader.nextInt(); while(t-->0){ int n=Reader.nextInt(); String[] arr=new String[n]; String s=""; for(int i=0;i<n;i++){ arr[i]=Reader.next(); s+=arr[i]; } char[] arri=s.toCharArray(); Arrays.sort(arri); int c=0; ArrayList<Integer> anss=new ArrayList<>(); for(int i=1;i<arri.length;i++){ if(arri[i]==arri[i-1]){ c++; } else{ anss.add(c+1); c=0; } } if(arri.length!=1){ if(arri[arri.length-1]==arri[arri.length-2]){ anss.add(c+1); } else{ anss.add(1); } } // System.out.println(anss.get(anss.size()-1)); int k=0; for(int i=0;i<anss.size();i++){ if(anss.get(i)%n!=0){ k=1; } } System.out.println((k==1)?"NO":"YES"); } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
952bef3f0a3077b893bc2e37d6025258
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; public class Cf131 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Cf131(),"Main",1<<27).start(); } static class Pair{ int nod; int ucn; Pair(int nod,int ucn){ this.nod=nod; this.ucn=ucn; } public static Comparator<Pair> wc = new Comparator<Pair>(){ public int compare(Pair e1,Pair e2){ //reverse order if(e1.ucn < e2.ucn) return 1; // 1 for swaping. else if (e1.ucn > e2.ucn) return -1; else{ // if(e1.siz>e2.siz) // return 1; // else // return -1; return 0; } } }; } public static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } ////recursive BFS public static int bfsr(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,int[] pre){ b[s]=true; int p = 0; int t = a[s].size(); for(int i=0;i<t;i++){ int x = a[s].get(i); if(!b[x]){ dist[x] = dist[s] + 1; p+= (bfsr(x,a,dist,b,pre)+1); } } pre[s] = p; return p; } //// iterative BFS public static int bfs(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,PrintWriter w){ b[s]=true; int siz = 0; Queue<Integer> q = new LinkedList<>(); q.add(s); while(q.size()!=0){ int i=q.poll(); Iterator<Integer> it = a[i].listIterator(); int z=0; while(it.hasNext()){ z=it.next(); if(!b[z]){ b[z]=true; dist[z] = dist[i] + 1; siz++; q.add(z); } } } return siz; } ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int defaultValue=0; int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); String[] s = new String[n]; int[] h = new int[201]; for(int i=0;i<n;i++){ s[i] = sc.next(); } for(int i=0;i<n;i++){ for(int j=0;j<s[i].length();j++){ h[(int)s[i].charAt(j)]++; } } boolean f = true; for(int i=0;i<201;i++){ if(h[i]%n!=0){ f = false; break; } } if(f)w.println("YES"); else w.println("NO"); } w.flush(); w.close(); } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
16241c768fa28bb1c7c8001059bb40b0
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.*; public class Main{ // public static public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); String[] arr= new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } HashMap<Character,Integer> map= new HashMap<Character,Integer>(); for(String str:arr){ char[] s= str.toCharArray(); for(Character x:s){ if(map.containsKey(x)){ map.put(x,map.get(x)+1); }else{ map.put(x,1); } } } int flag=0; for(Character c:map.keySet()){ if(map.get(c)%n!=0){ System.out.println("NO"); flag=1; break; } } if(flag==0) System.out.println("YES"); } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output
PASSED
a3f060bc5a08166337fbb5584619e36b
train_000.jsonl
1598798100
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?
256 megabytes
import java.util.Scanner; public class juggling { public static void main(String[] args) { int t; Scanner scan = new Scanner(System.in); t = scan.nextInt(); while (t--!=0) { int n = scan.nextInt(); int[] arr = new int[26]; for (int i = 0; i < arr.length; i++) { arr[i] = 0; } for (int i = 0; i < n; i++) { String str = scan.next(); for (int j = 0; j < str.length();j++) { int x = str.charAt(j); arr[x-97]+=1; } } boolean g = true; for (int j = 0; j < arr.length; j++) { if (arr[j]%n!=0) { g = false; break; } } if (g) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"]
1 second
["YES\nNO\nYES\nNO"]
NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $$$n$$$ strings equal.
Java 11
standard input
[ "greedy", "strings" ]
3d6cd0a82513bc2119c9af3d1243846f
The first line contains $$$t$$$ ($$$1 \le t \le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \le \lvert s_i \rvert \le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.
800
If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase.
standard output