exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
6a37ec035872211a47116e92cda13e99
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
// practice with kaiboy import java.io.*; import java.util.*; public class CF1492D extends PrintWriter { CF1492D() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1492D o = new CF1492D(); o.main(); o.flush(); } void main() { int a = sc.nextInt(); int b = sc.nextInt(); int k = sc.nextInt(); if (k > 0 && (a == 0 || b == 1 || k > a + b - 2)) { println("No"); return; } int n = a + b; byte[] xx = new byte[n]; Arrays.fill(xx, (byte) '0'); byte[] yy = new byte[n]; Arrays.fill(yy, (byte) '0'); xx[0] = yy[0] = '1'; b--; if (b > 0) { int j = n - 1, i = j - k; xx[i] = yy[j] = '1'; b--; for (int h = 1; h < n && b > 0; h++) if (h != i && h != j) { xx[h] = yy[h] = '1'; b--; } } println("Yes"); println(new String(xx)); println(new String(yy)); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
a485cddd9590f37bd8546319ab4905ac
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; public class D { Reader source; BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws Exception { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.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()); } public void solve() throws Exception { int a = nextInt(); int b = nextInt(); int k = nextInt(); if (k >= a + b) { out.println("No"); return; } if (a == 0 && k > 0) { out.println("No"); return; } if (b == 1 && k > 0) { out.println("No"); return; } if (b == 1) { if (k == 0) { out.println("Yes"); for (int i = 0; i < b; i++) out.print(1); for (int i = 0; i < a; i++) out.print(0); out.println(); for (int i = 0; i < b; i++) out.print(1); for (int i = 0; i < a; i++) out.print(0); out.println(); } else { out.println("No"); return; } } else if (b == 2) { if (k <= a) { out.println("Yes"); out.print(11); for (int i = 0; i < a; i++) out.print(0); out.println(); out.print(1); for (int i = 0; i < k; i++) out.print(0); out.print(1); for (int i = k; i < a; i++) out.print(0); out.println(); } else { out.println("No"); return; } } else { if (k <= a) { out.println("Yes"); for (int i = 0; i < b; i++) out.print(1); for (int i = 0; i < a; i++) out.print(0); out.println(); for (int i = 1; i < b; i++) out.print(1); for (int i = 0; i < k; i++) out.print(0); out.print(1); for (int i = k; i < a; i++) out.print(0); } else if (b + a - k > 1) { out.println("Yes"); for (int i = 0; i < b; i++) out.print(1); for (int i = 0; i < a; i++) out.print(0); out.println(); for (int i = 1; i < b + a - k; i++) out.print(1); out.print(0); for (int i = b + a - k; i < b; i++) out.print(1); for (int i = 1; i < a; i++) out.print(0); out.print(1); } else { out.println("No"); return; } out.println(); } } public void run() throws Exception { source = OJ ? new InputStreamReader(System.in) : new FileReader("D.in"); br = new BufferedReader(source); out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new D().run(); } private boolean OJ = System.getProperty("ONLINE_JUDGE") != null; }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
288b99971c2ad798294b230ef0417c3a
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; public class D{ public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader(".in")); // out = new PrintWriter(new FileWriter(".out")); read(); int a = RI(); int b = RI(); int k = RI(); //System.out.println(Long.toBinaryString(Long.parseLong("11111111111111111100000000000000000000",2) - Long.parseLong("11101111111111111100000000000000000001",2))); StringBuilder meat = new StringBuilder(""); if (a < k) { int ta = a; a-=k; meat.append("1"); b--; while (ta > 0) { ta--; meat.append(0); } StringBuilder prefix = new StringBuilder(); while (b-->0) prefix.append(1); while (ta-->0) prefix.append(0); char[] s1 = (prefix+""+meat).toCharArray(); char[] s2 = (prefix +""+meat.reverse()).toCharArray(); a = -a; for (int i = s2.length-1; i > 0 && a > 0; i--) { if (s2[i] == '0' && s2[i-1] == '1') { a--; s2[i] = '1'; s2[i-1] = '0'; } } if (a == 0 && s2[0] == '1') { out.println("Yes"); for (char c: s1) out.print(c); out.println(); for (char c: s2) out.print(c); out.println(); } else { out.println("No"); } } else if (b == 1 && k != 0) { out.println("No"); } else if (k == 0) { out.println("Yes"); for (int x =0 ; x < 2; x++) { for (int i= 0 ; i < b; i++) out.print(1); for (int i = 0; i < a; i++) out.print(0); out.println(); } } else { out.println("Yes"); meat.append("1"); b--; while (meat.length() < k+1) { a--; meat.append(0); } StringBuilder prefix = new StringBuilder(); while (b-->0) prefix.append(1); while (a-->0) prefix.append(0); out.println(prefix+""+meat); out.println(prefix+""+meat.reverse()); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static StringTokenizer st; static void read() throws IOException{st = new StringTokenizer(br.readLine());} static int RI() throws IOException{return Integer.parseInt(st.nextToken());} static long RL() throws IOException{return Long.parseLong(st.nextToken());} static double RD() throws IOException{return Double.parseDouble(st.nextToken());} }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
2889a06b69133a89fa6613db89ba33ca
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
//package Codeforces.Round704Div2; import java.io.*; import java.util.*; public class GeniusGambit { static void swap(int[] arr, int i, int j){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } public static void main(String[] args) throws IOException { Soumit sc = new Soumit(); int b = sc.nextInt(); int a = sc.nextInt(); int k = sc.nextInt(); if(a==0){ if(b==1 && k==0){ System.out.println("Yes"); System.out.println(0); System.out.println(0); } else{ System.out.println("No"); } System.exit(0); } else if(a==1){ if(k!=0) { System.out.println("No"); System.exit(0); } } else { if(k>=a+b-1){ System.out.println("No"); System.exit(0); } } if(b==0){ if(k==0){ System.out.println("Yes"); StringBuilder sb = new StringBuilder(); for(int i=0;i<a;i++) sb.append("1"); sb.append("\n"); for(int i=0;i<a;i++) sb.append("1"); sb.append("\n"); System.out.println(sb); } else{ System.out.println("No"); } System.exit(0); } StringBuilder sb = new StringBuilder(); int[] arr = new int[a+b]; sb.append("Yes\n"); for(int i=0;i<a;i++){ arr[i] = 1; } for(int i=a;i<a+b;i++){ arr[i] = 0; } for(int i: arr) sb.append(i); sb.append("\n"); if(k<=b){ swap(arr, a-1, a+k-1); } else{ k -= b; swap(arr, a-1, a+b-1); swap(arr, a-1, a-k-1); } for(int i: arr) sb.append(i); sb.append("\n"); System.out.println(sb); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { /*if (din == null) return;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
e4a8dfa09bf3b70768d3c52062bdbdaa
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.*; public class Main extends PrintWriter { static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main () { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); } void main() throws IOException { StringBuilder sb = new StringBuilder(); int t = 1; // t=i(s()[0]); // System.out.println(Math.log(10)); while(t-->0) { String[] s1 = s(); int a=i(s1[0]); int b=i(s1[1]);int k=i(s1[2]); if(a==0&&k>0){ System.out.println("No"); }else if(k==a+b){ System.out.printf("No"); } else if(k==0){ System.out.println("Yes"); StringBuilder sb1=new StringBuilder(); while(a>0){ a--;sb.append('0');sb1.append('0'); } while(b>0){ b--;sb.append("1");sb1.append(1); } System.out.println(sb.reverse()); System.out.println(sb1.reverse()); } else{ StringBuilder sb1=new StringBuilder(); while(a>k){ sb.append("0");sb1.append("0"); a--; } sb.append('0');sb1.append('1'); k-=1; while(a>1){ sb.append('0');sb1.append('0');a--; k--; } while(k>0){ k--; sb.append('1');sb1.append('1');b--; } sb.append('1');sb1.append('0'); b--; if(b==0){ System.out.println("No"); }else {System.out.println("Yes"); while (b > 0) { sb.append('1'); sb1.append('1'); b--; } System.out.println(sb.reverse()); System.out.println(sb1.reverse()); } } } // System.out.println(sb); } // System.out.println(sb); static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); } static int i(String ss) {return Integer.parseInt(ss); } static long l(String ss) {return Long.parseLong(ss); } public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }} public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=i(s2[i]); }} }class Pair{ int a,b; public Pair(int a,int b){ this.a=a;this.b=b; } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
3037a2e18407ce7f3266038702f48793
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.util.*; import java.io.*; public class _1492_D { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer line = new StringTokenizer(in.readLine()); int a = Integer.parseInt(line.nextToken()); int b = Integer.parseInt(line.nextToken()); int k = Integer.parseInt(line.nextToken()); if(a + b > 1 && k > a + b - 2) { out.println("NO"); }else if(b == a + b || b == 1) { if(k > 0) { out.println("NO"); }else { StringBuilder sb = new StringBuilder(); for(int i = 0; i < a + b; i++) { if(i < b) { sb.append(1); }else { sb.append(0); } } out.println("YES"); out.println(sb.toString()); out.println(sb.toString()); } }else { int[] x = new int[a + b]; int[] y = new int[a + b]; x[0] = 1; y[0] = 1; x[1] = 1; y[1 + k] = 1; int left = b - 2; for(int i = 2; i < a + b; i++) { if(i == 1 + k) continue; if(left > 0) { x[i] = 1; y[i] = 1; left--; } } StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for(int i = 0; i < a + b; i++) { sb1.append(x[i]); sb2.append(y[i]); } out.println("YES"); out.println(sb1.toString()); out.println(sb2.toString()); } in.close(); out.close(); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
3a0f71a986bfffd810ed874b9cf8b8b6
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DGeniussGambit solver = new DGeniussGambit(); solver.solve(1, in, out); out.close(); } static class DGeniussGambit { public void solve(int testNumber, InputReader in, OutputWriter out) { int a = in.nextInt(); int b = in.nextInt(); int k = in.nextInt(); int[] res1 = new int[a + b]; int[] res2 = new int[a + b]; for (int i = 0; i < b; i++) { res1[i] = 1; res2[i] = 1; } if (k == 0) { out.println("Yes"); for (int j = 0; j < a + b; j++) { out.print(res1[j]); } out.println(); for (int j = 0; j < a + b; j++) { out.print(res2[j]); } out.println(); return; } for (int i = 1; i < b; i++) { if (res1[i] == 1 && i + k < a + b && res1[i + k] == 0) { res2[i + k] = 1; res2[i] = 0; out.println("Yes"); for (int j = 0; j < a + b; j++) { out.print(res1[j]); } out.println(); for (int j = 0; j < a + b; j++) { out.print(res2[j]); } out.println(); return; } } out.println("No"); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println() { writer.println(); } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
023b80a8e7effe363035037ec2b561af
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //int cases = Integer.parseInt(br.readLine()); //o:while(cases-- > 0) { String[] str = br.readLine().split(" "); int a = Integer.parseInt(str[0]); int b = Integer.parseInt(str[1]); int k = Integer.parseInt(str[2]); StringBuffer buf = new StringBuffer(); int i = 0; if(a == 0) { if(k > 0) { System.out.println("NO"); return; } buf.append("YES\n"); for(i=0; i<b; i++) buf.append(1); buf.append("\n"); for(i=0; i<b; i++) buf.append(1); buf.append("\n"); System.out.print(buf); return; } if(b == 1) { if(k > 0) { System.out.println("NO"); return; } buf.append("YES\n1"); for(i=0; i<a; i++) buf.append(0); buf.append("\n1"); for(i=0; i<a; i++) buf.append(0); buf.append("\n"); System.out.print(buf); return; } if(k > a+b-2) { System.out.println("NO"); return; } if(k <= a) { buf.append("Yes\n"); int n=a+b,x[]=new int[n],y[]=new int[n]; for(i=0;i<b-1;i++) { x[i]=y[i]=1; } y[n-1]=1; x[n-1-k]=1; for(i=0;i<n;i++) buf.append(x[i]); buf.append("\n"); for(i=0;i<n;i++) buf.append(y[i]); buf.append("\n"); }else { buf.append("YES\n"); int n = a+b, x[] = new int[n], y[] = new int[n]; y[n-1] = 1; x[n-1-k] = 1; x[0] = y[0] = 1; int remb = b-2; for(i=n-1-k+1; i<n-1 && remb > 0; i++, remb--) { x[i] = y[i] = 1; } i = 1; while(remb > 0) { x[i] = y[i] = 1; i++; remb--; } for(i=0;i<n;i++) buf.append(x[i]); buf.append("\n"); for(i=0;i<n;i++) buf.append(y[i]); buf.append("\n"); } System.out.print(buf); //} } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
c29a001f17fb5bbee709bf34d8c98b7d
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.InputMismatchException; public class Main { static PrintWriter out; static Reader in; public static void main(String[] args) throws IOException { input_output(); Main solver = new Main(); solver.solve(); out.close(); out.flush(); } static long INF = (long)1e18; static int MAXN = (int)1e5 + 5; static int MOD = (int)1e9 + 7; static int q, t, n, m, k; static double pi = Math.PI; void solve() throws IOException { int zero = in.nextInt(), one = in.nextInt(), k = in.nextInt(); if (k == 0) { StringBuilder same = new StringBuilder(""); for (int i = 0; i < one; i++) same.append("1"); for (int i = 0; i < zero; i++) same.append("0"); out.println("Yes"); out.println(same); out.println(same); return; } else if (zero == 0 || one == 1 || one+zero-2 < k) { out.println("No"); return; } StringBuilder first = new StringBuilder("1"); StringBuilder second = new StringBuilder("1"); one--; first.append("1"); one--; second.append("0"); while (one+zero != 0) { if (k != 0) { if (k == 1) { first.append("0"); second.append("1"); zero--; k--; continue; } if (one != 0) { first.append("1"); second.append("1"); one--; } else { first.append("0"); second.append("0"); zero--; } k--; } else { if (one != 0) { first.append("1"); second.append("1"); one--; } else { first.append("0"); second.append("0"); zero--; } } } out.println("Yes"); out.println(first); out.println(second); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static void input_output() throws IOException { File f = new File("in.txt"); if (f.exists() && !f.isDirectory()) { in = new Reader(new FileInputStream("in.txt")); } else in = new Reader(); f = new File("out.txt"); if (f.exists() && !f.isDirectory()) { out = new PrintWriter(new File("out.txt")); } else out = new PrintWriter(System.out); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
89636ab74a6d320d66ddec66f9d56a5f
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; public class genius_gambit { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(),b=sc.nextInt(),k=sc.nextInt(); if(b==1) { if(k!=0) System.out.println("NO"); else { String x=""; x="1"+"0".repeat(a); System.out.println("YES\n"+x+"\n"+x); } } else if(k<=a) { System.out.println("YES"); String x="",y=""; x="1".repeat(b-1)+"0".repeat(a-k)+"1"+"0".repeat(k); y="1".repeat(b-1)+"0".repeat(a)+"1"; System.out.println(x+"\n"+y); } else if(k>a && (k-a+2)<=b && a>=1) { String x="",y=""; x="1".repeat(b)+"0".repeat(a); y="1".repeat(a+b-k-1)+"0"+"1".repeat(k-a)+"0".repeat(a-1)+"1"; System.out.println("YES\n"+x+"\n"+y); } else System.out.println("NO"); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
da064f24bdc4cdd72770ec952c188697
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; private static final String No = "No"; private static final String Yes = "Yes"; InputStream is; PrintWriter out; String INPUT = ""; private static long MOD = 1000000007; private static final int MAXN = 100000; void solve() { int T = 1;// ni(); for (int i = 0; i < T; i++) { solve(i); } } void solve(int T) { int n = ni(); int m = ni(); int k = ni(); char a[] = new char[n + m]; for (int i = 0; i < a.length; i++) a[i] = i < m ? '1' : '0'; if (k == 0) { out.println(Yes); out.println(new String(a)); out.println(new String(a)); return; } for (int i = 1; i + k < a.length; i++) { if (a[i] == '1' && a[i + k] == '0') { out.println(Yes); out.println(new String(a)); a[i] = '0'; a[i + k] = '1'; out.println(new String(a)); return; } } out.println(No); } public int solve(List<Integer> box, List<Integer> special) { Collections.sort(box); Collections.sort(special); int i = 0; int j = 0; int ans = 0; while (i < box.size() && j < special.size()) { if (box.get(i).equals(special.get(j))) { ans++; i++; j++; } else if (box.get(i) < special.get(j)) { i++; } else { j++; } } int max = ans; i = 0; for (j = 0; j < special.size(); j++) { while (i < box.size() && box.get(i) <= special.get(j)) { if (box.get(i).equals(special.get(j))) ans--; i++; } int bs = Collections.binarySearch(special, special.get(j) - i + 1); if (bs < 0) { bs = -bs - 1; } max = Math.max(max, ans + j - bs + 1); } return max; } // a^b long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Integer[] na2(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
d95c59e6f15fc70e2cfea200436caeca
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class problemD { static class Solution { void solve() { int a = fs.nextInt(), b = fs.nextInt(), k = fs.nextInt(); int n = a+b; int[] res = new int[n]; for (int i=n-1, j=0 ; j < k ; j++, i--) res[i] = 1; int[] low = new int[n]; low[0] = 1; for (int i=n-1, j=1, z=0 ; z < k && j < b ; j++, i--, z++) low[i] = 1; int count1 = 0; for (int x : low) if (x == 1) count1++; for (int i=1, j=count1; j < b; i++, j++) low[i] = 1; int[] up = new int[n]; int carry = 0; for (int i=n-1; i >= 0 ; i --) { int sum = res[i] + low[i] + carry; up[i] = sum&1; carry = sum >> 1; } if (carry != 0 || !valid(low, a, b) || !valid(up, a, b)) { out.println("No"); return; } out.println("Yes"); for (int x : up) out.print(x); out.println(); for (int x : low) out.print(x); out.println(); } boolean valid(int[] r, int zeros, int ones) { int nZeroes = 0; for (int x : r) if (x == 0) nZeroes++; return nZeroes == zeros && zeros+ones == r.length && r[0] == 1; } } public static void main(String[] args) throws Exception { int T = 1; Solution solution = new Solution(); for (int t = 0; t < T; t++) solution.solve(); out.close(); } static void debug(Object... O) { System.err.println("DEBUG: " + Arrays.deepToString(O)); } private static FastScanner fs = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); static class FastScanner { // Thanks SecondThread. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } ArrayList<Integer> readArrayList(int n) { ArrayList<Integer> a = new ArrayList<>(n); for (int i = 0 ; i < n; i ++ ) a.add(fs.nextInt()); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
9603bbb17ef115dc4c0b6bd7b1fc099d
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int[] cnt = sc.nextIntArray(2); int k = sc.nextInt(); cnt[1]--; int n = cnt[0] + cnt[1]; int[] x = new int[n], y = new int[n]; boolean can = true; if (k == 0) { for (int i = 0; i < cnt[1]; i++) x[i] = y[i] = 1; } else { int tmp = n - k - 1; if (tmp < 0 || cnt[0] == 0 || cnt[1] == 0) can = false; else { cnt[0]--; cnt[1]--; y[n - 1] = 1; x[tmp] = 1; for (int i = 0; i < n - 1; i++) { if (i == tmp) continue; if (cnt[0] == 0) x[i] = y[i] = 1; else cnt[0]--; } } } if (!can) out.println("No"); else { out.println("Yes"); out.print(1); for (int xx : x) out.print(xx); out.println(); out.print(1); for (int yy : y) out.print(yy); } out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(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 int[] nextIntArray(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } public long[] nextLongArray(int n) throws IOException { long[] ans = new long[n]; for (int i = 0; i < n; i++) ans[i] = nextLong(); return ans; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] ans = new Integer[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
483eba4e5baa7a8a68b2b112966bc6c9
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { long mod1 = (long) 1e9 + 7; int mod2 = 998244353; public void solve() throws Exception { int a=sc.nextInt(), b=sc.nextInt(), c=sc.nextInt(); int n=a+b; if(c==0) { out.println("YES"); for(int i=0;i<b;i++) { out.print(1); } for(int i=0;i<a;i++) { out.print(0); } out.println(); for(int i=0;i<b;i++) { out.print(1); } for(int i=0;i<a;i++) { out.print(0); } return; } if(a==0 || b==1) { out.println("NO"); return; } if(c>=n-1) { out.println("NO"); return; } int arr[]=new int[n]; int brr[]=new int[n]; Arrays.fill(arr, 0); Arrays.fill(brr, 0); arr[0]=1; brr[0]=1; arr[1]=1; brr[1+c]=1; b-=2; for(int i=2;i<n;i++) { if(b==0) break; if(i==1+c) continue; arr[i]=1; brr[i]=1; b--; } out.println("YES"); for(int i:arr) out.print(i); out.println(); for(int i:brr) out.print(i); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static long ncr(int n, int r, long p) { if (r > n) return 0l; if (r > n - r) r = n - r; long C[] = new long[r + 1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r] % p; } void sieveOfEratosthenes(boolean prime[], int size) { for (int i = 0; i < size; i++) prime[i] = true; for (int p = 2; p * p < size; p++) { if (prime[p] == true) { for (int i = p * p; i < size; i += p) prime[i] = false; } } } static int LowerBound(int a[], int x) { // smallest index having value >= x; returns 0-based index int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static int UpperBound(int a[], int x) {// biggest index having value <= x; returns 1-based index int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } public long power(long x, long y, long p) { long res = 1; // out.println(x+" "+y); x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public int[] readArray(int n) throws Exception { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
34677f32871a7b49d00c47dbd32c16ea
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
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.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.ListIterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; import java.util.TreeMap; import javax.swing.text.html.HTMLDocument.Iterator; /* * Printing Euler Tour in an Eulerian Graph */ public class E { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); if (k == 0) { int[] a = new int[n + m]; int[] b = new int[n + m]; for (int i = 0; i < a.length; i++) { if (i >= m) { a[i] = 0; b[i] = 0; } else { a[i] = 1; b[i] = 1; } } //////// pw.println("Yes"); for (int i = 0; i < a.length; i++) { pw.print(a[i]); } pw.println(); for (int i = 0; i < b.length; i++) { pw.print(b[i]); } pw.println(); } else if (n + m - 1 > k && n > 0 && m > 1) { int[] a = new int[n + m]; int[] b = new int[n + m]; for (int i = 0; i < Math.min(m - 1, k); i++) { a[k - i] = 1; b[k - i] = 1; } b[k] = 0; b[0] = 1; for (int i = 0; i < Math.max(m - k, 1); i++) { a[a.length - 1 - i] = 1; b[a.length - 1 - i] = 1; } // pw.println("Yes"); for (int i = 0; i < a.length; i++) { pw.print(a[a.length - i - 1]); } pw.println(); for (int i = 0; i < b.length; i++) { pw.print(b[b.length - i - 1]); } } else pw.println("No"); pw.flush(); // else if (n >= k && m >= 2) { // int[] a = new int[n + m]; // int[] b = new int[n + m]; // m--; // a[a.length - k - 1] = 1; // b[a.length - 1] = 1; // for (int i = 0; i < m; i++) { // a[i] = 1; // b[i] = 1; // } // pw.println("Yes"); // for (int i = 0; i < a.length; i++) { // pw.print(a[i]); // } // pw.println(); // for (int i = 0; i < b.length; i++) { // pw.print(b[i]); // } // pw.println(); // } else if (n >= 2 && m >= k) { // int[] a = new int[n + m]; // int[] b = new int[n + m]; // for (int i = 0; i < k - 1; i++) { // b[b.length - 1 - i] = 1; // } // a[a.length - k - 1] = 1; // for (int j = 1; j < k - 1; j++) { // a[a.length - 1 - j] = 1; // } // m -= k - 1; // for (int i = 0; i < m; i++) { // a[i] = 1; // b[i] = 1; // } // // // pw.println("Yes"); // for (int i = 0; i < a.length; i++) { // pw.print(a[i]); // } // pw.println(); // for (int i = 0; i < b.length; i++) { // pw.print(b[i]); // } // pw.println(); // } } static class pair implements Comparable<pair> { int x; int y; public pair(int d, int u) { x = d; y = u; } @Override public int compareTo(pair o) { return o.x - x; } } static ArrayList<Integer>[] adjList; static LinkedList<Integer> tour; static ArrayList<Integer>[] g; static ArrayList<Integer> primes; // generated by sieve /* * 1. Generating a list of prime factors of N */ static HashMap<Integer, Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { HashMap<Integer, Integer> factors = new HashMap<>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { int c = 0; while (N % p == 0) { N /= p; c++; } if (c > 0) factors.put(p, c); if (idx + 1 == primes.size()) break; p = primes.get(++idx); } if (N != 1) // last prime factor may be > sqrt(N) factors.put(N, 1); // for integers whose largest prime factor has a power of 1 return factors; } static int[] isComposite; static void sieve(int N) // O(N log log N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance { primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in // modified sieve isComposite[j] = 1; } } static HashSet<Integer> hs; static int[] a; static long[] cum; static void divide(int s, int e) { // System.out.println(s + " " + e); if (e == -1 || s > e) return; if (cum[e + 1] - cum[s] <= 1e9) hs.add((int) (cum[e + 1] - cum[s])); // System.out.println(cum[e + 1] - cum[s]); if (e == s) return; int temp = a[e] + a[s] >> 1; int lo = s; int hi = e; int ans = -1; while (lo <= hi) { int mid = lo + hi >> 1; if (a[mid] <= temp) { ans = mid; lo = mid + 1; } else hi = mid - 1; } if (ans == -1 || ans == e) return; divide(s, ans); divide(ans + 1, e); } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (int) ((res * 1l * x) % p); // y must be even now y = y >> 1; // y = y/2 x = (int) ((x * 1l * x) % p); } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (int) (((fac[n] * 1l * modInverse(fac[r], p)) % p * modInverse(fac[n - r], p) % p) % p); } static int[] fac; static void init() { fac = new int[200001]; fac[0] = 1; for (int i = 1; i <= 200000; i++) fac[i] = (int) ((fac[i - 1] * 1l * i) % mod); } static int mod = 1000000007; static long ans = 10000000000l; static void find(int i, int sum, String s, int t) { if (sum == t) { ans = Math.min(ans, Integer.parseInt(s)); return; } // System.out.println(s); if (i == 0) return; find(i - 1, sum, s, t); find(i - 1, sum + i, i + s, t); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(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 boolean ready() throws IOException { return br.ready(); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
dacac189da26b127916e5fc56b150262
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { private static void run(Reader in, PrintWriter out) throws IOException { int a = in.nextInt(); int b = in.nextInt(); int k = in.nextInt(); int n = a + b; boolean[] done = new boolean[n]; int[] s = new int[n]; int[] t = new int[n]; s[0] = t[0] = 1; done[0] = true; b--; if (k != 0) { if (b > 0 && a > 0 && n - k - 1 > 0) { s[n - k - 1] = 1; t[n - k - 1] = 0; s[n - 1] = 0; t[n - 1] = 1; done[n - k - 1] = done[n - 1] = true; b--; a--; } else { out.println("No"); return; } } for (int i = 0; i < n; i++) { if (done[i]) continue; if (b != 0) { s[i] = t[i] = 1; b--; } else { s[i] = t[i] = 0; } } out.println("Yes"); for (int i = 0; i < n; i++) { out.print(s[i]); } out.println(); for (int i = 0; i < n; i++) { out.print(t[i]); } out.println(); } public static void main(String[] args) throws IOException { Reader in = new Reader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); run(in, out); out.flush(); in.close(); out.close(); } static class Reader { BufferedReader reader; StringTokenizer st; Reader(InputStreamReader stream) { reader = new BufferedReader(stream, 32768); st = null; } void close() throws IOException { reader.close(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() throws IOException { return reader.readLine(); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
0c37a8c3c751ff713986b2c368a2ff5a
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer tok; public static void main(String[] args) throws Exception { solution(); } public static void solution() throws Exception { tok = new StringTokenizer(rd.readLine()); int a = Integer.parseInt(tok.nextToken()); int b = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); if((k <= a+b-2 && a > 0 && b > 1) || (k == 0 && (a == 0 || b == 1))) { wr.write("Yes"); wr.newLine(); int n = a+b, cIndex = 0; char[] x = new char[n]; char[] y = new char[n]; for(int i=0;i<n;i++) { if(i < b) { x[i] = '1'; y[i] = '1'; cIndex = i; // 가장끝에있는 1의 위치를 지정한다. } else { x[i] = '0'; y[i] = '0'; } } // 두 출력할 배열을 만든다. << 일단 최댓값으로 생성함. boolean[] pos = new boolean[n]; for(int i=1;i<n;i++) if(x[i] == '1') pos[i] = true; // y의 1이 움직일 수 있는 위치를 표현하기위한 pos 배열을 초기화 int kCount = 0 ,nIndex = cIndex; // 현재 kCount가 목표값 k에 맞을때까지 알고리즘을 반복해야함. while(kCount < k) { // System.out.println(Arrays.toString(y) +" 라운드 :: " + kCount); if(y[n-1] != '1') { y[cIndex++] = '0'; y[cIndex] = '1'; if(cIndex == n-1) { cIndex = nIndex; nIndex--; } } // 초깃값이 아직 끝에 들어가지 않았다면 초깃값을 지정해주어야한다. else { if(y[cIndex+1] == '0' && pos[cIndex+1]) { y[cIndex++] = '0'; y[cIndex] = '1'; } // 아직 움직일 수 있는 공간이 남아있는 경우 else { cIndex = nIndex; nIndex--; y[cIndex++] = '0'; y[cIndex] = '1'; } // 움직일 수 있는 공간이 없으면 nIndex로 현재위치를 조정한 후, 값을 적용해준다. } // 초깃값 지정이 끝나면 pos 위치에 따라서 내부조정이 필요하다. kCount++; } for(int i=0;i<n;i++) wr.write(x[i]); wr.newLine(); for(int i=0;i<n;i++) wr.write(y[i]); wr.newLine(); } else wr.write("No"); wr.flush(); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
54580294660c92398a09ede58f30df07
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.*; public class Main extends PrintWriter { static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main () { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); } void main() throws IOException { StringBuilder sb = new StringBuilder(); int t = 1; // t=i(s()[0]); // System.out.println(Math.log(10)); while(t-->0) { String[] s1 = s(); int a=i(s1[0]); int b=i(s1[1]);int k=i(s1[2]); if(a==0&&k>0){ System.out.println("No"); }else if(k==a+b){ System.out.printf("No"); } else if(k==0){ System.out.println("Yes"); StringBuilder sb1=new StringBuilder(); while(a>0){ a--;sb.append('0');sb1.append('0'); } while(b>0){ b--;sb.append("1");sb1.append(1); } System.out.println(sb.reverse()); System.out.println(sb1.reverse()); } else{ StringBuilder sb1=new StringBuilder(); while(a>k){ sb.append("0");sb1.append("0"); a--; } sb.append('0');sb1.append('1'); k-=1; while(a>1){ sb.append('0');sb1.append('0');a--; k--; } while(k>0){ k--; sb.append('1');sb1.append('1');b--; } sb.append('1');sb1.append('0'); b--; if(b==0){ System.out.println("No"); }else {System.out.println("Yes"); while (b > 0) { sb.append('1'); sb1.append('1'); b--; } System.out.println(sb.reverse()); System.out.println(sb1.reverse()); } } } // System.out.println(sb); } // System.out.println(sb); static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); } static int i(String ss) {return Integer.parseInt(ss); } static long l(String ss) {return Long.parseLong(ss); } public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }} public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=i(s2[i]); }} }class Pair{ int a,b; public Pair(int a,int b){ this.a=a;this.b=b; } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
3677c9c555342437a07a1a0bf76cf168
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
// Problem: D. Genius's Gambit // Contest: Codeforces - Codeforces Round #704 (Div. 2) // URL: http://codeforces.com/contest/1492/problem/D // Memory Limit: 512 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) import java.io.*; import java.util.*; public class Main { private static PrintWriter pw = new PrintWriter(System.out); private static InputReader sc = new InputReader(); private static final int intmax = Integer.MAX_VALUE, intmin = Integer.MIN_VALUE; static class InputReader{ private static BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tk; private void next()throws IOException{ while(tk == null || !tk.hasMoreTokens()) tk = new StringTokenizer(r.readLine()); } private int nextInt()throws IOException{ next(); return Integer.parseInt(tk.nextToken()); } private long nextLong()throws IOException{ next(); return Long.parseLong(tk.nextToken()); } private String readString()throws IOException{ next(); return tk.nextToken(); } private double nextDouble()throws IOException{ next(); return Double.parseDouble(tk.nextToken()); } private int[] intArray(int n)throws IOException{ next(); int arr[] = new int[n]; for(int i=0; i<n; i++) arr[i] = nextInt(); return arr; } private long[] longArray(int n)throws IOException{ next(); long arr[] = new long[n]; for(int i=0; i<n; i++) arr[i] = nextLong(); return arr; } } public static void main(String args[])throws IOException{ // int t = sc.nextInt(); int t = 1; while(t-->0) solve(); pw.flush(); pw.close(); } private static void solve()throws IOException{ int a = sc.nextInt(), b = sc.nextInt(), k = sc.nextInt(); if(k == 0){ pw.println("Yes"); for(int i = 0; i < b; i++) pw.print(1); for(int i = 0; i < a; i++) pw.print(0); pw.println(); for(int i = 0; i < b; i++) pw.print(1); for(int i = 0; i < a; i++) pw.print(0); } else if(k >= a + b - 1 || b == 1 || a == 0) pw.println("No"); else{ int ttl = a + b, res[][] = new int[2][ttl]; res[0][0] = 1; res[1][0] = 1; b--; res[0][ttl - k - 1] = 1; res[1][ttl - k - 1] = 0; res[0][ttl - 1] = 0; res[1][ttl - 1] = 1; b--; a--; for(int i = 1; i + 1< ttl; i++){ if(i != ttl - k - 1){ int no; if(a > 0){ no = 0; a--; } else{ no = 1; b--; } res[0][i] = no; res[1][i] = no; } } pw.println("Yes"); for(int i = 0; i < 2; i++){ for(int j = 0; j < res[i].length; j++) pw.print(res[i][j]); pw.println(); } } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
4bcd48e70fcd3aadd92fab252b2cc025
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
import java.util.Scanner; public class p1492D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(),b=sc.nextInt(),k=sc.nextInt(); if(k==0) { String s="1".repeat(b)+"0".repeat(a); System.out.println("YES "+s+" "+s); }else if(a==0||b==1||a+b<=k+1) System.out.println("NO"); else { int i=a+b-k-1; String s="1".repeat(b-1)+"0".repeat(a-1),x=s.substring(0,i),y=s.substring(i,s.length()); System.out.println("YES "+x+1+y+0+" "+x+0+y+1); } } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
608579f1ce2a9f22cc3a3912bca34341
train_109.jsonl
1614071100
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.
512 megabytes
//package round704; import java.io.*; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Queue; public class D { InputStream is; FastWriter out; String INPUT = ""; // 11110 // 10111 void solve() { int A = ni(), B = ni(), K = ni(); if(A > 0 && B > 1){ if(K > A+B-2){ out.println("No"); }else{ out.println("Yes"); char[] s = new char[A+B]; char[] t = new char[A+B]; Arrays.fill(s, '0'); Arrays.fill(t, '0'); s[0] = t[0] = '1'; int i = 1; for(;i <= B-2 && K < A+B-i-1;i++){ s[i] = t[i] = '1'; } s[i] = '1'; t[i+K] = '1'; B -= i+1; while(B > 0){ s[i+1] = t[i+1] = '1'; i++; B--; } out.println(new String(s)); out.println(new String(t)); } }else{ if(K == 0){ out.println("Yes"); for(int i = 0;i < B;i++)out.print(1); for(int i = 0;i < A;i++)out.print(0); out.println(); for(int i = 0;i < B;i++)out.print(1); for(int i = 0;i < A;i++)out.print(0); out.println(); }else{ out.println("No"); } } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["4 2 3", "3 2 1", "3 2 5"]
2 seconds
["Yes\n101000\n100001", "Yes\n10100\n10010", "No"]
NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer.
Java 11
standard input
[ "bitmasks", "constructive algorithms", "greedy", "math" ]
ea620a8dbef506567464dcaddcc2b34f
The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \leq a$$$; $$$1 \leq b$$$; $$$0 \leq k \leq a + b \leq 2 \cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.
1,900
If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them.
standard output
PASSED
80d494bbb70d41514b6e93662d4d4ebc
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.*; import java.util.*; public class Absent_Students { static double dist(triple a, triple b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); a = new int[n][m]; int[] diff = new int[n]; int max = 0; for (int i = 0; i < n; i++) { int c = 0; for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); if (a[i][j] != a[0][j]) c++; } diff[i] = c; max = Math.max(c, max); } // System.out.println(Arrays.toString(diff)); boolean f = false; if (max >= 5) ; else if (max == 4) { out: for (int i = 1; i < diff.length; i++) { if (diff[i] == 4) { int[] diffpos = new int[4]; int idx = 0; for (int j = 0; j < m; j++) { if (a[0][j] != a[i][j]) { diffpos[idx++] = j; } } f = helper4(i, diffpos); if (f) break out; } } } else if (max == 3) { out: for (int i = 1; i < diff.length; i++) { if (diff[i] == 3) { int[] diffpos = new int[3]; int idx = 0; for (int j = 0; j < m; j++) { if (a[0][j] != a[i][j]) { diffpos[idx++] = j; } } // System.out.println(i); // System.out.println(Arrays.toString(diffpos)); f = helper3(i, diffpos); if (f) break out; } } } else { f = true; ans = a[0].clone(); } if (f) { pw.println("Yes"); for (int i = 0; i < ans.length; i++) { pw.print(ans[i] + " "); } } else { pw.println("No"); } pw.flush(); } static int a[][]; static int[] ans; static boolean helper4(int cur, int[] diffpos) { for (int i = 0; i < diffpos.length - 1; i++) { for (int j = i + 1; j < diffpos.length; j++) { int[] temp = a[0].clone(); temp[diffpos[i]] = a[cur][diffpos[i]]; temp[diffpos[j]] = a[cur][diffpos[j]]; boolean f = check(temp, 2); if (f) { ans = temp.clone(); return true; } } } return false; } static boolean helper3(int cur, int[] diffpos) { for (int i = 0; i < diffpos.length; i++) { int[] temp = a[0].clone(); temp[diffpos[i]] = a[cur][diffpos[i]]; // System.out.println(Arrays.toString(temp)+" this the first one"); boolean f = check(temp, 1); // System.out.println(Arrays.toString(temp)); if (f) { ans = temp.clone(); return true; } } return false; } static boolean check(int[] temp, int changes) { for (int i = 0; i < a.length; i++) { ArrayList<Integer> diff = new ArrayList<>(); for (int j = 0; j < temp.length; j++) { if (a[i][j] != temp[j]) { diff.add(j); } } // System.out.println(diff); if (diff.size() > 2) { if (changes == 2 || diff.size() > 3) return false; for (int x : diff) { int[] tt = temp.clone(); tt[x] = a[i][x]; boolean f = check(tt, 2); if (f) { temp[x] = a[i][x]; return true; } } return false; } } return true; } static final int INF = (int) 1e9; static int V, s, t, res[][]; // input static ArrayList<Integer>[] adjList; // input static int[] ptr, dist; static long dinic() // O(V^2E) { long mf = 0; while (bfs()) { ptr = new int[V]; int f; while ((f = dfs(s, INF)) != 0) mf += f; } return mf; } static boolean bfs() { dist = new int[V]; Arrays.fill(dist, -1); dist[s] = 0; Queue<Integer> q = new LinkedList<Integer>(); q.add(s); while (!q.isEmpty()) { int u = q.remove(); if (u == t) return true; for (int v : adjList[u]) if (dist[v] == -1 && res[u][v] > 0) { dist[v] = dist[u] + 1; q.add(v); } } return false; } static int dfs(int u, int flow) { if (u == t) return flow; for (int i = ptr[u]; i < adjList[u].size(); i = ++ptr[u]) { int v = adjList[u].get(i); if (dist[v] == dist[u] + 1 && res[u][v] > 0) { int f = dfs(v, Math.min(flow, res[u][v])); if (f > 0) { res[u][v] -= f; res[v][u] += f; return f; } } } return 0; } static class triple implements Comparable<triple> { double x; double y; int z; int idx; public triple(double a, double b, int c, int d) { x = a; y = b; z = c; idx = d; } @Override public int compareTo(triple o) { // TODO Auto-generated method stub return 0; } } static int[] isComposite; static void sieve(int N) // O(N log log N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number // primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance { // primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in // modified sieve isComposite[j] = 1; } } static class pair implements Comparable<pair> { int x, y; int w; pair(int s, int d) { x = s; y = d; } pair(int s, int d, int c) { x = s; y = d; w = c; } @Override public int compareTo(pair p) { return (x == p.x && y == p.y) ? 0 : 1; } @Override public String toString() { return x + " " + y; } } static long mod(long ans, int mod) { return (ans % mod + mod) % mod; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int log(int n, int base) { int ans = 0; while (n + 1 > base) { ans++; n /= base; } return ans; } static long pow(long b, long e) { long ans = 1; while (e > 0) { if ((e & 1) == 1) ans = ((ans * 1l * b)); e >>= 1; { } b = ((b * 1l * b)); } return ans; } static int powmod(int b, long e, int mod) { int ans = 1; b %= mod; while (e > 0) { if ((e & 1) == 1) ans = (int) ((ans * 1l * b) % mod); e >>= 1; b = (int) ((b * 1l * b) % mod); } return ans; } static int ceil(int a, int b) { int ans = a / b; return a % b == 0 ? ans : ans + 1; } static long ceil(long a, long b) { long ans = a / b; return a % b == 0 ? ans : ans + 1; } static HashMap<Integer, Integer> compress(int a[]) { TreeSet<Integer> ts = new TreeSet<>(); HashMap<Integer, Integer> hm = new HashMap<>(); for (int x : a) ts.add(x); for (int x : ts) { hm.put(x, hm.size() + 1); } return hm; } // Returns nCr % p static int C[]; static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; if (C[r] != 0) return C[r]; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public int[] intArr(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } return a; } public long[] longArr(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < a.length; i++) { a[i] = nextLong(); } return a; } 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(); } } public static void shuffle(Integer[] x2) { int n = x2.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = x2[i]; x2[i] = x2[r]; x2[r] = tmp; } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
3963d2236cb11e3c900af35f256da7a3
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; public final class E { static int[] b = new int[(int) (2e5 + 5)]; private static String yes(int m, int[] row) { final StringBuilder res = new StringBuilder(); res.append("Yes\n"); for (int i = 0; i < m; i++) { if (row[i] == 0) { row[i] = 1; } res.append(row[i]); res.append(' '); } res.append('\n'); return res.toString(); } private static void check(int p, int[][] a, int n, int m) { if (p == n) { System.out.println(yes(m, b)); System.exit(0); } final List<Integer> fr = new ArrayList<>(); int mistakes = 0; for (int i = 0; i < m; i++) { if (b[i] == 0) { fr.add(i); } else if (b[i] != a[p][i]) { mistakes++; } } if (mistakes > 2) { return; } final int k = fr.size(); if (mistakes + k <= 2) { check(p + 1, a, n, m); return; } for (int mask = 0; mask < (1 << k); mask++) { int ppc = mistakes; for (int i = 0; i < k; i++) { if ((mask & (1 << i)) != 0) { ppc++; } else { b[fr.get(i)] = a[p][fr.get(i)]; } } if (ppc == 2) { check(p + 1, a, n, m); } for (int i = 0; i < k; i++) { b[fr.get(i)] = 0; } } } private static void trySolve(int p, int q, int[][] a, int n, int m) { final List<Integer> bad = new ArrayList<>(); for (int i = 0; i < m; i++) { if (a[p][i] != a[q][i]) { bad.add(i); } } final int k = bad.size(); for (int id : new int[] { p, q }) { for (int mask = 0; mask < (1 << k); mask++) { if (Integer.bitCount(mask) != 2) { continue; } final List<Integer> mist = new ArrayList<>(); for (int i = 0; i < k; i++) { if ((mask & (1 << i)) != 0) { mist.add(bad.get(i)); } } for (int i = 0; i < m; i++) { if (i == mist.get(0) || i == mist.get(1)) { b[i] = 0; } else { b[i] = a[id][i]; } } check(0, a, n, m); } } } public static void main(String[] args) throws IOException { final FastReader fs = new FastReader(); final int n = fs.nextInt(); final int m = fs.nextInt(); final int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = fs.nextInt(); } } for (int i = 1; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { diff += a[0][j] != a[i][j] ? 1 : 0; } if (diff > 4) { System.out.println("No"); return; } if (diff <= 2) { continue; } trySolve(0, i, a, n, m); System.out.println("No"); return; } System.out.println(yes(m, a[0])); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
1eb71299ee79062b9b8772ba11638c39
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; public final class E { static int[] b = new int[(int) (2e5 + 5)]; private static String yes(int m, int[] row) { final StringBuilder res = new StringBuilder(); res.append("Yes\n"); for (int i = 0; i < m; i++) { if (row[i] == 0) { row[i] = 1; } res.append(row[i]); res.append(' '); } res.append('\n'); return res.toString(); } private static void check(int p, int[][] a, int n, int m) { if (p == n) { System.out.println(yes(m, b)); System.exit(0); } final List<Integer> fr = new ArrayList<>(); int mistakes = 0; for (int i = 0; i < m; i++) { if (b[i] == 0) { fr.add(i); } else if (b[i] != a[p][i]) { mistakes++; } } if (mistakes > 2) { return; } final int k = fr.size(); if (mistakes + k <= 2) { check(p + 1, a, n, m); return; } for (int mask = 0; mask < (1 << k); mask++) { int ppc = mistakes; for (int i = 0; i < k; i++) { if ((mask & (1 << i)) != 0) { ppc++; } else { b[fr.get(i)] = a[p][fr.get(i)]; } } if (ppc == 2) { check(p + 1, a, n, m); } for (int i = 0; i < k; i++) { b[fr.get(i)] = 0; } } } private static void trySolve(int p, int q, int[][] a, int n, int m) { final List<Integer> bad = new ArrayList<>(); for (int i = 0; i < m; i++) { if (a[p][i] != a[q][i]) { bad.add(i); } } final int k = bad.size(); for (int id : new int[] { p, q }) { for (int mask = 0; mask < (1 << k); mask++) { final List<Integer> mist = new ArrayList<>(); for (int i = 0; i < k; i++) { if ((mask & (1 << i)) != 0) { mist.add(bad.get(i)); } } // bitcount 2? if (mist.size() != 2) { continue; } for (int i = 0; i < m; i++) { if (i == mist.get(0) || i == mist.get(1)) { b[i] = 0; } else { b[i] = a[id][i]; } } check(0, a, n, m); } } } public static void main(String[] args) throws IOException { final FastReader fs = new FastReader(); final int n = fs.nextInt(); final int m = fs.nextInt(); final int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = fs.nextInt(); } } for (int i = 1; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { diff += a[0][j] != a[i][j] ? 1 : 0; } if (diff > 4) { System.out.println("No"); return; } if (diff <= 2) { continue; } trySolve(0, i, a, n, m); System.out.println("No"); return; } System.out.println(yes(m, a[0])); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
b668aab0a1a724ca1e076ffcd33eca45
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.*; import java.util.*; public class Main { static int n, m; static int[][] a; static int ans[]; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); n = sc.nextInt(); m = sc.nextInt(); a = new int[n][]; for (int i = 0; i < n; i++) a[i] = sc.nextIntArray(m); boolean can = solve(1, 2); if (can) { out.println("Yes"); for (int j = 0; j < m; j++) out.print(ans[j] + " "); } else out.println("No"); out.flush(); out.close(); } static boolean solve(int i, int rem) { if (rem < 0) return false; if (i == n) { if (check()) { ans = a[0].clone(); return true; } return false; } boolean can = false; ArrayList<Integer> nomatch = new ArrayList<>(); for (int j = 0; j < m; j++) if (a[0][j] != a[i][j]) nomatch.add(j); if (nomatch.size() > 4) return false; if (nomatch.size() <= 2) return solve(i + 1, rem); if (nomatch.size() == 3) { for (int idx = 0; idx < nomatch.size() && !can; idx++) { int j = nomatch.get(idx); int tmp = a[0][j]; a[0][j] = a[i][j]; can = solve(i + 1, rem - 1); a[0][j] = tmp; } } for (int idx = 0; idx < nomatch.size() && !can; idx++) { int j = nomatch.get(idx); int tmp = a[0][j]; a[0][j] = a[i][j]; for (int idx2 = idx + 1; idx2 < nomatch.size() && !can; idx2++) { int k = nomatch.get(idx2); int tmp2 = a[0][k]; a[0][k] = a[i][k]; can = solve(i + 1, rem - 2); a[0][k] = tmp2; } a[0][j] = tmp; } return can; } static boolean check() { for (int i = 1; i < n; i++) { int cnt = 0; for (int j = 0; j < m; j++) if (a[0][j] != a[i][j]) { cnt++; if (cnt == 3) return false; } } return true; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(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 int[] nextIntArray(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } public long[] nextLongArray(int n) throws IOException { long[] ans = new long[n]; for (int i = 0; i < n; i++) ans[i] = nextLong(); return ans; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] ans = new Integer[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
4effa33b08f6edbee2b5efbd0a95c182
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
// Problem: E. Almost Fault-Tolerant Database // Contest: Codeforces - Codeforces Round #704 (Div. 2) // URL: http://codeforces.com/contest/1492/problem/E // Memory Limit: 512 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) import java.io.*; import java.util.*; public class CF { private static PrintWriter pw = new PrintWriter(System.out); private static InputReader sc = new InputReader(); private static final int intmax = Integer.MAX_VALUE, intmin = Integer.MIN_VALUE; static class InputReader{ private static BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tk; private void next()throws IOException{ while(tk == null || !tk.hasMoreTokens()) tk = new StringTokenizer(r.readLine()); } private int nextInt()throws IOException{ next(); return Integer.parseInt(tk.nextToken()); } } public static void main(String args[])throws IOException{ int t = 1; while(t-->0) solve(); pw.flush(); pw.close(); } private static void printyes(int res[]){ pw.println("Yes"); for(int j = 0; j < res.length; j++) pw.print(+res[j]+" "); } private static boolean step4(int res[], int arr[][]){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(arr[i][j] != res[j]) count++; } if(count > 2) return false; } printyes(res); return true; } private static boolean step3(int res[], int arr[][], int pos){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(arr[i][j] != res[j] && j != pos) count++; } if(count > 2) return false; else if(count == 2){ int tmp = res[pos]; res[pos] = arr[i][pos]; boolean r = step4(res, arr); res[pos] = tmp; return r; } } printyes(res); return true; } private static boolean step2(int res[], int arr[][], int pos1, int pos2){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(j != pos1 && j != pos2 && res[j] != arr[i][j]) count++; } if(count > 2) return false; else if(count == 2){ int temp1 = res[pos1], temp2 = res[pos2]; res[pos1] = arr[i][pos1]; res[pos2] = arr[i][pos2]; boolean r = step4(res, arr); res[pos1] = temp1; res[pos2] = temp2; return r; } else if(count == 1){ int temp = res[pos1]; res[pos1] = arr[i][pos1]; boolean r = step3(res, arr, pos2); res[pos1] = temp; if(r) return true; temp = res[pos2]; res[pos2] = arr[i][pos2]; r = step3(res, arr, pos1); res[pos2] = temp; return r; } } printyes(res); return true; } private static boolean step1(int res[], int arr[][]){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ ArrayList<Integer> list = new ArrayList<>(); for(int j = 0; j < m; j++){ if(res[j] != arr[i][j]) list.add(j); } if(list.size() > 4) return false; else if(list.size() >= 3){ for(int p = 0; p + 1 < list.size(); p++){ for(int q = p + 1; q < list.size(); q++){ if(step2(res, arr, list.get(p), list.get(q))) return true; } } return false; } } printyes(res); return true; } private static void solve()throws IOException{ int n = sc.nextInt(), m = sc.nextInt(), arr[][] = new int[n][m], res[] = new int[m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ arr[i][j] = sc.nextInt(); res[j] = arr[0][j]; } } if(!step1(res, arr)) pw.println("No"); } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
38c8180cf41ddeece0b8f4c98f2a3f14
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
// Problem: E. Almost Fault-Tolerant Database // Contest: Codeforces - Codeforces Round #704 (Div. 2) // URL: http://codeforces.com/contest/1492/problem/E // Memory Limit: 512 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) import java.io.*; import java.util.*; public class CF { private static PrintWriter pw = new PrintWriter(System.out); private static InputReader sc = new InputReader(); private static final int intmax = Integer.MAX_VALUE, intmin = Integer.MIN_VALUE; static class InputReader{ private static BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tk; private void next()throws IOException{ while(tk == null || !tk.hasMoreTokens()) tk = new StringTokenizer(r.readLine()); } private int nextInt()throws IOException{ next(); return Integer.parseInt(tk.nextToken()); } } public static void main(String args[])throws IOException{ // int t = sc.nextInt(); int t = 1; while(t-->0) solve(); pw.flush(); pw.close(); } private static void printyes(int res[]){ pw.println("Yes"); for(int j = 0; j < res.length; j++) pw.print(+res[j]+" "); } private static boolean step4(int res[], int arr[][]){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(arr[i][j] != res[j]) count++; } if(count > 2) return false; } printyes(res); return true; } private static boolean step3(int res[], int arr[][], int pos){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(arr[i][j] != res[j] && j != pos) count++; } if(count > 2) return false; else if(count == 2){ int tmp = res[pos]; res[pos] = arr[i][pos]; // if(!step4(res, arr)) // return false; boolean r = step4(res, arr); res[pos] = tmp; return r; } } printyes(res); return true; } private static boolean step2(int res[], int arr[][], int pos1, int pos2){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ int count = 0; for(int j = 0; j < m; j++){ if(j != pos1 && j != pos2 && res[j] != arr[i][j]) count++; } if(count > 2) return false; else if(count == 2){ int temp1 = res[pos1], temp2 = res[pos2]; res[pos1] = arr[i][pos1]; res[pos2] = arr[i][pos2]; // if(!step4(res, arr)) // return false; boolean r = step4(res, arr); // return step4(res, arr); res[pos1] = temp1; res[pos2] = temp2; return r; } else if(count == 1){ int temp = res[pos1]; res[pos1] = arr[i][pos1]; boolean r = step3(res, arr, pos2); res[pos1] = temp; if(r) return true; temp = res[pos2]; res[pos2] = arr[i][pos2]; // if(!step3(res, arr, pos1)) // return false; r = step3(res, arr, pos1); res[pos2] = temp; return r; } } printyes(res); return true; } private static boolean step1(int res[], int arr[][]){ int n = arr.length, m = arr[0].length; for(int i = 0; i < n; i++){ ArrayList<Integer> list = new ArrayList<>(); for(int j = 0; j < m; j++){ if(res[j] != arr[i][j]) list.add(j); } if(list.size() > 4) return false; else if(list.size() >= 3){ for(int p = 0; p + 1 < list.size(); p++){ for(int q = p + 1; q < list.size(); q++){ if(step2(res, arr, list.get(p), list.get(q))) return true; } } return false; } } printyes(res); return true; } private static void solve()throws IOException{ int n = sc.nextInt(), m = sc.nextInt(), arr[][] = new int[n][m], res[] = new int[m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ arr[i][j] = sc.nextInt(); res[j] = arr[0][j]; } } if(!step1(res, arr)) pw.println("No"); } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
cc1d9d1e5060292acdf91a9bcdb5f2fa
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { private static void run(Reader in, PrintWriter out) throws IOException { int n = in.nextInt(); int m = in.nextInt(); int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = in.nextInt(); } } int max_diff = 0; int max_pos = -1; for (int i = 1; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (a[i][j] != a[0][j]) { diff++; } } if (diff > 4) { out.println("No"); return; } else if (diff > max_diff) { max_diff = diff; max_pos = i; } } if (max_diff <= 2) { out.println("Yes"); for (int i = 0; i < m; i++) { out.print(a[0][i]); out.print(' '); } out.println(); } else if (max_diff == 4) { int[] ans = handle_4(n, m, a, max_pos); show(out, ans); } else { int[] ans = handle_3(n, m, a, max_pos); show(out, ans); } } private static void show(PrintWriter out, int[] ans) { if (ans == null) { out.println("No"); } else { out.println("Yes"); for (int an : ans) { out.print(an); out.print(' '); } out.println(); } } private static int[] handle_3(int n, int m, int[][] a, int max_pos) { int[] ans = new int[m]; for (int pos_0 = 0; pos_0 < 3; pos_0++) { for (int pos_1 = 0; pos_1 < 3; pos_1++) { if (pos_0 == pos_1) continue; int diff_index = 0; int p = -1; for (int i = 0; i < m; i++) { if (a[0][i] == a[max_pos][i]) { ans[i] = a[0][i]; } else { if (pos_0 == diff_index) { ans[i] = a[0][i]; } else if (pos_1 == diff_index) { ans[i] = a[max_pos][i]; } else { ans[i] = -1; p = i; } diff_index++; } } boolean current_flag = true; for (int i = 0; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (a[i][j] != ans[j] && ans[j] != -1) { diff++; } if (diff > 2) { current_flag = false; } } if (diff == 2 && ans[p] == -1) { ans[p] = a[i][p]; } } if (current_flag) { if (ans[p] == -1) { ans[p] = a[0][p]; } return ans; } } } return null; } private static int[] handle_4(int n, int m, int[][] a, int max_pos) { int[] ans = new int[m]; for (int index = 0; index < (1 << 4); index++) { int state = index; int[] count = new int[2]; for (int i = 0; i < 4; i++) { int choose = state & (1 << i); count[choose == 0 ? 0 : 1]++; } if (count[0] > 2 || count[1] > 2) continue; state = index; int current_choose = 0; for (int i = 0; i < m; i++) { if (a[0][i] == a[max_pos][i]) { ans[i] = a[0][i]; } else { int choose = state & (1 << current_choose); ans[i] = choose == 0 ? a[0][i] : a[max_pos][i]; current_choose++; } } boolean current_flag = true; for (int i = 0; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (ans[j] != a[i][j]) { diff++; } if (diff > 2) { current_flag = false; } } } if (current_flag) { return ans; } } return null; } public static void main(String[] args) throws IOException { Reader in = new Reader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); run(in, out); out.flush(); in.close(); out.close(); } static class Reader { BufferedReader reader; StringTokenizer st; Reader(InputStreamReader stream) { reader = new BufferedReader(stream, 32768); st = null; } void close() throws IOException { reader.close(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() throws IOException { return reader.readLine(); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
f8bfb71df5140dbab494c28470ebed4c
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
// Don't place your source in a package import java.util.*; import java.lang.*; import java.io.*; import java.math.*; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); static class FastScanner {//scanner from SecondThread BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(System.out); int T=1; for(int t=0;t<T;t++){ int n=Int();int m=Int(); int A[][]=new int[n][m]; for(int i=0;i<A.length;i++){ for(int j=0;j<A[0].length;j++){ A[i][j]=Int(); } } Solution sol=new Solution(); sol.solution(out,A); } out.flush(); } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution{ public void solution(PrintWriter out,int A[][]){ boolean v=dfs(out,A,0); if(v){ out.println("YES"); for(int i:A[0]){ out.print(i+" "); } } else{ out.println("NO"); } } public boolean dfs(PrintWriter out,int A[][],int cnt){ if(cnt>2)return false; for(int i=1;i<A.length;i++){ List<Integer>list=new ArrayList<>(); for(int j=0;j<A[0].length;j++){ if(A[0][j]!=A[i][j]){ list.add(j); } } int size=list.size(); if(size>=5)return false; if(size<=2)continue; for(int index:list){ int old=A[0][index]; A[0][index]=A[i][index]; if(dfs(out,A,cnt+1))return true; A[0][index]=old; } return false; } return true; } public void yes(PrintWriter out,int A[]){ out.println("YES"); for(int i:A){ out.print(i+" "); } out.println(); } public void no(PrintWriter out){ out.print("NO"); } } //1 2 3 1 1 1
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
10fdac8ac0b59a1f625eeec990d89576
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { private static void run(Reader in, PrintWriter out) throws IOException { int n = in.nextInt(); int m = in.nextInt(); int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = in.nextInt(); } } int max_diff = 0; int max_pos = -1; for (int i = 1; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (a[i][j] != a[0][j]) { diff++; } } if (diff > 4) { out.println("No"); return; } else if (diff > max_diff) { max_diff = diff; max_pos = i; } } if (max_diff <= 2) { out.println("Yes"); for (int i = 0; i < m; i++) { out.print(a[0][i]); out.print(' '); } out.println(); } else if (max_diff == 4) { int[] ans = handle_4(n, m, a, max_pos); show(out, ans); } else { int[] ans = handle_3(n, m, a, max_pos); show(out, ans); } } private static void show(PrintWriter out, int[] ans) { if (ans == null) { out.println("No"); } else { out.println("Yes"); for (int an : ans) { out.print(an); out.print(' '); } out.println(); } } private static int[] handle_3(int n, int m, int[][] a, int max_pos) { int[] ans = new int[m]; for (int pos_0 = 0; pos_0 < 3; pos_0++) { for (int pos_1 = 0; pos_1 < 3; pos_1++) { if (pos_0 == pos_1) continue; int diff_index = 0; int p = -1; for (int i = 0; i < m; i++) { if (a[0][i] == a[max_pos][i]) { ans[i] = a[0][i]; } else { if (pos_0 == diff_index) { ans[i] = a[0][i]; } else if (pos_1 == diff_index) { ans[i] = a[max_pos][i]; } else { ans[i] = -1; p = i; } diff_index++; } } boolean current_flag = true; for (int i = 0; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (a[i][j] != ans[j] && ans[j] != -1) { diff++; } if (diff > 2) { current_flag = false; } } if (diff == 2 && ans[p] == -1) { ans[p] = a[i][p]; } } if (current_flag) { if (ans[p] == -1) { ans[p] = a[0][p]; } return ans; } } } return null; } private static int[] handle_4(int n, int m, int[][] a, int max_pos) { int[] ans = new int[m]; for (int index = 0; index < (1 << 4); index++) { int state = index; int[] count = new int[2]; for (int i = 0; i < 4; i++) { int choose = state & (1 << i); count[choose == 0 ? 0 : 1]++; } if (count[0] > 2 || count[1] > 2) continue; state = index; int current_choose = 0; for (int i = 0; i < m; i++) { if (a[0][i] == a[max_pos][i]) { ans[i] = a[0][i]; } else { int choose = state & (1 << current_choose); ans[i] = choose == 0 ? a[0][i] : a[max_pos][i]; current_choose++; } } boolean current_flag = true; for (int i = 0; i < n; i++) { int diff = 0; for (int j = 0; j < m; j++) { if (ans[j] != a[i][j]) { diff++; } if (diff > 2) { current_flag = false; } } } if (current_flag) { return ans; } } return null; } public static void main(String[] args) throws IOException { Reader in = new Reader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); run(in, out); out.flush(); in.close(); out.close(); } static class Reader { BufferedReader reader; StringTokenizer st; Reader(InputStreamReader stream) { reader = new BufferedReader(stream, 32768); st = null; } void close() throws IOException { reader.close(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() throws IOException { return reader.readLine(); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
e3518c8b090939523edce8d0044895a5
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; private static final String No = "No"; private static final String Yes = "Yes"; InputStream is; PrintWriter out; String INPUT = ""; private static long MOD = 1000000007; private static final int MAXN = 100000; void solve() { int T = 1;// ni(); for (int i = 0; i < T; i++) { solve(i); } } int a[][]; int n; int m; void solve(int T) { n = ni(); m = ni(); a = na(n, m); if (go(0)) { out.println(YES); for (int j = 0; j < m; j++) out.print(a[0][j]+" "); return; } out.println(NO); } boolean go(int dep) { if (dep > 2) return false; for (int i = 1; i < n; i++) { int k = 0; for (int j = 0; j < m; j++) k += a[0][j] != a[i][j] ? 1 : 0; if (k > 4) return false; if (k > 2) { for (int j = 0; j < m; j++) if (a[0][j] != a[i][j]) { int t = a[0][j]; a[0][j] = a[i][j]; if (go(dep + 1)) { return true; } a[0][j] = t; } return false; } } return true; } // a^b long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Integer[] na2(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
23596b0a54911364bcde4394065217b0
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
//package round704; import java.io.*; import java.util.*; public class E { InputStream is; FastWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(); int[][] a = new int[n][]; for(int i = 0;i < n;i++){ a[i] = na(m); } if(m <= 2){ out.println("Yes"); out.println(a[0]); return; } int[][] imaps = new int[m][]; for(int i = 0;i < m;i++){ int[] t = new int[n]; for(int j = 0;j < n;j++){ t[j] = a[j][i]; } imaps[i] = shrinkX(t); for(int j = 0;j < n;j++){ a[j][i] = t[j]; } } Arrays.sort(a, (x, y) -> { for(int i = 0;i < x.length;i++){ if(x[i] != y[i])return x[i] - y[i]; } return 0; }); int p = 1; for(int i = 1;i < n;i++){ if(!Arrays.equals(a[i], a[i-1])){ a[p++] = a[i]; } } a = Arrays.copyOf(a, p); n = p; if(n == 1){ out.println("Yes"); out.println(a[0]); return; } int[] df = new int[n]; int[] args = new int[m]; int[] args2 = new int[m]; for(int i = 0;i < m;i++){ int[] f = new int[n]; for(int j = 0;j < n;j++){ f[a[j][i]]++; } int fmax = -1; int fmax2 = -1; int arg = -1; int arg2 = -1; for(int j = 0;j < n;j++){ if(f[j] > fmax){ fmax2 = fmax; arg2 = arg; fmax = f[j]; arg = j; }else if(f[j] > fmax2){ fmax2 = f[j]; arg2 = j; } } args[i] = arg; args2[i] = fmax == fmax2 ? arg2 : -1; for(int j = 0;j < n;j++){ if(a[j][i] != arg){ df[j]++; } } } boolean ok = true; int maxdf = -1; int argdf = -1; for(int j = 0;j < n;j++){ if(df[j] > 2){ ok = false; } if(df[j] > 6){ out.println("No"); return; } if(df[j] > maxdf){ maxdf = df[j]; argdf = j; } } if(ok){ out.println("Yes"); for(int j = 0;j < m;j++){ out.print(imaps[j][args[j]]).print(" "); } out.println(); return; } List<Integer> change = new ArrayList<>(); for(int j = 0;j < m;j++){ if(a[argdf][j] != args[j]){ change.add(j); } } int[] can = Arrays.copyOf(args, m); for(int i = 0;i < 1<<change.size();i++){ for(int j = 0;j < change.size();j++){ can[change.get(j)] = i<<~j<0 ? args[change.get(j)] : a[argdf][change.get(j)]; } if(check(can, a)){ out.println("Yes"); for(int j = 0;j < m;j++){ out.print(imaps[j][can[j]]).print(" "); } out.println(); return; } } out.println("No"); } boolean check(int[] can, int[][] a) { for(int[] row : a){ int df = 0; for(int i = 0;i < can.length;i++){ if(row[i] != can[i])df++; } if(df > 2)return false; } return true; } Random gen = new Random(); public int[] shrinkX(int[] a) { int n = a.length; long[] b = new long[n]; for(int i = 0;i < n;i++)b[i] = (long)a[i]<<32|i; b = shuffle(b, gen); Arrays.sort(b); int[] ret = new int[n]; int p = 0; for(int i = 0;i < n;i++) { if(i == 0 || (b[i]^b[i-1])>>32!=0)ret[p++] = (int)(b[i]>>32); a[(int)b[i]] = p-1; } return Arrays.copyOf(ret, p); } public static long[] shuffle(long[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++){ int ind = gen.nextInt(n-i)+i; long d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
820c240b793e6a4536368973823dffd2
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
//package round704; import java.io.*; import java.util.*; public class E { InputStream is; FastWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(); int[][] a = new int[n][]; for(int i = 0;i < n;i++){ a[i] = na(m); } if(m <= 2){ out.println("Yes"); out.println(a[0]); return; } int[][] imaps = new int[m][]; for(int i = 0;i < m;i++){ int[] t = new int[n]; for(int j = 0;j < n;j++){ t[j] = a[j][i]; } imaps[i] = shrinkX(t); for(int j = 0;j < n;j++){ a[j][i] = t[j]; } } Arrays.sort(a, (x, y) -> { for(int i = 0;i < x.length;i++){ if(x[i] != y[i])return x[i] - y[i]; } return 0; }); int p = 1; for(int i = 1;i < n;i++){ if(!Arrays.equals(a[i], a[i-1])){ a[p++] = a[i]; } } a = Arrays.copyOf(a, p); n = p; if(n == 1){ out.println("Yes"); out.println(a[0]); return; } int[] df = new int[n]; int[] args = new int[m]; int[] args2 = new int[m]; for(int i = 0;i < m;i++){ int[] f = new int[n]; for(int j = 0;j < n;j++){ f[a[j][i]]++; } int fmax = -1; int fmax2 = -1; int arg = -1; int arg2 = -1; for(int j = 0;j < n;j++){ if(f[j] > fmax){ fmax2 = fmax; arg2 = arg; fmax = f[j]; arg = j; }else if(f[j] > fmax2){ fmax2 = f[j]; arg2 = j; } } args[i] = arg; args2[i] = fmax == fmax2 ? arg2 : -1; for(int j = 0;j < n;j++){ if(a[j][i] != arg){ df[j]++; } } } boolean ok = true; int maxdf = -1; int argdf = -1; for(int j = 0;j < n;j++){ if(df[j] > 2){ ok = false; } if(df[j] > 6){ out.println("No"); return; } if(df[j] > maxdf){ maxdf = df[j]; argdf = j; } } if(ok){ out.println("Yes"); for(int j = 0;j < m;j++){ out.print(imaps[j][args[j]]).print(" "); } out.println(); return; } List<Integer> change = new ArrayList<>(); for(int j = 0;j < m;j++){ if(a[argdf][j] != args[j]){ change.add(j); } } int[] can = Arrays.copyOf(args, m); for(int i = 0;i < 1<<change.size();i++){ for(int j = 0;j < change.size();j++){ can[change.get(j)] = i<<~j<0 ? args[change.get(j)] : a[argdf][change.get(j)]; } if(check(can, a)){ out.println("Yes"); for(int j = 0;j < m;j++){ out.print(imaps[j][can[j]]).print(" "); } out.println(); return; } } out.println("No"); } boolean check(int[] can, int[][] a) { for(int[] row : a){ int df = 0; for(int i = 0;i < can.length;i++){ if(row[i] != can[i])df++; } if(df > 2)return false; } return true; } Random gen = new Random(); public int[] shrinkX(int[] a) { int n = a.length; long[] b = new long[n]; for(int i = 0;i < n;i++)b[i] = (long)a[i]<<32|i; b = shuffle(b, gen); Arrays.sort(b); int[] ret = new int[n]; int p = 0; for(int i = 0;i < n;i++) { if(i == 0 || (b[i]^b[i-1])>>32!=0)ret[p++] = (int)(b[i]>>32); a[(int)b[i]] = p-1; } return Arrays.copyOf(ret, p); } public static long[] shuffle(long[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++){ int ind = gen.nextInt(n-i)+i; long d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 11
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
6883a477a17aad6cf6b9772d9b533ca7
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javafx.util.Pair; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); EAlmostFaultTolerantDatabase solver = new EAlmostFaultTolerantDatabase(); solver.solve(1, in, out); out.close(); } static class EAlmostFaultTolerantDatabase { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = in.nextInt(); } } Pair<Integer, Integer> pair = check(grid); if (pair.getValue() > 4) { out.println("No"); return; } if (pair.getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } List<Integer> list = new ArrayList<>(); for (int i = 0; i < m; i++) { if (grid[0][i] != grid[pair.getKey()][i]) { list.add(i); } } for (int i = 1; i < (1 << list.size()); i++) { List<Integer> one = new ArrayList<>(); for (int j = 0; j < 4; j++) { if ((i & (1 << j)) > 0) { one.add(list.get(j)); } } if (one.size() > 2 || list.size() - one.size() > 2) continue; int[] tmp = new int[one.size()]; for (int j = 0; j < one.size(); j++) { tmp[j] = grid[0][one.get(j)]; grid[0][one.get(j)] = grid[pair.getKey()][one.get(j)]; } if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } for (int j = 0; j < one.size(); j++) { grid[0][one.get(j)] = tmp[j]; } } if (list.size() == 3) { Map<Integer, Integer>[] diff = new HashMap[m]; Arrays.setAll(diff, i -> new HashMap<>()); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Integer num = diff[i].getOrDefault(grid[j][i], 0); diff[i].put(grid[j][i], ++num); } } List<Node>[] nodes = new ArrayList[m]; Arrays.setAll(nodes, i -> new ArrayList<>()); for (int i = 0; i < m; i++) { int finalI = i; diff[i].forEach((k, v) -> { nodes[finalI].add(new Node(k, v)); }); nodes[i].sort((a, b) -> b.num - a.num); } for (int i = 1; i < (1 << list.size()); i++) { List<Integer> one = new ArrayList<>(); for (int j = 0; j < 4; j++) { if ((i & (1 << j)) > 0) { one.add(list.get(j)); } } if (one.size() != 2) continue; int[] tmp = new int[one.size()]; for (int j = 0; j < 3 && j < nodes[one.get(0)].size(); j++) { int df = nodes[one.get(0)].get(j).index; //} //for (Integer df : diff[one.get(0)]) { tmp[0] = grid[0][one.get(0)]; grid[0][one.get(0)] = df; tmp[1] = grid[0][one.get(1)]; grid[0][one.get(1)] = grid[pair.getKey()][one.get(1)]; if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } grid[0][one.get(0)] = tmp[0]; grid[0][one.get(1)] = tmp[1]; } for (int j = 0; j < 3 && j < nodes[one.get(1)].size(); j++) { int df = nodes[one.get(1)].get(j).index; //for (Integer df : diff[one.get(1)]) { tmp[1] = grid[0][one.get(1)]; grid[0][one.get(1)] = df; tmp[0] = grid[0][one.get(0)]; grid[0][one.get(0)] = grid[pair.getKey()][one.get(0)]; if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } grid[0][one.get(0)] = tmp[0]; grid[0][one.get(1)] = tmp[1]; } } } out.println("No"); } private void printArr(int[] ints, PrintWriter out) { for (int i = 0; i < ints.length; i++) { out.print(ints[i] + " "); } out.println(); } private Pair<Integer, Integer> check(int[][] grid) { int len = grid.length; int n = grid[0].length; int max = 0; int p = 0; for (int i = 1; i < len; i++) { int num = 0; for (int j = 0; j < n; j++) { if (grid[i][j] != grid[0][j]) { num++; } } if (num > max) { max = num; p = i; } } return new Pair<>(p, max); } class Node { int index; int num; public Node(int index, int num) { this.index = index; this.num = num; } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 8
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
314bb56574a81155e40e72a28996c031
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javafx.util.Pair; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); EAlmostFaultTolerantDatabase solver = new EAlmostFaultTolerantDatabase(); solver.solve(1, in, out); out.close(); } static class EAlmostFaultTolerantDatabase { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = in.nextInt(); } } Pair<Integer, Integer> pair = check(grid); if (pair.getValue() > 4) { out.println("No"); return; } if (pair.getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } List<Integer> list = new ArrayList<>(); for (int i = 0; i < m; i++) { if (grid[0][i] != grid[pair.getKey()][i]) { list.add(i); } } for (int i = 1; i < (1 << list.size()); i++) { List<Integer> one = new ArrayList<>(); for (int j = 0; j < 4; j++) { if ((i & (1 << j)) > 0) { one.add(list.get(j)); } } if (one.size() > 2 || list.size() - one.size() > 2) continue; int[] tmp = new int[one.size()]; for (int j = 0; j < one.size(); j++) { tmp[j] = grid[0][one.get(j)]; grid[0][one.get(j)] = grid[pair.getKey()][one.get(j)]; } if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } for (int j = 0; j < one.size(); j++) { grid[0][one.get(j)] = tmp[j]; } } if (list.size() == 3) { Map<Integer, Integer>[] diff = new HashMap[m]; Arrays.setAll(diff, i -> new HashMap<>()); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Integer num = diff[i].getOrDefault(grid[j][i], 0); diff[i].put(grid[j][i], ++num); } } List<Node>[] nodes = new ArrayList[m]; Arrays.setAll(nodes, i -> new ArrayList<>()); for (int i = 0; i < m; i++) { int finalI = i; diff[i].forEach((k, v) -> { nodes[finalI].add(new Node(k, v)); }); nodes[i].sort((a, b) -> b.num - a.num); } for (int i = 1; i < (1 << list.size()); i++) { List<Integer> one = new ArrayList<>(); for (int j = 0; j < 4; j++) { if ((i & (1 << j)) > 0) { one.add(list.get(j)); } } if (one.size() != 2) continue; int[] tmp = new int[one.size()]; for (int j = 0; j < 10 && j < nodes[one.get(0)].size(); j++) { int df = nodes[one.get(0)].get(j).index; //} //for (Integer df : diff[one.get(0)]) { tmp[0] = grid[0][one.get(0)]; grid[0][one.get(0)] = df; tmp[1] = grid[0][one.get(1)]; grid[0][one.get(1)] = grid[pair.getKey()][one.get(1)]; if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } grid[0][one.get(0)] = tmp[0]; grid[0][one.get(1)] = tmp[1]; } for (int j = 0; j < 10 && j < nodes[one.get(1)].size(); j++) { int df = nodes[one.get(1)].get(j).index; //for (Integer df : diff[one.get(1)]) { tmp[1] = grid[0][one.get(1)]; grid[0][one.get(1)] = df; tmp[0] = grid[0][one.get(0)]; grid[0][one.get(0)] = grid[pair.getKey()][one.get(0)]; if (check(grid).getValue() <= 2) { out.println("Yes"); printArr(grid[0], out); return; } grid[0][one.get(0)] = tmp[0]; grid[0][one.get(1)] = tmp[1]; } } } out.println("No"); } private void printArr(int[] ints, PrintWriter out) { for (int i = 0; i < ints.length; i++) { out.print(ints[i] + " "); } out.println(); } private Pair<Integer, Integer> check(int[][] grid) { int len = grid.length; int n = grid[0].length; int max = 0; int p = 0; for (int i = 1; i < len; i++) { int num = 0; for (int j = 0; j < n; j++) { if (grid[i][j] != grid[0][j]) { num++; } } if (num > max) { max = num; p = i; } } return new Pair<>(p, max); } class Node { int index; int num; public Node(int index, int num) { this.index = index; this.num = num; } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 8
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
a8958c0eac2636abac2454cfc59b6f60
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.awt.Point; public class CFTemplate { static final long MOD = 1000000007L; //static final long MOD2 = 1000000009L; //static final long MOD = 998244353L; //static final long INF = 500000000000L; static final int INF = 1100000000; static final int NINF = -100000; static FastScanner sc; static PrintWriter pw; static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; static int[][] nums; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int N = sc.ni(); int M = sc.ni(); nums = new int[N][M]; for (int i = 0; i < N; i++) nums[i] = sc.intArray(M, 0); for (int i = 1; i < N; i++) { ArrayList<Integer> inds = diffs(0,i); if (inds.size()==5) { pw.println("No"); pw.close(); return; } else if (inds.size()==4) { for (int x: new int[]{3,5,6,9,10,12}) { int[] ans = nums[0].clone(); for (int b = 0; b < 4; b++) { if ((x&(1<<b)) > 0) ans[inds.get(b)] = nums[i][inds.get(b)]; } boolean good = true; for (int j = 0; j < N; j++) { if (check(j,ans) > 2) { good = false; break; } } if (good) { pw.println("Yes"); for (int A: ans) pw.print(A + " "); pw.close(); return; } } pw.println("No"); pw.close(); return; } else if (inds.size()==3) { for (int unk = 0; unk < 3; unk++) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { int[] ans = nums[0].clone(); ans[inds.get(unk)] = -1; int j1 = inds.get((unk+1)%3); int j2 = inds.get((unk+2)%3); ans[j1] = a==0 ? nums[0][j1] : nums[i][j1]; ans[j2] = b==0 ? nums[0][j2] : nums[i][j2]; int[] print = works(ans,inds.get(unk)); if (print != null) { pw.println("Yes"); for (int A: print) pw.print(A + " "); pw.close(); return; } } } } pw.println("No"); pw.close(); return; } } pw.println("Yes"); for (int A: nums[0]) pw.print(A + " "); pw.close(); } public static int[] works(int[] ans, int u) { for (int r = 0; r < nums.length; r++) { int d = check(r,ans); boolean canfix = (ans[u]==-1); if (d > 3 || (d==3 && !canfix)) { return null; } else if (d==3 && canfix) { ans[u] = nums[r][u]; } } if (ans[u]==-1) ans[u] = nums[0][u]; return ans; } public static ArrayList<Integer> diffs(int r1, int r2) { ArrayList<Integer> inds = new ArrayList<>(); for (int j = 0; j < nums[0].length; j++) { if (nums[r1][j]!=nums[r2][j]) { inds.add(j); if (inds.size()==5) break; } } return inds; } public static int check(int r, int[] guess) { int d = 0; for (int j = 0; j < nums[r].length; j++) { if (guess[j] != nums[r][j]) d++; } return d; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni()+mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl()+mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 8
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
a0c915145551e0a1e2bde02057df60d7
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int n=sc.nextInt(); int m=sc.nextInt(); int[][]a=new int[n][m]; int[]diff=new int[n]; boolean[][]diffpos=new boolean[n][m]; int ind=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); if(i!=0) { diff[i]+=(a[i][j]==a[0][j]?0:1); diffpos[i][j]=(a[i][j]!=a[0][j]); } } } int maxdiff=0; for(int i=0;i<n;i++) { if(diff[i]>maxdiff) { ind=i; maxdiff=Math.max(maxdiff, diff[i]); } } if(maxdiff<=2) { pw.println("Yes"); for(int i=0;i<m;i++) { pw.print(a[0][i]+" "); } pw.println(); }else if(maxdiff==4) { boolean found=false; out: for(int i=0;i<(1<<4);i++) { int[]temp=new int[m]; int cur=0; for(int j=0;j<m;j++) { if(diffpos[ind][j]) { if((i&(1<<cur))!=0) { temp[j]=a[0][j]; }else { temp[j]=a[ind][j]; } cur++; }else { temp[j]=a[0][j]; } } int md=0; for(int j=0;j<n;j++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[j][k]==temp[k]?0:1); } md=Math.max(md, tedif); } if(md<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp[k]+" "); } pw.println(); found=true; break out; } } if(!found)pw.println("No"); }else if(maxdiff==3) { // pw.println("*"); boolean found=false; out: for(int i=0;i<3;i++) { int[]temp=new int[m]; int cur=0; for(int j=0;j<m;j++) { if(diffpos[ind][j]) { if(i!=cur) { temp[j]=a[0][j]; }else { temp[j]=a[ind][j]; } cur++; }else { temp[j]=a[0][j]; } } // pw.println(Arrays.toString(temp)); int md=0; int jnd=0; for(int j=0;j<n;j++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[j][k]==temp[k]?0:1); } if(tedif>md) { jnd=j; md=Math.max(md, tedif); } } // pw.println(md); if(md<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp[k]+" "); } pw.println(); found=true; break out; }else if(md==3) { for(int j=0;j<3;j++) { int[]temp2=new int[m]; int cur2=0; for(int k=0;k<m;k++) { if(a[jnd][k]!=temp[k]) { if(cur2==j) { temp2[k]=a[jnd][k]; }else { temp2[k]=temp[k]; } cur2++; }else { temp2[k]=temp[k]; } } // pw.println(Arrays.toString(temp2)); int md2=0; for(int jj=0;jj<n;jj++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[jj][k]==temp2[k]?0:1); } md2=Math.max(md2, tedif); } // pw.println("* "+md2); if(md2<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp2[k]+" "); } pw.println(); found=true; break out; } } } } if(!found)pw.println("No"); }else { pw.println("No"); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 8
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
2462aa613d699068e47d6a38f9240a73
train_109.jsonl
1614071100
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well.
512 megabytes
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int n=sc.nextInt(); int m=sc.nextInt(); int[][]a=new int[n][m]; int[]diff=new int[n]; boolean[][]diffpos=new boolean[n][m]; int ind=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); if(i!=0) { diff[i]+=(a[i][j]==a[0][j]?0:1); diffpos[i][j]=(a[i][j]!=a[0][j]); } } } int maxdiff=0; for(int i=0;i<n;i++) { if(diff[i]>maxdiff) { ind=i; maxdiff=Math.max(maxdiff, diff[i]); } } if(maxdiff<=2) { pw.println("Yes"); for(int i=0;i<m;i++) { pw.print(a[0][i]+" "); } pw.println(); }else if(maxdiff==4) { boolean found=false; out: for(int i=0;i<(1<<4);i++) { int[]temp=new int[m]; int cur=0; for(int j=0;j<m;j++) { if(diffpos[ind][j]) { if((i&(1<<cur))!=0) { temp[j]=a[0][j]; }else { temp[j]=a[ind][j]; } cur++; }else { temp[j]=a[0][j]; } } int md=0; for(int j=0;j<n;j++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[j][k]==temp[k]?0:1); } md=Math.max(md, tedif); } if(md<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp[k]+" "); } pw.println(); found=true; break out; } } if(!found)pw.println("No"); }else if(maxdiff==3) { // pw.println("*"); boolean found=false; out: for(int i=0;i<3;i++) { int[]temp=new int[m]; int cur=0; for(int j=0;j<m;j++) { if(diffpos[ind][j]) { if(i!=cur) { temp[j]=a[0][j]; }else { temp[j]=a[ind][j]; } cur++; }else { temp[j]=a[0][j]; } } // pw.println(Arrays.toString(temp)); int md=0; int jnd=0; for(int j=0;j<n;j++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[j][k]==temp[k]?0:1); } if(tedif>md) { jnd=j; md=Math.max(md, tedif); } } // pw.println(md); if(md<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp[k]+" "); } pw.println(); found=true; break out; }else if(md==3) { for(int j=0;j<3;j++) { int[]temp2=new int[m]; int cur2=0; for(int k=0;k<m;k++) { if(a[jnd][k]!=temp[k]) { if(cur2==j) { temp2[k]=a[jnd][k]; }else { temp2[k]=temp[k]; } cur2++; }else { temp2[k]=temp[k]; } } // pw.println(Arrays.toString(temp2)); int md2=0; for(int jj=0;jj<n;jj++) { int tedif=0; for(int k=0;k<m;k++) { tedif+=(a[jj][k]==temp2[k]?0:1); } md2=Math.max(md2, tedif); } // pw.println("* "+md2); if(md2<=2) { pw.println("Yes"); for(int k=0;k<m;k++) { pw.print(temp2[k]+" "); } pw.println(); found=true; break out; } } } } if(!found)pw.println("No"); }else { pw.println("No"); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["3 4\n1 10 10 100\n1 1 1 100\n10 100 1 100", "10 7\n1 1 1 1 1 1 1\n1 1 1 1 1 1 2\n1 1 1 1 1 2 2\n1 1 1 1 2 2 1\n1 1 1 2 2 1 1\n1 1 2 2 1 1 1\n1 2 2 1 1 1 1\n2 2 1 1 1 1 1\n2 1 1 1 1 1 1\n1 1 1 1 1 1 1", "2 5\n2 2 1 1 1\n1 1 2 2 2"]
2 seconds
["Yes\n1 10 1 100", "Yes\n1 1 1 1 1 1 1", "No"]
NoteIn the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions.In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions.In the third example, there is no array differing in at most two positions from every database's copy.
Java 8
standard input
[ "brute force", "constructive algorithms", "dfs and similar", "greedy", "implementation" ]
5f670d159734ce90fad4e88c1b017db3
The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n$$$; $$$1 \le m$$$; $$$n \cdot m \le 250\,000$$$) — the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, \dots, s_{i, m}$$$ ($$$1 \le s_{i, j} \le 10^9$$$).
2,500
If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them.
standard output
PASSED
d0d8715c9ce9ba43dce9e09f665fa4c7
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static long ans = 0; public static void merge(int[] arr, int i, int j, int x, int y) { int[] arr2 = new int[(j-i+1) + (y-x+1)]; int p = i; int q = x; int k = 0; while(i <= j && x<=y) { if(2L*arr[x] < arr[i]) { ans+=(j-i+1); x++; } else { i++; } } i = p; x = q; while(i <= j && x<=y) { if(arr[x] < arr[i]) { arr2[k] = arr[x]; x++; } else { arr2[k] = arr[i]; i++; } k++; } while(i<=j){ arr2[k] = arr[i]; i++; k++; } while(x<=y){ arr2[k] = arr[x]; x++; k++; } for(int d=p;d<=y;d++)arr[d] = arr2[d-p]; } public static void sort(int[] arr, int i, int j) { if(i==j)return; int middle = (i + j)/2; sort(arr, i, middle); sort(arr, middle+1, j); merge(arr, i, middle, middle+1, j); return; } public static int[][] merge(int[][] intervals) { int n = intervals.length; ArrayList<pair> arr = new ArrayList<pair>(); for(int i=0;i<n;i++)arr.add(new pair(intervals[i][0], intervals[i][1])); arr.sort(new comp()); int min = -1; int max = -1; ArrayList<pair> ar = new ArrayList<>(); int cur = -1; for(int i=0;i<n;i++) { if(max < arr.get(i).x) { if(cur != -1) { ar.get(cur).y = max; } min = arr.get(i).x; max = arr.get(i).y; pair p = new pair(min, max); ar.add(p); cur++; } else { max = arr.get(i).y; } } ar.get(cur).y = max; int[][] ans = new int[ar.size()][2]; for(int i=0;i<ar.size();i++) { ans[i][0] = ar.get(i).x; ans[i][1] = ar.get(i).y; } return ans; } static class Reader{ private final BufferedReader reader; private StringTokenizer tokenizer; Reader(InputStream input) { this.reader = new BufferedReader(new InputStreamReader(input)); this.tokenizer = new StringTokenizer(""); } public String next() throws IOException { while(!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for(int i=0;i<n;i++)arr[i]=nextInt(); return arr; } public long[] nextLongArr(int n)throws IOException { long[] arr = new long[n]; for(int i=0;i<n;i++)arr[i]=nextLong(); return arr; } public char[] nextCharArr(int n) throws IOException { char[] arr = new char[n]; for(int i=0;i<n;i++)arr[i] = next().charAt(0); return arr; } } public static void main(String[] args) throws IOException { Reader scan = new Reader(System.in); StringBuffer sb = new StringBuffer(); BufferedOutputStream b = new BufferedOutputStream(System.out); int t = 1; t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int[] arr = scan.nextIntArr(n); sb.append(solve(n, arr)).append("\n"); } b.write((sb.toString()).getBytes()); b.flush(); } // 0 0 0 1 1 0 // 0 1 2 3 4 5 private static String solve(int n, int[] arr) { if(n==1)return (arr[0]==0) ? "Yes" : "No"; long[] front = new long[n]; long[] back = new long[n]; int i = n-1; while( i>=0 && arr[i]==0) { i--; } while(i >= 0) { if(i==n-1) { front[i] = 0; if(arr[i] > 0)return "No"; back[i] = Math.abs(arr[i]); } else if(i==0) { front[i] = back[i+1]; back[i] = 0; } else { front[i] = back[i+1]; if(front[i] - arr[i] <= 0)return "No"; back[i] = front[i] - arr[i]; } i--; } if(front[0] == arr[0])return "Yes"; return "No"; } } class pair { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } } class comp implements Comparator<pair> { @Override public int compare(pair a, pair b) { return a.x-b.x; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
d9fe69522f7edc8902c4998f0839f229
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static long ans = 0; public static void merge(int[] arr, int i, int j, int x, int y) { int[] arr2 = new int[(j-i+1) + (y-x+1)]; int p = i; int q = x; int k = 0; while(i <= j && x<=y) { if(2L*arr[x] < arr[i]) { ans+=(j-i+1); x++; } else { i++; } } i = p; x = q; while(i <= j && x<=y) { if(arr[x] < arr[i]) { arr2[k] = arr[x]; x++; } else { arr2[k] = arr[i]; i++; } k++; } while(i<=j){ arr2[k] = arr[i]; i++; k++; } while(x<=y){ arr2[k] = arr[x]; x++; k++; } for(int d=p;d<=y;d++)arr[d] = arr2[d-p]; } public static void sort(int[] arr, int i, int j) { if(i==j)return; int middle = (i + j)/2; sort(arr, i, middle); sort(arr, middle+1, j); merge(arr, i, middle, middle+1, j); return; } public static int[][] merge(int[][] intervals) { int n = intervals.length; ArrayList<pair> arr = new ArrayList<pair>(); for(int i=0;i<n;i++)arr.add(new pair(intervals[i][0], intervals[i][1])); arr.sort(new comp()); int min = -1; int max = -1; ArrayList<pair> ar = new ArrayList<>(); int cur = -1; for(int i=0;i<n;i++) { if(max < arr.get(i).x) { if(cur != -1) { ar.get(cur).y = max; } min = arr.get(i).x; max = arr.get(i).y; pair p = new pair(min, max); ar.add(p); cur++; } else { max = arr.get(i).y; } } ar.get(cur).y = max; int[][] ans = new int[ar.size()][2]; for(int i=0;i<ar.size();i++) { ans[i][0] = ar.get(i).x; ans[i][1] = ar.get(i).y; } return ans; } static class Reader{ private final BufferedReader reader; private StringTokenizer tokenizer; Reader(InputStream input) { this.reader = new BufferedReader(new InputStreamReader(input)); this.tokenizer = new StringTokenizer(""); } public String next() throws IOException { while(!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for(int i=0;i<n;i++)arr[i]=nextInt(); return arr; } public long[] nextLongArr(int n)throws IOException { long[] arr = new long[n]; for(int i=0;i<n;i++)arr[i]=nextLong(); return arr; } public char[] nextCharArr(int n) throws IOException { char[] arr = new char[n]; for(int i=0;i<n;i++)arr[i] = next().charAt(0); return arr; } } public static void main(String[] args) throws IOException { Reader scan = new Reader(System.in); StringBuffer sb = new StringBuffer(); BufferedOutputStream b = new BufferedOutputStream(System.out); int t = 1; t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int[] arr = scan.nextIntArr(n); sb.append(solve(n, arr)).append("\n"); } b.write((sb.toString()).getBytes()); b.flush(); } // 0 0 0 1 1 0 // 0 1 2 3 4 5 private static String solve(int n, int[] arr) { long[] front = new long[n]; front[0] = arr[0]; for(int i=1;i<n;i++) { front[i] = arr[i] + front[i-1]; } if(front[n-1] != 0)return "No"; boolean pos = true; for(int i=0;i<n;i++)if(front[i] < 0)pos = false; boolean vis = false; for(int i=0;i<n;i++) { if(front[i]==0 )vis = true; else if(vis)pos = false; } return (pos) ? "Yes" : "No"; } } class pair { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } } class comp implements Comparator<pair> { @Override public int compare(pair a, pair b) { return a.x-b.x; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
65b9f9f40294b04446657e0541bd5ed3
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Queue; import java.util.StringTokenizer; public class cf { static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static int n,m,k; static long arr[]; static int brr[]; public static void main(String[] args) { FastScanner s= new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); while(t-->0) { n=s.nextInt(); arr=new long[n]; for(int i=0;i<n;i++)arr[i]=s.nextLong(); long co=-arr[0]; boolean z=true; if(arr[0]!=0)z=false; String ans="YES"; for(int i=1;i<n;i++) { long a=arr[i]; if(z) { if(a!=0) { ans="NO"; break; } } if(a==co) { co=0; continue; } // System.out.println(i); // if(i==n) if(a<co || co==0) { ans="NO"; break; } co=-Math.abs(a-co); } if(co!=0)ans="NO"; if(arr[0]<0)ans="NO"; System.out.println(ans); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
bced741007bc52c293b75f0760b84114
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.Scanner; public class Solution { public static String solve(int[] arr) { int n = arr.length; int b1 = arr[0]; boolean foundZero = false; for(int i = 1; i < n; i++) { if(foundZero && arr[i] != 0) { return "NO"; } b1 += arr[i]; if(b1 < 0) { return "NO"; } if(b1 == 0) { foundZero = true; } } return "YES"; } public static void main(String[] str) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] a = new int[n]; long preSum = 0; boolean ok = true; boolean flag = false; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); if(flag&&a[i]!=0) ok = false; preSum += a[i]; // System.out.println(preSum); if(preSum<0) ok = false; if(preSum==0){ flag = true; } } if(preSum!=0) System.out.println("no"); else { if(ok) System.out.println("yes"); else System.out.println("no"); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0b9c2982e10d9e3fbf537a208f6942b0
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
//created by Toufique on 24/11/2022 import java.io.*; import java.util.*; public class Div2_800A { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextLong(); if (solve(n, a)) pw.println("YES"); else pw.println("NO"); } pw.close(); } static boolean solve(int n, long[] a) { boolean allzero = true; for (long x : a) if (x != 0) allzero = false; if (allzero) return true; long sum = 0; boolean seenZero = false; for (int i = 0; i < n; i++) { long aa = a[i]; sum += aa; if (sum < 0) return false; else if (sum == 0) seenZero = true; if (seenZero && sum != 0) return false; } if (sum != 0) return false; return true; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
dfacd520d39b0d8cc7894c804720c0fd
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
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){ // 每个位置,前面的和都大于等于0, 如果前面的和大于0,那么当前指针,必然在最左边,如果后面还有内容,就出现了错误 int n = sc.nextInt(); int[] a = new int[n]; long preSum = 0; boolean ok = true; boolean flag = false; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); if(flag&&a[i]!=0) ok = false; preSum += a[i]; // System.out.println(preSum); if(preSum<0) ok = false; if(preSum==0){ flag = true; } } if(preSum!=0) System.out.println("no"); else { if(ok) System.out.println("yes"); else System.out.println("no"); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
950973589c6c8ca5e58ca276ec181e8d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.Scanner; public class DirectionalIncrease { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); for(int t=0; t<testCases; t++){ boolean yes = false; int size = sc.nextInt(); int arr[] = new int[size]; for (int i=0; i<size; i++){ arr[i] = sc.nextInt(); } long sum=0L; int i=0; for(;i<size; i++){ sum+=arr[i]; if(sum<0){ break; } if(sum == 0){ if(!checkAllRightsZero(arr, i+1, size)){ break; }else { i = size; break; } } } if(sum==0 && i==size){ yes = true; } if(yes){ System.out.println("Yes"); }else { System.out.println("No"); } } sc.close(); } private static boolean checkAllRightsZero(int[] arr, int start, int end) { for(int i=start;i<end; i++){ if(arr[i]!=0){ return false; } } return true; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
a1e28ea42509098ee8a3fe6d460c9cf9
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int test = in.nextInt(); for(int t = 1;t<=test;t++){ int n = in.nextInt(); int a[] = new int[n]; boolean flag = true; for(int i = 0;i<n;i++){ a[i] = in.nextInt(); } long sum = 0,ind = n-1; for(int i = 0;i<n;i++){ sum+=a[i]; if(sum<0){ flag = false; break; } else if(sum == 0){ ind = i; break; } } if(sum!=0){ flag = false; } else { for (int i = (int) (ind+1); i<n; i++){ if(a[i]!=0){ flag = false; break; } } } if(flag){ pw.println("Yes"); } else { pw.println("No"); } } pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
127e5466b2e84deba600fef0f9abdef0
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class practice { public static void solve() { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] a = new int[n + 1]; long sum = 0; for(int i = 1;i <= n;i++) { a[i] = sc.nextInt(); sum += a[i]; } if(sum == 0) { List<Long> b = new ArrayList<>(); b.add((long)a[1]); for(int i = 2;i <= n;i++) b.add(a[i] + b.get(b.size() - 1)); boolean ok = true; int i; for(i = 0;i < n;i++) { if(b.get(i) < 0) { ok = false; break; } else if(b.get(i) > 0) continue; else break; } if(!ok) out.println("NO"); else { for(int j = i;j < n;j++) { if(b.get(j) != 0) { ok = false; break; } } out.println(ok ? "YES" : "NO"); } } else out.println("NO"); } out.flush(); } public static void main(String[] args) throws IOException { solve(); } public static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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 { if(st.hasMoreTokens()) str = st.nextToken("\n"); else str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
154fe1da461894cbd492ec15f66b978a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); long T = sc.nextLong(); while(T-->0){ long len = sc.nextLong(); long[] table = new long[(int)len+1]; long[] sum = new long[(int) len + 1]; for(int i = 1;i<=len;i++){ table[i] = sc.nextLong(); sum[i] = sum[i-1] + table[i]; } if(sum[(int)len]!=0){ System.out.println("No"); continue; } boolean flag = true; for(int i = 1;i<=len;i++){ if(sum[i]<0){ flag = false; break; } } boolean visZero = false; for(int i = 1;i<=len;i++){ if(sum[i]==0){ visZero=true; }else if(visZero){ flag = false; break; } } if(flag){ System.out.println("Yes"); }else{ System.out.println("No"); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b237f6b33a82f04a42553ec7169b9669
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; public class a1693 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,j,i,n,c; long sum; t=sc.nextInt(); for(j=1;j<=t;j++){ n=sc.nextInt(); List<Long> a=new ArrayList<>(); a.add((long)0); for(i=1;i<=n;i++) a.add(sc.nextLong()); sum=0; for(i=1;i<=n;i++){ sum+=a.get(i); if(sum<=0) break; } if(sum<0||i>n) System.out.println("No"); else if(i==n) System.out.println("Yes"); else{ i++; for(;i<=n;i++){ if(a.get(i)!=0) break; } if(i<=n) System.out.println("No"); else System.out.println("Yes"); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
2da94c2e7f779a3b75652cb1d5610f86
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { takeInput(); Scanner scn = new Scanner(System.in); int t = 1; t = scn.nextInt(); while (t-- > 0) solve(scn); } public static void solve(Scanner scn) { int n = scn.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scn.nextLong(); } int r = n - 1; while (r >= 0 && arr[r] == 0) r--; if (r == -1) { System.out.println("Yes"); return; } if (arr[r] > 0) { System.out.println("No"); return; } if (arr[0] < 0 ) { System.out.println("No"); return; } long sum = 0; for (int i = 0; i <= r; i++) sum += arr[i]; if (sum != 0) { System.out.println("No"); return; } long prev = 1L * Math.abs(arr[r--]); while (r >= 0) { prev = prev - arr[r]; if (prev <= 0) break; r--; } if (prev == 0 && r == 0) { System.out.println("Yes"); } else System.out.println("No"); } public static void takeInput() { try { System.setIn(new FileInputStream("input1.txt")); System.setOut(new PrintStream(new FileOutputStream("output1.txt"))); } catch (Exception e) { System.err.println("Error"); } } private static boolean isPalindrome(String s) { int l = 0, r = s.length() - 1; while (l < r) { if (s.charAt(l) != s.charAt(r)) { return false; } l++; r--; } return true; } public static void swap(int x, int y) { x = x ^ y ^ (y = x); //x = x ^ y ^ (y = x); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
6cc7e4b2434ac8f573c3b0a545a2d61e
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; import static java.util.Comparator.*; public class Solution { public static boolean useInFile = false; public static boolean useOutFile = false; public static void main(String args[]) throws IOException { InOut inout = new InOut(); Resolver resolver = new Resolver(inout); // long time = System.currentTimeMillis(); resolver.solve(); // resolver.print("\n" + (System.currentTimeMillis() - time)); inout.flush(); } private static class Resolver { final long LONG_INF = (long) 1e18; final int INF = (int) (1e9 + 7); final int MOD = 998244353; long f[], inv[]; InOut inout; Resolver(InOut inout) { this.inout = inout; } void initF(int n, int mod) { f = new long[n + 1]; f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = (f[i - 1] * i) % mod; } } void initInv(int n, int mod) { inv = new long[n + 1]; inv[n] = pow(f[n], mod - 2, mod); for (int i = inv.length - 2; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } long cmn(int n, int m, int mod) { return f[n] * inv[m] % mod * inv[n - m] % mod; } int d[] = {0, -1, 0, 1, 0}; boolean legal(int r, int c, int n, int m) { return r >= 0 && r < n && c >= 0 && c < m; } int[] getBits(int n) { int b[] = new int[31]; for (int i = 0; i < 31; i++) { if ((n & (1 << i)) != 0) { b[i] = 1; } } return b; } private char ask1(int i) throws IOException { format("? 1 %d\n", i); flush(); return next(10).charAt(0); } private int ask2(int l, int r) throws IOException { format("? 2 %d %d\n", l, r); flush(); return nextInt(); } void solve() throws IOException { int tt = 1; boolean hvt = true; if (hvt) { tt = nextInt(); // tt = Integer.parseInt(nextLine()); } // initF(300001, MOD); // initInv(300001, MOD); // boolean pri[] = generatePrime(40000); for (int cs = 1; cs <= tt; cs++) { long rs = 0; int n = nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } boolean ok = true; int j = n - 1; while (j >= 0 && a[j] == 0) { j--; } if (j < 0) { } else if (j == 0) { ok = false; } else { long lft = 0; for (int i = 0; i < j; i++) { lft += a[i]; } for (int i = j; i > 0; i--) { if (lft + a[i] != 0 || a[i] >= 0) { ok = false; break; } lft -= a[i - 1]; a[i - 1] += a[i]; } if (ok) { ok = a[0] == 0; } } print(ok ? "Yes" : "No"); // print("" + rs); if (cs < tt) { format("\n"); // format(" "); } // flush(); } } private void updateSegTree(int n, long l, SegmentTree lft) { long lazy; lazy = 1; for (int j = 1; j <= l; j++) { lazy = (lazy + cmn((int) l, j, INF)) % INF; lft.modify(1, j, j, lazy); } lft.modify(1, (int) (l + 1), n, lazy); } String next() throws IOException { return inout.next(); } String next(int n) throws IOException { return inout.next(n); } String nextLine() throws IOException { return inout.nextLine(); } int nextInt() throws IOException { return inout.nextInt(); } long nextLong(int n) throws IOException { return inout.nextLong(n); } int[] anInt(int i, int j) throws IOException { int a[] = new int[j + 1]; for (int k = i; k <= j; k++) { a[k] = nextInt(); } return a; } long[] anLong(int i, int j, int len) throws IOException { long a[] = new long[j + 1]; for (int k = i; k <= j; k++) { a[k] = nextLong(len); } return a; } void print(String s) { inout.print(s, false); } void print(String s, boolean nextLine) { inout.print(s, nextLine); } void format(String format, Object... obj) { inout.format(format, obj); } void flush() { inout.flush(); } void swap(int a[], int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } void swap(long a[], int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } int getP(int x, int p[]) { if (p[x] == 0 || p[x] == x) { return x; } return p[x] = getP(p[x], p); } void union(int x, int y, int p[]) { if (x < y) { p[y] = x; } else { p[x] = y; } } boolean topSort() { int n = adj2.length - 1; int d[] = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 0; j < adj2[i].size(); j++) { d[adj2[i].get(j)[0]]++; } } List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (d[i] == 0) { list.add(i); } } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < adj2[list.get(i)].size(); j++) { int t = adj2[list.get(i)].get(j)[0]; d[t]--; if (d[t] == 0) { list.add(t); } } } return list.size() == n; } class SegmentTreeNode { long defaultVal = 0; int l, r; long val = defaultVal, lazy = defaultVal; SegmentTreeNode(int l, int r) { this.l = l; this.r = r; } } class SegmentTree { SegmentTreeNode tree[]; long inf = Long.MIN_VALUE; // long c[]; SegmentTree(int n) { assert n > 0; tree = new SegmentTreeNode[n * 3 + 1]; } void setAn(long cn[]) { // c = cn; } SegmentTree build(int k, int l, int r) { if (l > r) { return this; } if (null == tree[k]) { tree[k] = new SegmentTreeNode(l, r); } if (l == r) { return this; } int mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); return this; } void pushDown(int k) { if (tree[k].l == tree[k].r) { return; } long lazy = tree[k].lazy; // tree[k << 1].val = ((c[tree[k << 1].l] - c[tree[k << 1].r + 1] + MOD) % MOD * lazy) % MOD; tree[k << 1].val = lazy; tree[k << 1].lazy = lazy; // tree[k << 1 | 1].val = ((c[tree[k << 1 | 1].l] - c[tree[k << 1 | 1].r + 1] + MOD) % MOD * lazy) % MOD; tree[k << 1 | 1].val = lazy; tree[k << 1 | 1].lazy = lazy; tree[k].lazy = 0; } void modify(int k, int l, int r, long val) { if (tree[k].l >= l && tree[k].r <= r) { // tree[k].val = ((c[tree[k].l] - c[tree[k].r + 1] + MOD) % MOD * val) % MOD; tree[k].val = val; tree[k].lazy = val; return; } int mid = (tree[k].l + tree[k].r) >> 1; if (tree[k].lazy != 0) { pushDown(k); } if (mid >= l) { modify(k << 1, l, r, val); } if (mid + 1 <= r) { modify(k << 1 | 1, l, r, val); } // tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD; tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val); } long query(int k, int l, int r) { if (tree[k].l > r || tree[k].r < l) { return 0; } if (tree[k].lazy != 0) { pushDown(k); } if (tree[k].l >= l && tree[k].r <= r) { return tree[k].val; } long ans = (query(k << 1, l, r) + query(k << 1 | 1, l, r)) % MOD; if (tree[k].l < tree[k].r) { // tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD; tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val); } return ans; } } class BitMap { boolean[] vis = new boolean[32]; List<Integer> g[]; void init() { for (int i = 0; i < 32; i++) { g = new List[32]; g[i] = new ArrayList<>(); } } void dfs(int p) { if (vis[p]) return; vis[p] = true; for (int it : g[p]) dfs(it); } boolean connected(int a[], int n) { int m = 0; for (int i = 0; i < n; i++) if (a[i] == 0) return false; for (int i = 0; i < n; i++) m |= a[i]; for (int i = 0; i < 31; i++) g[i].clear(); for (int i = 0; i < n; i++) { int last = -1; for (int j = 0; j < 31; j++) if ((a[i] & (1 << j)) > 0) { if (last != -1) { g[last].add(j); g[j].add(last); } last = j; } } Arrays.fill(vis, false); for (int j = 0; j < 31; j++) if (((1 << j) & m) > 0) { dfs(j); break; } for (int j = 0; j < 31; j++) if (((1 << j) & m) > 0 && !vis[j]) return false; return true; } } class BinaryIndexedTree { int n = 1; long C[]; BinaryIndexedTree(int sz) { while (n <= sz) { n <<= 1; } n = sz + 1; C = new long[n]; } int lowbit(int x) { return x & -x; } void add(int x, long val) { while (x < n) { C[x] += val; x += lowbit(x); } } long getSum(int x) { long res = 0; while (x > 0) { res += C[x]; x -= lowbit(x); } return res; } int binSearch(long sum) { if (sum == 0) { return 0; } int n = C.length; int mx = 1; while (mx < n) { mx <<= 1; } int res = 0; for (int i = mx / 2; i >= 1; i >>= 1) { if (C[res + i] < sum) { sum -= C[res + i]; res += i; } } return res + 1; } } static class TrieNode { int cnt = 0; TrieNode next[]; TrieNode() { next = new TrieNode[2]; } private void insert(TrieNode trie, int ch[], int i) { while (i < ch.length) { int idx = ch[i]; if (null == trie.next[idx]) { trie.next[idx] = new TrieNode(); } trie.cnt++; trie = trie.next[idx]; i++; } } private static int query(TrieNode trie) { if (null == trie) { return 0; } int ans[] = new int[2]; for (int i = 0; i < trie.next.length; i++) { if (null == trie.next[i]) { continue; } ans[i] = trie.next[i].cnt; } if (ans[0] == 0 && ans[0] == ans[1]) { return 0; } if (ans[0] == 0) { return query(trie.next[1]); } if (ans[1] == 0) { return query(trie.next[0]); } return Math.min(ans[0] - 1 + query(trie.next[1]), ans[1] - 1 + query(trie.next[0])); } } //Binary tree class TreeNode { int val; int tier = -1; TreeNode parent; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } //binary tree dfs void tierTree(TreeNode root) { if (null == root) { return; } if (null != root.parent) { root.tier = root.parent.tier + 1; } else { root.tier = 0; } tierTree(root.left); tierTree(root.right); } //LCA start TreeNode[][] lca; TreeNode[] tree; void lcaDfsTree(TreeNode root) { if (null == root) { return; } tree[root.val] = root; TreeNode nxt = root.parent; int idx = 0; while (null != nxt) { lca[root.val][idx] = nxt; nxt = lca[nxt.val][idx]; idx++; } lcaDfsTree(root.left); lcaDfsTree(root.right); } TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException { if (null == root) { return null; } if (-1 == root.tier) { tree = new TreeNode[n + 1]; tierTree(root); } if (null == lca) { lca = new TreeNode[n + 1][31]; lcaDfsTree(root); } int z = Math.abs(x.tier - y.tier); int xx = x.tier > y.tier ? x.val : y.val; while (z > 0) { final int zz = z; int l = (int) BinSearch.bs(0, 31 , k -> zz < (1 << k)); xx = lca[xx][l].val; z -= 1 << l; } int yy = y.val; if (x.tier <= y.tier) { yy = x.val; } while (xx != yy) { final int xxx = xx; final int yyy = yy; int l = (int) BinSearch.bs(0, 31 , k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]); xx = lca[xx][l].val; yy = lca[yy][l].val; } return tree[xx]; } //LCA end //graph List<Integer> adj[]; List<int[]> adj2[]; void initGraph(int n, int m, boolean hasW, boolean directed, int type) throws IOException { if (type == 1) { adj = new List[n + 1]; } else { adj2 = new List[n + 1]; } for (int i = 1; i <= n; i++) { if (type == 1) { adj[i] = new ArrayList<>(); } else { adj2[i] = new ArrayList<>(); } } for (int i = 0; i < m; i++) { int f = nextInt(); int t = nextInt(); if (type == 1) { adj[f].add(t); if (!directed) { adj[t].add(f); } } else { int w = hasW ? nextInt() : 0; adj2[f].add(new int[]{t, w}); if (!directed) { adj2[t].add(new int[]{f, w}); } } } } void getDiv(Map<Integer, Integer> map, long n) { int sqrt = (int) Math.sqrt(n); for (int i = 2; i <= sqrt; i++) { int cnt = 0; while (n % i == 0) { cnt++; n /= i; } if (cnt > 0) { map.put(i, cnt); } } if (n > 1) { map.put((int) n, 1); } } boolean[] generatePrime(int n) { boolean p[] = new boolean[n + 1]; p[2] = true; for (int i = 3; i <= n; i += 2) { p[i] = true; } for (int i = 3; i <= Math.sqrt(n); i += 2) { if (!p[i]) { continue; } for (int j = i * i; j <= n; j += i << 1) { p[j] = false; } } return p; } boolean isPrime(long n) { //determines if n is a prime number int p[] = {2, 3, 5, 233, 331}; int pn = p.length; long s = 0, t = n - 1;//n - 1 = 2^s * t while ((t & 1) == 0) { t >>= 1; ++s; } for (int i = 0; i < pn; ++i) { if (n == p[i]) { return true; } long pt = pow(p[i], t, n); for (int j = 0; j < s; ++j) { long cur = llMod(pt, pt, n); if (cur == 1 && pt != 1 && pt != n - 1) { return false; } pt = cur; } if (pt != 1) { return false; } } return true; } long[] llAdd2(long a[], long b[], long mod) { long c[] = new long[2]; c[1] = (a[1] + b[1]) % (mod * mod); c[0] = (a[1] + b[1]) / (mod * mod) + a[0] + b[0]; return c; } long[] llMod2(long a, long b, long mod) { long x1 = a / mod; long y1 = a % mod; long x2 = b / mod; long y2 = b % mod; long c = (x1 * y2 + x2 * y1) / mod; c += x1 * x2; long d = (x1 * y2 + x2 * y1) % mod * mod + y1 * y2; return new long[]{c, d}; } long llMod(long a, long b, long mod) { if (a > mod || b > mod) { return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod; } return a * b % mod; // long r = 0; // a %= mod; // b %= mod; // while (b > 0) { // if ((b & 1) == 1) { // r = (r + a) % mod; // } // b >>= 1; // a = (a << 1) % mod; // } // return r; } long pow(long a, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * a; } a = a * a; n >>= 1; } return ans; } long pow(long a, long n, long mod) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = llMod(ans, a, mod); } a = llMod(a, a, mod); n >>= 1; } return ans; } private long[][] initC(int n) { long c[][] = new long[n][n]; for (int i = 0; i < n; i++) { c[i][0] = 1; } for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } return c; } /** * ps: n >= m, choose m from n; */ // private int cmn(long n, long m) { // if (m > n) { // n ^= m; // m ^= n; // n ^= m; // } // m = Math.min(m, n - m); // // long top = 1; // long bot = 1; // for (long i = n - m + 1; i <= n; i++) { // top = (top * i) % MOD; // } // for (int i = 1; i <= m; i++) { // bot = (bot * i) % MOD; // } // // return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD); // } long[] exGcd(long a, long b) { if (b == 0) { return new long[]{a, 1, 0}; } long[] ans = exGcd(b, a % b); long x = ans[2]; long y = ans[1] - a / b * ans[2]; ans[1] = x; ans[2] = y; return ans; } long gcd(long a, long b) { if (a < b) { return gcd(b, a); } while (b != 0) { long tmp = a % b; a = b; b = tmp; } return a; } int[] unique(int a[], Map<Integer, Integer> idx) { int tmp[] = a.clone(); Arrays.sort(tmp); int j = 0; for (int i = 0; i < tmp.length; i++) { if (i == 0 || tmp[i] > tmp[i - 1]) { idx.put(tmp[i], j++); } } int rs[] = new int[j]; j = 0; for (int key : idx.keySet()) { rs[j++] = key; } Arrays.sort(rs); return rs; } boolean isEven(long n) { return (n & 1) == 0; } static class BinSearch { static long bs(long l, long r, IBinSearch sort) throws IOException { while (l < r) { long m = l + (r - l) / 2; if (sort.binSearchCmp(m)) { l = m + 1; } else { r = m; } } return l; } interface IBinSearch { boolean binSearchCmp(long k) throws IOException; } } } private static class InOut { private BufferedReader br; private StreamTokenizer st; private PrintWriter pw; InOut() throws FileNotFoundException { if (useInFile) { System.setIn(new FileInputStream("resources/inout/in.text")); } if (useOutFile) { System.setOut(new PrintStream("resources/inout/out.text")); } br = new BufferedReader(new InputStreamReader(System.in)); st = new StreamTokenizer(br); pw = new PrintWriter(new OutputStreamWriter(System.out)); st.ordinaryChar('\''); st.ordinaryChar('\"'); st.ordinaryChar('/'); } private boolean hasNext() throws IOException { return st.nextToken() != StreamTokenizer.TT_EOF; } private long[] anLong(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } private String next() throws IOException { if (st.nextToken() == StreamTokenizer.TT_EOF) { throw new IOException(); } return st.sval; } private String next(int n) throws IOException { return next(n, false); } private String next(int len, boolean isDigit) throws IOException { char ch[] = new char[len]; int cur = 0; char c; while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t' || (isDigit && (c < '0' || c > '9'))) ; do { ch[cur++] = c; } while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') && (!isDigit || c >= '0' && c <= '9')); return String.valueOf(ch, 0, cur); } private int nextInt() throws IOException { if (st.nextToken() == StreamTokenizer.TT_EOF) { throw new IOException(); } return (int) st.nval; } private long nextLong(int n) throws IOException { return Long.parseLong(next(n, true)); } private double nextDouble() throws IOException { st.nextToken(); return st.nval; } private String[] nextSS(String reg) throws IOException { return br.readLine().split(reg); } private String nextLine() throws IOException { return br.readLine(); } private void print(String s, boolean newLine) { if (null != s) { pw.print(s); } if (newLine) { pw.println(); } } private void format(String format, Object... obj) { pw.format(format, obj); } private void flush() { pw.flush(); } } private static class FFT { double[] roots; int maxN; public FFT(int maxN) { this.maxN = maxN; initRoots(); } public long[] multiply(int[] a, int[] b) { int minSize = a.length + b.length - 1; int bits = 1; while (1 << bits < minSize) bits++; int N = 1 << bits; double[] aa = toComplex(a, N); double[] bb = toComplex(b, N); fftIterative(aa, false); fftIterative(bb, false); double[] c = new double[aa.length]; for (int i = 0; i < N; i++) { c[2 * i] = aa[2 * i] * bb[2 * i] - aa[2 * i + 1] * bb[2 * i + 1]; c[2 * i + 1] = aa[2 * i] * bb[2 * i + 1] + aa[2 * i + 1] * bb[2 * i]; } fftIterative(c, true); long[] ret = new long[minSize]; for (int i = 0; i < ret.length; i++) { ret[i] = Math.round(c[2 * i]); } return ret; } static double[] toComplex(int[] arr, int size) { double[] ret = new double[size * 2]; for (int i = 0; i < arr.length; i++) { ret[2 * i] = arr[i]; } return ret; } void initRoots() { roots = new double[2 * (maxN + 1)]; double ang = 2 * Math.PI / maxN; for (int i = 0; i <= maxN; i++) { roots[2 * i] = Math.cos(i * ang); roots[2 * i + 1] = Math.sin(i * ang); } } int bits(int N) { int ret = 0; while (1 << ret < N) ret++; if (1 << ret != N) throw new RuntimeException(); return ret; } void fftIterative(double[] array, boolean inv) { int bits = bits(array.length / 2); int N = 1 << bits; for (int from = 0; from < N; from++) { int to = Integer.reverse(from) >>> (32 - bits); if (from < to) { double tmpR = array[2 * from]; double tmpI = array[2 * from + 1]; array[2 * from] = array[2 * to]; array[2 * from + 1] = array[2 * to + 1]; array[2 * to] = tmpR; array[2 * to + 1] = tmpI; } } for (int n = 2; n <= N; n *= 2) { int delta = 2 * maxN / n; for (int from = 0; from < N; from += n) { int rootIdx = inv ? 2 * maxN : 0; double tmpR, tmpI; for (int arrIdx = 2 * from; arrIdx < 2 * from + n; arrIdx += 2) { tmpR = array[arrIdx + n] * roots[rootIdx] - array[arrIdx + n + 1] * roots[rootIdx + 1]; tmpI = array[arrIdx + n] * roots[rootIdx + 1] + array[arrIdx + n + 1] * roots[rootIdx]; array[arrIdx + n] = array[arrIdx] - tmpR; array[arrIdx + n + 1] = array[arrIdx + 1] - tmpI; array[arrIdx] += tmpR; array[arrIdx + 1] += tmpI; rootIdx += (inv ? -delta : delta); } } } if (inv) { for (int i = 0; i < array.length; i++) { array[i] /= N; } } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
7efa5f50414461cc76263f98bde831bd
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
// package faltu; import java.util.*; import java.util.Map.Entry; 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.math.BigInteger; public class Main { // ***********************MATHS--STARTS************************************************* // private static ArrayList<Long> get_divisor(long x) { ArrayList<Long>a=new ArrayList<Long>(); for(long i=1;i*i<=x;i++) { if(x%i==0) { a.add((long) i); if(x/i!=i)a.add(x/i); } } return a; } static long[] sieve; static long[] smallestPrime; public static void sieve() { int n=4000000+1; sieve=new long[n]; smallestPrime=new long[n]; sieve[0]=1; sieve[1]=1; for(int i=2;i<n;i++){ sieve[i]=i; smallestPrime[i]=i; } for(int i=2;i*i<n;i++){ if(sieve[i]==i){ for(int j=i*i;j<n;j+=i){ if(sieve[j]==j)sieve[j]=1; if(smallestPrime[j]==j||smallestPrime[j]>i)smallestPrime[j]=i; } } } } static long nCr(long n,long r,long MOD) { if(n<r)return 0; if(r==0)return 1; return fact[(int) n]*mod_inv(fact[(int) r],MOD)%MOD*mod_inv(fact[(int) (n-r)],MOD)%MOD; } static long[]fact; static void computeFact(long n,long MOD) { fact=new long[(int)n+1]; fact[0]=1; for(int i=1;i<=n;i++)fact[i]=(fact[i-1]*i%MOD)%MOD; } static long bin_expo(long a,long b,long MOD) { if(b == 0)return 1; long ans = bin_expo(a,b/2,MOD); ans = (ans*ans)%MOD; if(b % 2!=0){ ans = (ans*a)%MOD; } return ans%MOD; } static long mod_add(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} static long mod_mul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} static long mod_sub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} static long mod_inv(long n,long p) {return bin_expo(n,p-2,p);} static long gcd(long a, long b){if (a == 0) {return b;}return gcd(b % a, a); } static int gcd(int a, int b){if (a == 0) {return b; }return gcd(b % a, a); } static long lcm(long a,long b){return (a / gcd(a, b)) * b;} static long min(long x,long y) {return Math.min(x, y);}static long max(long x,long y) {return Math.max(x, y);} static int min(int x,int y) {return Math.min(x, y);}static int max(int x,int y) {return Math.max(x, y);} static ArrayList<String>powof2s; static void powof2S() { long i=1; while(i<(long)2e18) { powof2s.add(String.valueOf(i)); i*=2; } } static long power(long a, long b){ a %=MOD;long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static boolean coprime(int a, long l){return (gcd(a, l) == 1);} // ****************************MATHS-ENDS***************************************************** // ***********************BINARY-SEARCH STARTS*********************************************** public static int upperBound(long[] arr, long m, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(arr[mid]<=m) l=mid+1; else r=mid-1; } return l; } public static int lowerBound(long[] a, long m, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(a[mid]<m) l=mid+1; else r=mid-1; } return l; } public static int lowerBound(ArrayList<Integer> ar,int k){ int s=0,e=ar.size(); while (s!=e){ int mid = s+e>>1; if (ar.get(mid) <k)s=mid+1; else e=mid; } if(s==ar.size())return -1; return s; } public static int upperBound(ArrayList<Integer> ar,int k){ int s=0,e=ar.size(); while (s!=e){ int mid = s+e>>1; if (ar.get(mid) <=k)s=mid+1; else e=mid; } if(s==ar.size())return -1; return s; } public static long getClosest(long val1, long val2,long target){if (target - val1 >= val2 - target)return val2; else return val1;} static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } static int lowerLimitBinarySearch(ArrayList<Long> v,long k) { int n =v.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(v.get(mid) > k) { second = mid; }else { first = mid+1; } } if(first < n && v.get(first) < k) { first++; } return first; //1 index } public static int searchindex(long arr[], long t){int index = Arrays.binarySearch(arr, t);return (index < 0) ? -1 : index;} public static long[] sort(long[] a) {ArrayList<Long> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;} public static int[] sort(int[] a) {ArrayList<Integer> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;} // *******************************BINARY-SEARCH ENDS*********************************************** // *********************************GRAPHS-STARTS**************************************************** // *******----SEGMENT TREE IMPLEMENT---***** // -------------START--------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode){ if(start==end){ tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value){ if(start==end){ arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid)updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); else updateTree(arr,tree,start,mid,2*treeNode,idx,value); tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } long query(int[]arr,int[]tree,int start,int end,int treeNode,int qleft,int qright) { if(start>=qleft&&end<=qright)return tree[treeNode]; if(start>qright||end<qleft)return 0; int mid=(start+end)/2; long valLeft=query(arr,tree,start,mid-1,treeNode*2,qleft,qright); long valRight=query(arr,tree,mid+1,end,treeNode*2+1,qleft,qright); return valLeft+valRight; } // -------------ENDS--------------- //***********************DSU IMPLEMENT START************************* static int parent[]; static int rank[]; static int[]Size; static void makeSet(int n){ parent=new int[n]; rank=new int[n]; Size=new int[n]; for(int i=0;i<n;i++){ parent[i]=i; rank[i]=0; Size[i]=1; } } static void union(int u,int v){ u=findpar(u); v=findpar(v); if(rank[u]<rank[v]) { parent[u]=v; Size[v]+=Size[u]; } else if(rank[v]<rank[u]) { parent[v]=u; Size[u]+=Size[v]; } else{ parent[v]=u; rank[u]++; Size[u]+=Size[v]; } } private static int findpar(int node){ if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } // *********************DSU IMPLEMENT ENDS************************* // ****__________PRIMS ALGO______________________**** private static int prim(ArrayList<node>[] adj,int N,int node) { int key[] = new int[N+1]; int parent[] = new int[N+1]; boolean mstSet[] = new boolean[N+1]; for(int i = 0;i<N;i++) { key[i] = 100000000; mstSet[i] = false; } PriorityQueue<node> pq = new PriorityQueue<node>(N, new node()); key[node] = 0; parent[node] = -1; pq.add(new node( node,key[node])); for(int i = 0;i<N-1;i++) { int u = pq.poll().getV(); mstSet[u] = true; for(node it: adj[u]) { if(mstSet[it.getV()] == false && it.getW() < key[it.getV()]) { parent[it.getV()] = u; key[it.getV()] = (int) it.getW(); pq.add(new node(it.getV(), key[it.getV()])); } } } int sum=0; for(int i=1;i<N;i++) { System.out.println(key[i]); sum+=key[i]; } System.out.println(sum); return sum; } // ****____________DIJKSTRAS ALGO___________**** static int[]dist; static int dijkstra(int u,int n,ArrayList<node>adj[]) { dist=new int[n]; Arrays.fill(dist,Integer.MAX_VALUE); dist[u]=0; PriorityQueue<node>pq=new PriorityQueue<node>(new node()); pq.add(new node(u,0)); while(!pq.isEmpty()) { node v=pq.poll(); for(node it:adj[v.getV()]) { if(dist[it.getV()]>it.getW()+dist[v.getV()]) { dist[it.getV()]=(int) (it.getW()+dist[v.getV()]); pq.add(new node(it.getV(),dist[it.getV()])); } } } int sum=0; for(int i=1;i<n;i++){ System.out.println(dist[i]); sum+=dist[i]; } return sum; } private static void setGraph(int n,int m){ vis=new boolean[n+1]; indeg=new int[n+1]; // adj=new ArrayList<ArrayList<Integer>>(); // for(int i=0;i<=n;i++)adj.add(new ArrayList<>()); // for(int i=0;i<m;i++){ // int u=s.nextInt(),v=s.nextInt(); // adj.get(u).add(v); // adj.get(v).add(u); // } adj=new ArrayList[n+1]; // backadj=new ArrayList[n+1]; for(int i=0;i<=n;i++){ adj[i]=new ArrayList<Integer>(); // backadj[i]=new ArrayList<Integer>(); } for(int i=0;i<m;i++){ int u=s.nextInt(),v=s.nextInt(); adj[u].add(v); adj[v].add(u); // backadj[v].add(u); indeg[v]++; indeg[u]++; } // weighted adj // adj=new ArrayList[n+1]; // for(int i=0;i<=n;i++){ // adj[i]=new ArrayList<node>(); // } // for(int i=0;i<m;i++){ // int u=s.nextInt(),v=s.nextInt(); // long w=s.nextInt(); // adj[u].add(new node(v,w)); //// adj[v].add(new node(u,w)); // } } // *********************************GRAPHS-ENDS**************************************************** static int[][] dirs8 = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; static int[][] dirs4 = {{1,0},{-1,0},{0,1},{0,-1}}; //d-u-r-l static long MOD=(long) (1e9+7); static int prebitsum[][]; static boolean[] vis; static int[]indeg; // static ArrayList<ArrayList<Integer>>adj; static ArrayList<Integer> adj[]; static ArrayList<Integer> backadj[]; static FastReader s = new FastReader(System.in); public static void main(String[] args) throws IOException { // sieve(); // computeFact((int)1e7+1,MOD); // prebitsum=new int[2147483648][31]; // presumbit(prebitsum); // powof2S(); // // try { int tt = s.nextInt(); // int tt=1; for(int i=1;i<=tt;i++) { solver(); } // catch(Exception e) {return;} } private static void solver() { int n=s.nextInt(); long sum=0,cnt=1; long[]a=new long[n+1]; long[]ps=new long[n+1]; for(int i = 1; i <= n; i++){ a[i]=s.nextLong(); ps[i] = ps[i - 1] + a[i]; } if(ps[n] != 0){ System.out.println("NO"); return; } boolean ok = true; for(int i = 1; i <= n; i++){ if(ps[i] < 0) ok = false; } boolean visited_zero = false; for(int i = 1; i <= n; i++){ if(ps[i] == 0) visited_zero = true; else if(visited_zero) ok = false; } if(ok) System.out.println("YES"); else System.out.println("NO"); } /* *********************BITS && TOOLS &&DEBUG STARTS***********************************************/ static boolean issafe(int i, int j, int r,int c, int sx, int sy, int d,boolean[][]vis){ if (i < 0 || j < 0 || i >= r || j >= c||((Math.abs(i-sx)+Math.abs(j-sy))<=d)||vis[i][j]==true)return false; else return true; } static void presumbit(int[][]prebitsum) { for(int i=1;i<=200000;i++) { int z=i; int j=0; while(z>0) { if((z&1)==1) { prebitsum[i][j]+=(prebitsum[i-1][j]+1); }else { prebitsum[i][j]=prebitsum[i-1][j]; } z=z>>1; j++; } } } static void countOfSetBit(long[]a) { for(int j=30;j>=0;j--) { int cnt=0; for(long i:a) { if((i&1<<j)==1)cnt++; } // printing the current no set bit in all array element System.out.println(cnt); } } public static String revStr(String str){String input = str;StringBuilder input1 = new StringBuilder();input1.append(input);input1.reverse();return input1.toString();} static void printA(long[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();} static void printA(int[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();} static void pc2d(boolean[][] vis) { int n=vis.length; int m=vis[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(vis[i][j]+" "); } System.out.println(); } } static void pi2d(char[][] a) { int n=a.length; int m=a[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } static void p1d(int[]a) { for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } // *****************BITS && TOOLS &&DEBUG ENDS*********************************************** } // **************************I/O************************* class FastReader { public BufferedReader reader; public StringTokenizer tokenizer; public FastReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;} public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();} public int nextInt(){ return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() {String str = "";try {str = reader.readLine();}catch (IOException e) {e.printStackTrace();}return str;} } class dsu{ int n; static int parent[]; static int rank[]; static int[]Size; public dsu(int n) {this.n=n;this.parent=new int[n];this.rank=new int[n];this.Size=new int[n]; for(int i=0;i<n;i++){parent[i]=i;rank[i]=0;Size[i]=1;} } static int findpar(int node) {if(node==parent[node])return node;return parent[node]=findpar(parent[node]);} static void union(int u,int v){ u=findpar(u);v=findpar(v); if(u!=v) { if(rank[u]<rank[v]) {parent[u]=v;Size[v]+=Size[u];} else if(rank[v]<rank[u]) {parent[v]=u;Size[u]+=Size[v];} else{parent[v]=u;rank[u]++;Size[u]+=Size[v];} } } } class pair{ int x;int y; long u,v; public pair(int x,int y){this.x=x;this.y=y;} public pair(long u,long v) {this.u=u;this.v=v;} } class node implements Comparator<node>{ private int v; private long w; node(int _v, long _w) { v = _v; w = _w; } node() {} int getV() { return v; } long getW() { return w; } @Override public int compare(node node1, node node2) { if (node1.w < node2.w) return -1; if (node1.w > node2.w) return 1; return 0; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
89917df2d0bdd4f7622172d4e0bafe2d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.InputStreamReader; import java.util.*; public class Main { static Scanner cin = new Scanner(new InputStreamReader(System.in)); public static void main(String[] args) { int t = cin.nextInt(); while (t-->0) { solve(); } cin.close(); } private static void solve() { int n = cin.nextInt(); long temp; long sum = 0; String ans = ""; for(int i=1;i<=n;i++) { temp = cin.nextLong(); if(!ans.isEmpty()) { continue; } if (i>1) { if(sum<0) { ans = "No"; } if(sum==0 && temp!=0) { ans = "No"; } } sum = sum + temp; } if(ans.isEmpty()) { if(sum ==0) { ans = "Yes"; } else { ans = "No"; } } System.out.println(ans); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
40cb5a58e9a271f381c9e48ed79f6aad
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
//package com.example.lib; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class Algorithm { public static void main(String[] rgs) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu")); StringBuilder stringBuilder = new StringBuilder(); int t = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < t; i++) { int size= Integer.parseInt(bufferedReader.readLine()); String [] s= bufferedReader.readLine().split(" "); int [] input = new int[size]; boolean seenzpos=false; for (int j = 0; j < size; j++) { int c= Integer.parseInt(s[j]); if(c!=0)seenzpos=true; input[j]=c; } if(!seenzpos){ stringBuilder.append("YES\n"); continue; } if(input[0]<1 || size==1){ stringBuilder.append("NO\n"); continue; } long expected = -1*input[0]; boolean br=false; for (int j = 1; j < input.length; j++) { int cur= input[j]; if(j==input.length-1){ if(expected!=cur){ stringBuilder.append("NO\n"); br=true; } }else{ if(expected>cur){ stringBuilder.append("NO\n"); br=true; break; } if(expected==cur){ for (int k = j+1; k < input.length; k++) { if(input[k]!=0){ stringBuilder.append("NO\n"); br=true; break; } } break; }else{ expected-=cur; } } } if(br)continue; stringBuilder.append("YES\n"); } System.out.println(stringBuilder); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
3c723e23f3689322181ba92b09022007
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
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(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a) ? "Yes" : "No"); } sc.close(); } static boolean solve(int[] a) { int index = a.length - 1; while (index != -1 && a[index] == 0) { --index; } if (index == -1) { return true; } long sum = 0; while (true) { sum += a[index]; if (sum > 0) { return false; } if (sum == 0) { return index == 0; } --index; if (index == -1) { return false; } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f0779e2a7c4ba0daa51e52185fe89c1b
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.util.*; import static java.lang.System.out; import static java.lang.Math.*; public class pre25 { 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 class MultiSet<K> { TreeMap<K, Integer> map; MultiSet() { map = new TreeMap<>(); } void add(K a) { map.put(a, map.getOrDefault(a, 0) + 1); } boolean contains(K a) { return map.containsKey(a); } void remove(K a) { map.put(a, map.get(a) - 1); if (map.get(a) == 0) map.remove(a); } int occrence(K a) { return map.get(a); } K floor(K a) { return map.floorKey(a); } K ceil(K a) { return map.ceilingKey(a); } @Override public String toString() { ArrayList<K> set = new ArrayList<>(); for (Map.Entry<K, Integer> i : map.entrySet()) { for (int j = 1; j <= i.getValue(); j++) set.add(i.getKey()); } return set.toString(); } } static class Pair<K, V> { K value1; V value2; Pair(K a, V b) { value1 = a; value2 = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(this.value1, p.value1) && Objects.equals(this.value2, p.value2); } @Override public int hashCode() { int result = this.value1.hashCode(); result = 31 * result + this.value2.hashCode(); return result; } @Override public String toString() { return ("[" + value1 + " <=> " + value2 + "]"); } } static ArrayList<Integer> primes; static void setPrimes(int n) { boolean p[] = new boolean[n]; Arrays.fill(p, true); for (int i = 2; i < p.length; i++) { if (p[i]) { primes.add(i); for (int j = i * 2; j < p.length; j += i) p[j] = false; } } } static int mod = (int) (1e9 + 7); static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static void main(String args[]) { FastReader obj = new FastReader(); int tc = obj.nextInt(); while(tc--!=0){ int n = obj.nextInt(),arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = obj.nextInt(); long ptr = arr[0]; boolean ans = true; for(int i=1;i<n;i++){ if(ptr<0){ ans = false; break; }else if(ptr==0 && arr[i]!=0){ ans = false; break; }else ptr+=arr[i]; } ans &= ptr==0; out.println(ans?"YES":"NO"); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
e3e3e32221780e95db4f04e97941d22a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.Scanner; public class A1693 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); boolean ok = true; boolean seenZero = false; long sum = 0; for (int n=0; n<N; n++) { int a = in.nextInt(); sum += a; if (sum < 0) { ok = false; } else if (sum == 0) { seenZero = true; } if (seenZero && sum != 0) { ok = false; } } if (sum != 0) { ok = false; } System.out.println(ok ? "YES" : "NO"); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
a725539a4ba169eac8e6e460c79c49e4
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/* || श्री राम समर्थ || || जय जय रघुवीर समर्थ || */ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.util.*; import static java.util.Arrays.sort; /* Think until you get Good idea And then Code Easily Try Hard And Pay Attention to details (be positive always possible) think smart */ public class CodeforcesTemp { public static void main(String[] args) throws IOException { Reader scan = new Reader(); FastPrinter out = new FastPrinter(); int tt = scan.nextInt(); for (int tc = 1; tc <= tt; tc++) { int n= scan.nextInt(); long[] arr= scan.nextLongArray(n); long sum=0; for (int i = 0; i < n ; i++) { sum+=arr[i]; } if(sum!=0){ out.println("NO"); }else{ int j=n-1; while (j>=0 && arr[j]==0)--j; sum=0; boolean works=true; for (int i = 0; i <=j; i++) { sum+=arr[i]; if(sum>0)continue; else{ if(sum==0 && i==j){ continue; } else{ works=false; } } } out.println((works)?"YES":"NO"); } out.flush(); } out.close(); } static class Reader { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public Reader(InputStream in) { this.in = in; } public Reader() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } } throw new ArithmeticException( String.format(" overflows long.")); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } static class FastPrinter extends PrintWriter { public FastPrinter(PrintStream stream) { super(stream); } public FastPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { this.printArray(arr[i]); } } } static Random __r = new Random(); static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f6c3c5acdf16d2f7584851e8b05f6385
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); solver.solve(in.nextInt(), in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { for (int i = 0; i < testNumber; i++) { int n = in.nextInt(); Long[] a = new Long[n]; for (int j = 0; j < a.length; j++) { a[j] = in.nextLong(); } out.println(new Solution().solve(a) ? "YES" : "NO"); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } } class Solution { public boolean solve(Long[] a) { int n = a.length; long sum = 0; boolean b = false; for (int i = 0; i < n; i++) { if (b) { if (a[i] == 0) { continue; } return false; } sum += a[i]; if (sum < 0) { return false; } else if (sum == 0) { b = true; } } return sum == 0; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
c05e462fe6d126799cbbdf70ba001a5a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
//make sure to make new file! import java.io.*; import java.util.*; public class A800{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(f.readLine()); for(int q = 1; q <= t; q++){ int n = Integer.parseInt(f.readLine()); StringTokenizer st = new StringTokenizer(f.readLine()); long[] array = new long[n]; int lastn0 = -1; //last non-zero long sum = 0L; for(int k = 0; k < n; k++){ array[k] = Long.parseLong(st.nextToken()); sum += array[k]; if(array[k] != 0) lastn0 = k; } if(sum != 0){ out.println("No"); continue; } if(lastn0 == -1){ out.println("Yes"); continue; } boolean fail = false; long psum = 0L; for(int k = 0; k < lastn0; k++){ psum += array[k]; if(psum <= 0){ fail = true; break; } } if(fail){ out.println("No"); } else { out.println("Yes"); } } out.close(); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
a30d1ec47d97c4032472c2f06b1bb70e
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class DirectionalIncrease { public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(); int[] arr = new int[N]; for (int i = 0; i < N; i++) { arr[i] = in.nextInt(); } long current = 0; boolean valid = true; i: for (int i = 0; i < N; i++) { current += arr[i]; if (current < 0) { valid = false; break; } if (current == 0) { for (int j = i + 1; j < N; j++) { if (arr[j] != 0) { valid = false; break i; } } break; } } valid &= current == 0; out.println(valid ? "YES" : "NO"); } out.close(); } static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } public static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
1175180e50cf330cce16995be85b58b6
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; public class A { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); // int T=1; for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); int[] a=fs.readArray(n); if (a[0]>=0) { long sum=0; boolean works=true; boolean hitZero=false; for (int i:a) { sum+=i; if (sum<0) works=false; if (sum==0) hitZero=true; if (hitZero&&sum>0) works=false; } out.println(works && sum==0?"Yes":"No"); } else if (a[0]==0) { boolean works=true; for (int i=1; i<n; i++) if (a[i]!=0) works=false; out.println(works?"Yes":"No"); } else { out.println("No"); } } out.close(); } static final Random random=new Random(); static final int mod=1_000_000_007; static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 8
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
cc4e3a192685a39a4e4cbf530f428f6f
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; // Code by: @Oscar-gg // Problem from: public class DirectionalIncrease { // FastReader template from: // https://www.geeksforgeeks.org/java-competitive-programming-setup-in-vs-code-with-fast-i-o-and-snippets/ // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); for (int i = 0; i < t; i++) { solve(reader); } } public static void solve(FastReader reader) { int n = reader.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = reader.nextInt(); } long negatives = 0; long positives = 0; int index = 0; for (int i = n - 1; i >= 0; i--) { negatives += a[i]; if (negatives > 0) { System.out.println("No"); return; } positives += a[index++]; if (positives < 0) { System.out.println("No"); return; } else if (positives == 0) { for (int y = index; y < n; y++) { if (a[y] != 0) { System.out.println("No"); return; } } System.out.println("Yes"); return; } } System.out.println("Yes"); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
6857da1cc1f8bf9d62bc51b825c55042
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class A1{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; static final long mod=1000000007; static final int mod1=998244353; public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); if(n==1){ if(ar[0]==0) bw.write("YES\n"); else bw.write("NO\n"); return ; } long s=0; for(int i:ar) s=(long)s+i; int idx=0; for(int i=n-1;i>=0;i--){ if(ar[i]!=0){ idx=i;break; } } if(s==0 && Check(ar,idx)){ bw.write("YES\n"); } else bw.write("NO\n"); } public static boolean Check(int[] ar,int idx){ int n=ar.length; long s=0; for(int i=idx;i>0;i--){ s=(long)s+ar[i]; if(s>=0) return false; } return true; } /** Main Method**/ public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } /** Helpers**/ private static char[] getStr()throws IOException{ return br.readLine().toCharArray(); } private static int Gcd(int a,int b){ if(b==0) return a; return Gcd(b,a%b); } private static long Gcd(long a,long b){ if(b==0) return a; return Gcd(b,a%b); } private static int[] getArrIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static Integer[] getArrInP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Integer[] ar=new Integer[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static long[] getArrLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); long[] ar=new long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static Long[] getArrLoP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Long[] ar=new Long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static List<Integer> getListIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken())); return al; } private static List<Long> getListLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken())); return al; } private static long pow_mod(long a,long b) { long result=1; while(b!=0){ if((b&1)!=0) result=(result*a)%mod; a=(a*a)%mod; b>>=1; } return result; } private static int pow_mod(int a,int b) { int result=1; int mod1=(int)mod; while(b!=0){ if((b&1)!=0) result=(result*a)%mod1; a=(a*a)%mod1; b>>=1; } return result; } private static int lower_bound(int a[],int x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r+1; } private static long lower_bound(long a[],long x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r+1; } private static int upper_bound(int a[],int x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static long upper_bound(long a[],long x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static boolean Sqrt(int x){ int a=(int)Math.sqrt(x); return a*a==x; } private static boolean Sqrt(long x){ long a=(long)Math.sqrt(x); return a*a==x; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
630ced67534a1d2f11e4d486ae84da5c
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int q = cin.nextInt(); // int q = 1; label:while(q-->0){ int n = cin.nextInt(); long []presum = new long [n + 1]; int []arr = new int [n + 1]; for(int i = 1 ; i <= n ; i ++){ arr[i] = cin.nextInt(); presum[i] = presum[i-1]+arr[i]; } if(presum[n]!=0){ out.println("NO"); continue ; } boolean flag = true; for(int i = 1 ; i <= n ;i++){ if(presum[i]<0){ flag=false; out.println("NO"); continue label; } } boolean zero = false; for(int i = 1 ; i <= n ; i++){ if(presum[i]==0){ zero = true; }else if(zero){ out.println("NO"); continue label; } } out.println("YES"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f73d1575ed9bd0828a84828a74c25c73
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i] = sc.nextLong(); int j = n-1; while(j >= 0 && arr[j] == 0) j--; long sum = 0; String ans = "yes"; for(int i=0;i<=j;i++) { sum += arr[i]; if(sum < 0 || (sum == 0 && i != j)) { ans = "no"; break; } } if(sum != 0) ans = "no"; System.out.println(ans); } // #################### } // #################### }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
3325565d41f864ec3c66c76380ad73a3
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/* * Author Name: Raj Kumar * IDE: IntelliJ IDEA Ultimate Edition * JDK: 18 version * Date: 08-Jul-22 */ import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int q = cin.nextInt(); // int q = 1; label:while(q-->0){ int n = cin.nextInt(); long []presum = new long [n + 1]; int []arr = new int [n + 1]; for(int i = 1 ; i <= n ; i ++){ arr[i] = cin.nextInt(); presum[i] = presum[i-1]+arr[i]; } if(presum[n]!=0){ out.println("NO"); continue ; } boolean flag = true; for(int i = 1 ; i <= n ;i++){ if(presum[i]<0){ flag=false; out.println("NO"); continue label; } } boolean zero = false; for(int i = 1 ; i <= n ; i++){ if(presum[i]==0){ zero = true; }else if(zero){ out.println("NO"); continue label; } } out.println("YES"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
1680cd534e7e4e9505deebf00994fe1a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int q = cin.nextInt(); // int q = 1; label:while(q-->0){ int n = cin.nextInt(); long []presum = new long [n + 1]; int []arr = new int [n + 1]; for(int i = 1 ; i <= n ; i ++){ arr[i] = cin.nextInt(); presum[i] = presum[i-1]+arr[i]; } if(presum[n]!=0){ out.println("NO"); continue ; } boolean flag = true; for(int i = 1 ; i <= n ;i++){ if(presum[i]<0){ flag=false; out.println("NO"); continue label; } } boolean zero = false; for(int i = 1 ; i <= n ; i++){ if(presum[i]==0){ zero = true; }else if(zero){ out.println("NO"); continue label; } } out.println("YES"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
ff925c81421dfa5be66fed1c74e4086d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int q = cin.nextInt(); // int q = 1; label:while(q-->0){ int n = cin.nextInt(); long []presum = new long [n + 1]; int []arr = new int [n + 1]; for(int i = 1 ; i <= n ; i ++){ arr[i] = cin.nextInt(); presum[i] = presum[i-1]+arr[i]; } if(presum[n]!=0){ out.println("NO"); continue ; } boolean flag = true; for(int i = 1 ; i <= n ;i++){ if(presum[i]<0){ flag=false; out.println("NO"); continue label; } } boolean zero = false; for(int i = 1 ; i <= n ; i++){ if(presum[i]==0){ zero = true; }else if(zero){ out.println("NO"); continue label; } } out.println("YES"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 17
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
bce7811c6d0bfcdbb4cc3e34a606960d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; // REF No. 1654003260383_21910620_5 // Main Class public class Main { static class Utils{ public static void println(String s){ System.out.println(s); } } // Main driver method static final int MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setOut(new PrintStream( new FileOutputStream("output.txt"))); sc = new Scanner(new File("input.txt")); } catch (Exception e) { System.out.println(e); } } // Your Code Start Here // Read input Utils util = new Utils(); int t = sc.nextInt(); // int t = 1; while(t-- > 0){ solve(sc, util); } } private static void solve(Scanner sc, Utils util){ int n = sc.nextInt(); long[] arr = new long[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextLong(); } long sum = 0; for(long num : arr){ sum += num; } if(arr[0] < 0){ System.out.println("No"); return; } if(sum != 0){ System.out.println("No"); return; } long rem = 0; int i = n - 1; while(i > 0 && arr[i] == 0) i--; if(arr[i] > 0){ System.out.println("No"); return; } for(; i >= 0; i--){ rem -= arr[i]; if(rem <= 0){ break; } } if(rem == 0 && i == 0){ System.out.println("Yes"); } else System.out.println("No"); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
88bcc5c3a55a8f24b31b295809c44c49
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.Scanner; public class DirectionalIncrease { public static void main(String[] args) { try(Scanner myObj = new Scanner(System.in)) { int tests = myObj.nextInt(), n; boolean equalsFail, result; long[] target, temp; for(int i = 0; i < tests; i++) { // Get the input. n = myObj.nextInt(); equalsFail = false; target = new long[n]; temp = new long[n]; result = true; for(int j = 0; j < n; j++) { target[j] = myObj.nextLong(); } for(int j = n - 1; j > 0; j--) { // Indicates there are no more trailing zeroes // and equality breaking the process. if(target[j] != 0) equalsFail = true; // If it breaks at any point the result should be "No", // since the pointer is not on the first element. if(target[j] > temp[j] || (equalsFail && target[j] == temp[j])) { result = false; break; } else { temp[j - 1] += temp[j] - target[j]; temp[j] -= temp[j] - target[j]; } } if(result && temp[0] == target[0]) System.out.println("Yes"); else System.out.println("No"); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
6bd9ba74916496551d39312919b1f57a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class DirectionalIncrease { public static void main(String[] args) { try(Scanner myObj = new Scanner(System.in)) { int tests = myObj.nextInt(), n; boolean equalsFail, result; long[] target, temp; for(int i = 0; i < tests; i++) { // Get the input. n = myObj.nextInt(); equalsFail = false; target = new long[n]; temp = new long[n]; result = true; for(int j = 0; j < n; j++) { target[j] = myObj.nextLong(); } for(int j = n - 1; j > 0; j--) { if(target[j] != 0) equalsFail = true; if(target[j] > temp[j] || (equalsFail && target[j] == temp[j])) { result = false; break; } else { temp[j - 1] += temp[j] - target[j]; temp[j] -= temp[j] - target[j]; } } // if(Arrays.equals(temp, target)) // System.out.println("Yes"); // else // System.out.println("No"); if(result && temp[0] == target[0]) System.out.println("Yes"); else System.out.println("No"); } } } } /* 1 4 1 -1 1 -1 */
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
1efc7fd520318dc0e9abe012d200955c
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class A_Directional_Increase { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); long a[] = f.nextArray(n); for(int i = 1; i < n; i++) { a[i] += a[i-1]; } if(a[n-1] != 0) { out.println("No"); return; } for(int i = 0; i < n; i++) { if(a[i] < 0) { out.println("No"); return; } } int ind = 0; while(ind < n && a[ind] != 0) { ind++; } for(int i = ind; i < n; i++) { if(a[i] != 0) { out.println("No"); return; } } out.println("Yes"); } public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm(int a, int b) { int lcm = gcd(a, b); int hcf = (a * b) / lcm; return hcf; } public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } long[] nextArray(int n) { long[] a = new long[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f290baec41cd2322cb8fced7e1043dea
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class C { void go() { int n = Reader.nextInt(); int[] A = new int[n]; boolean ans = true; for(int i = 0; i < n; i++) { A[i] = Reader.nextInt(); } if(A[0] < 0) { ans = false; } else { long add = A[0]; for(int i = 1; i < n; i++) { if(add == 0) { if(A[i] != 0) { ans = false; break; } } else { add += A[i]; if(add < 0) { ans = false; break; } } } ans = ans && add == 0; } Writer.println(ans ? "YES" : "NO"); } void solve() { for(int T = Reader.nextInt(); T > 0; T--) go(); } void run() throws Exception { Reader.init(System.in); Writer.init(System.out); solve(); Writer.close(); } public static void main(String[] args) throws Exception { new C().run(); } public static class Reader { public static StringTokenizer st; public static BufferedReader br; public static void init(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public static String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new InputMismatchException(); } } return st.nextToken(); } public static int nextInt() { return Integer.parseInt(next()); } public static long nextLong() { return Long.parseLong(next()); } public static double nextDouble() { return Double.parseDouble(next()); } } public static class Writer { public static PrintWriter pw; public static void init(OutputStream os) { pw = new PrintWriter(new BufferedOutputStream(os)); } public static void print(String s) { pw.print(s); } public static void print(char c) { pw.print(c); } public static void print(int x) { pw.print(x); } public static void print(long x) { pw.print(x); } public static void println(String s) { pw.println(s); } public static void println(char c) { pw.println(c); } public static void println(int x) { pw.println(x); } public static void flush() { pw.flush(); } public static void println(long x) { pw.println(x); } public static void close() { pw.close(); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
fb29208a3bf10e5f8ab141efc65780f2
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class cf { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int T = Integer.parseInt(bf.readLine()); for(int test = 0; test < T; test++){ int n = Integer.parseInt(bf.readLine()); StringTokenizer stk = new StringTokenizer(bf.readLine()); long[] a = new long[n]; for(int i = 0; i < n; i++)a[i] = Long.parseLong(stk.nextToken()); long[] b = new long[n]; b[0] = a[0]; for(int i = 1; i < n; i++) b[i] = b[i-1]+a[i]; if(b[n-1] != 0){ pw.println("NO"); continue; } boolean ok = true; for(int i = 0; i < n; i++){ if(b[i] < 0) ok = false; } boolean visited_zero = false; for(int i = 0; i < n; i++){ if(b[i] == 0) visited_zero = true; else if(visited_zero) ok = false; } if(ok) pw.println("YES"); else pw.println("NO"); } pw.close(); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
903a7f5e3bfab12a1379bf2840ccddab
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
// package cp; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; import java.util.Map.Entry; public class Solution { static class pair { int e1;int p,c; pair(int e1,int p,int c) { this.e1=e1; this.p=p; this.c=c; } } public static void main(String hi[]) throws Exception { PrintWriter out = new PrintWriter(System.out); FastReader ob=new FastReader(); int T = ob.nextInt(); StringBuilder sb = new StringBuilder(); while(T-->0) { int n=ob.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=ob.nextInt(); } if(helper(n,arr)) { System.out.println("Yes"); } else { System.out.println("No"); } } out.flush(); } static boolean helper(int n,int arr[]) { if(n == 1) { if(arr[0] == 0) { return true; } else { return false; } } long pre[]=new long[n]; pre[0]=(long)arr[0]; for(int i=1;i<n;i++) { pre[i]=pre[i-1]+(long)arr[i]; } if(pre[n-1] != 0) { return false; } for(int i=0;i<n;i++) { if(pre[i] < 0) { return false; } } for(int i=0;i<n;i++) { if(pre[i] == 0) { for(int j=i;j<n;j++) { if(pre[j] != 0) { return false; } } break; } } return true; } static int bs(List<Long> a,int l,int h,long tar) { int res=Integer.MAX_VALUE; while(l <= h) { int mid=(l+h)>>1; if(a.get(mid) == tar) { l=mid+1; } else if(a.get(mid) > tar) { res=Math.min(res, mid); h=mid-1; } else { l=mid+1; } } return res; } static boolean helper(int arr[],int n,int m) { if(n > m) { return false; } else { Arrays.sort(arr); int chair_left=m; for(int i=0;i<n;i++) { if(i == 0) { if(chair_left >= arr[i]+1+arr[i]) { chair_left-=1; } else { return false; } } else if(i == n-1) { if(chair_left-arr[i]-1-arr[i] < 0) { return false; } } else { if(chair_left-arr[i]-1 < 0) { return false; } else { chair_left=chair_left-(arr[i]+1); } } } return true; } } static int isSum(int n) { int n1=n;int sum=0; while(n1 > 0) { int a=n1 % 10; sum+=a; n1=n1 / 10; } return sum; } static int isCheck(long s) { double k = (-1.0 + Math.sqrt(1 + 8 * s)) / 2; if (Math.ceil(k) == Math.floor(k)) { return (int)k; } else { return -1; } } static String middle(int i) { String str=""; for(int j=1;j<=i;j++) { str+="()"; } return str; } static String start(int i,int n) { String str=""; for(int j=1;j<=(n-i);j++) { str+="("; } return str; } static String close(int i,int n) { String str=""; for(int j=1;j<=(n-i);j++) { str+=")"; } return str; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } } 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
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
00559b99c12aa58c540e2b207714a90c
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
//package com.jayant; import java.io.*; import java.util.*; public class Solutions { public static void main(String[] args) { int tc = io.nextInt(); for (int i = 0; i < tc; i++) { solve(); } io.close(); } private static void solve() { int n = io.nextInt(); boolean flag = true, visited_zero = false; int[] nums = new int[n + 1]; long[] arr = new long[n + 1]; for (int i = 1; i <= n; i++) { nums[i] = io.nextInt(); arr[i] = arr[i-1] + nums[i]; } if(arr[n] != 0){ System.out.println("No"); }else{ for(int i = 1; i <= n; i++){ if(arr[i] < 0) flag = false; } for (int i = 1; i <= n; i++) { if(arr[i] == 0) visited_zero = true; else if(visited_zero) flag = false; } if(flag == true) System.out.println("YES"); else System.out.println("NO"); } // int ptr = 0, mid = (nums.length%2 == 0) ? nums.length/2 : nums.length/2; // for (int i = 0; i < n; i++) { // if(i == 0){ // arr[ptr] += 1; // ptr++; // }else if(i == nums.length-1){ // arr[ptr] -= 1; // ptr--; // }else if(i < mid) { // arr[ptr] -= 1; // ptr--; // arr[ptr] += 1; // ptr++; // }else { // arr[ptr] += 1; // ptr++; // arr[ptr] -= 1; // ptr--; // } // } // for (int i = 0; i <= n; i++) { // if(nums[i] != arr[i]){ // flag = false; // break; // } // } // // if(flag == true && ptr == 0) System.out.println("YES"); // else System.out.println("NO"); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(a.length); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private final InputStream stream; private final byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(next()); } } //-------------------------------------------------------- }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b79d8ab30db9aad8da78f441706b6e97
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
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 Main { public static void main (String[] args) throws java.lang.Exception { Scanner scn = new Scanner(System.in); int t= scn.nextInt(); while(t-->0){ int n= scn.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i]=scn.nextInt(); } if(isPossibl(arr)){ System.out.println("Yes"); }else { System.out.println("No"); } } } public static boolean isPossibl(int[] arr){ int i=arr.length-1; long sum=0l; while(i>=0 && arr[i]==0){ i--; } if(i<0){ return true; } if(arr[i]>0){ return false; } while(i>0){ sum=sum-arr[i]; if(sum<=0) return false; i--; } if(arr[i]==sum) return true; return false; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
601f8ab6116bff22427f896dac758924
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
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 Main { public static void main (String[] args) throws java.lang.Exception { Scanner scn = new Scanner(System.in); int t= scn.nextInt(); while(t-->0){ int n= scn.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i]=scn.nextInt(); } if(isPossible(arr)){ System.out.println("Yes"); }else { System.out.println("No"); } } } public static boolean isPossible(int[] arr){ int i=arr.length-1; long sum=0l; while(i>=0 && arr[i]==0){ i--; } if(i<0){ return true; } if(arr[i]>0){ return false; } while(i>0){ sum=sum-arr[i]; if(sum<=0) return false; i--; } if(arr[i]==sum) return true; return false; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
93c36705a8aa6418a71eeb59351e68ec
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)32768; static StringBuilder sb = new StringBuilder(); /* start */ public static void main(String [] args) { int testcases = 1; testcases = i(); // calc(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); int a[] = input(n); if(allO(a,n)) { pl("Yes"); return; } if(a[0]<=0) { pl("No"); return ; } int j=n-1; while(a[j]==0) j--; if(a[j]>=0) { pl("No"); return ; } if(a[0]<=0) { pl("No"); return ; } long sum = 0; int i = 0; for(i=0;i<=j;i++) { sum+=a[i]; if(sum==0) { break; } if(sum<0) { pl("No"); return ; } } if(sum==0 && i==j) { pl("Yes"); } else pl("No"); } static boolean allO(int a[],int n) { for(int i=0;i<n;i++) { if(a[i]!=0) return false; } return true; } /* end */ 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; } } // print code start static void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static void pl() { out.println(""); } // print code end // static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first < p.first) return 1; else if (first > p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } // segment t start static long seg[] ; static void build(long a[], int v, int tl, int tr) { if (tl == tr) { seg[v] = a[tl]; } else { int tm = (tl + tr) / 2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } static long query(int v, int tl, int tr, int l, int r) { if (l > r || tr < tl) return Integer.MAX_VALUE; if (l == tl && r == tr) { return seg[v]; } int tm = (tl + tr) / 2; return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r)); } static void update(int v, int tl, int tr, int pos, long new_val) { if (tl == tr) { seg[v] = new_val; } else { int tm = (tl + tr) / 2; if (pos <= tm) update(v*2, tl, tm, pos, new_val); else update(v*2+1, tm+1, tr, pos, new_val); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } // segment t end // }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
bffa4fa3e68f7642f868717cc0290a88
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class pb3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0){ int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i< n ; i++){ arr[i] = sc.nextLong(); } int ind = n - 1; while (ind >= 0 && arr[ind] == 0){ ind--; } if (ind < 0){ System.out.println("Yes"); continue; } System.out.println(solve(arr , ind , n)); } } static String solve(long[] arr , int ind , int n){ long[] ans = new long[n]; for (int i = ind ; i >= 1 ; i--){ if (arr[i] >= ans[i]) return "No"; ans[i-1] = (ans[i] - arr[i]); } // System.out.println(Arrays.toString(ans)); if (ans[0] == arr[0]) return "Yes"; return "No"; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0f49e7783b6cb66231f6ee53aa6ca7b1
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class R800D1A { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream input; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { input = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) { try { input = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } catch (IOException e) { e.printStackTrace(); } } public String readLine() { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong(){ long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i ++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for(int i = 0; i < n; i ++) { a[i] = nextLong(); } return a; } private void fillBuffer() { try { bytesRead = input.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } catch(IOException e) { e.printStackTrace(); } } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() { try { if (input == null) return; input.close(); } catch(IOException e) { e.printStackTrace(); } } } static class FastWriter { BufferedWriter output; public FastWriter() { output = new BufferedWriter( new OutputStreamWriter(System.out)); } void write(String s) { try { output.write(s); } catch (IOException e) { e.printStackTrace(); } } void endLine() { try { output.write("\n"); output.flush(); } catch (IOException e) { e.printStackTrace(); } } void writeInt(int x) { write(Integer.toString(x)); endLine(); } void writeLong(long x) { write(Long.toString(x)); endLine(); } void writeLine(String line) { write(line); endLine(); } void writeIntArray(int[] a) { write(Integer.toString(a[0])); for (int i = 1; i < a.length; i++) { write(" "+Integer.toString(a[i])); } endLine(); } void writeLongArray(long[] a) { write(Long.toString(a[0])); for (int i = 1; i < a.length; i++) { write(" "+Long.toString(a[i])); } endLine(); } void writeBoolArray(boolean[] a) { write(Boolean.toString(a[0])); for (int i = 1; i < a.length; i++) { write(" "+Boolean.toString(a[i])); } endLine(); } } public static class Utility { public static void safeSort(int[] a) { shuffle(a); Arrays.sort(a); } public static void safeSort(long[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { Random r = new Random(); for(int i = 0; i < a.length-2; i ++) { int j = i + r.nextInt(a.length - i); int t = a[i]; a[i] = a[j]; a[j] = t; } } public static void shuffle(long[] a) { Random r = new Random(); for(int i = 0; i < a.length-2; i ++) { int j = i + r.nextInt(a.length - i); long t = a[i]; a[i] = a[j]; a[j] = t; } } } public static void main(String[] args) { Reader r = new Reader(); FastWriter w = new FastWriter(); int cases = r.nextInt(); for(int test = 0; test < cases; test ++) { //INPUT int n = r.nextInt(); //int k = r.nextInt(); int[] a = r.nextIntArray(n); //CODE long[] suf = new long[n]; long[] pre = new long[n]; boolean[] suf0 = new boolean[n]; pre[0] = a[0]; suf[n-1] = a[n-1]; suf0[n-1] = a[n-1] == 0; for(int i = 1; i < n; i ++) { pre[i] = pre[i-1]+a[i]; suf[n-i-1] = suf[n-i]+a[n-i-1]; suf0[n-i-1] = suf0[n-i] && (a[n-i] == 0); } String output = "YES"; for(int i = 0; i < n-1; i ++) { if(pre[i] < 0 || pre[i] != -1*suf[i+1] || pre[i] == 0 && !suf0[i+1]) output = "NO"; } if(n == 1 && a[0] != 0) output = "NO"; //END CODE //OUTPUT w.writeLine(output); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
345a07f4e9bcb2a226e16ee1af04683a
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Test { private static final PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } private static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException ignored) { } return neg ? -ans : ans; } private static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException ignored) { } return neg ? -ans : ans; } private static String readString() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c; (c = System.in.read()) != -1; ) { if (!Character.isWhitespace(c)) { start = true; b.append((char) c); } else if (start) break; } } catch (IOException ignored) { } return b.toString(); } private static int readChars(char[] a, int off) { int cnt = 0; try { boolean start = false; for (int c; (c = System.in.read()) != -1; ) { if (!Character.isWhitespace(c)) { start = true; a[off + cnt++] = (char) c; } else if (start) break; } } catch (IOException ignored) { } return cnt; } void start() { int t = readInt(); next: while (t-- > 0) { int n = readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = readInt(); long incs = 0; for (int i = 0; i < n; i++) { if (incs < 0) { writer.println("No"); continue next; } if (incs == 0) { if (i != 0 && a[i] != 0) { writer.println("No"); continue next; } } long decs = incs; incs = a[i] + decs; } writer.println(incs == 0 ? "Yes" : "No"); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
9ea74621eea73efba3fd1623845ab6d5
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class DirectionalIncrease { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(); int arr[] = new int [n]; long total = 0; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); total += arr[i]; } if (total != 0 || arr[0] < 0 || arr[n - 1] > 0) { System.out.println("NO"); continue; } solve(n, arr); } } public static void solve(int n, int[]arr) { long prefixsum = arr[n - 1]; for (int i = n - 2; i > 0; i--) { if (arr[i] == 0 )continue; prefixsum += arr[i]; if (prefixsum >= 0) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
859f6e83cc4ff98020237042bcfb3baf
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; public class SelectionSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); boolean ans = true, checkCycleComplete = false; long sum = 0; for(int i=1; i<=n; i++) { long a = sc.nextInt(); sum+=a; if(checkCycleComplete && a!=0) ans = false; if(sum==0 && !checkCycleComplete) checkCycleComplete = true; if(sum<0) ans = false; } if(ans&&sum==0) System.out.println("Yes"); else System.out.println("No"); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
0a38a2413b46835f4e34c02301ccb3f4
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/* Rating: 1378 Date: 29-07-2022 Time: 22-03-17 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney ----------------------------Jai Shree Ram---------------------------- */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A_Directional_Increase { public static void s() { int n = sc.nextInt(); long[] arr = sc.readLongArray(n); for(int i=1; i<arr.length; i++) arr[i] += arr[i-1]; if(arr[n-1] != 0) { p.writeln("No"); return; } for(int i=0; i<n; i++) { if(arr[i] < 0) { p.writeln("No"); return; } } for(int i=0; i<n; i++) { if(arr[i] == 0) { for(int j=i+1; j<n; j++) { if(arr[j] != 0) { p.writeln("No"); return; } } p.writeln("Yes"); return; } } p.writeln("Yes"); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { static void sort(int... a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long... a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static int max(int... a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int... a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long... a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long... a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long... a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int... a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int... arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long... arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int... arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
ff7711413e23423d90a232a75174141d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static int globalVariable = 123456789; static String author = "pl728 on codeforces"; public static void main(String[] args) { FastReader sc = new FastReader(); MathUtils mathUtils = new MathUtils(); ArrayUtils arrayUtils = new ArrayUtils(); int tc = sc.ni(); while (tc-- != 0) { int n = sc.ni(); long[] a = sc.readLongArray(n); if(n == 1) { if(a[0] == 0) { System.out.println("Yes"); } else { System.out.println("No"); } continue; } // current sum cannot be <= 0 before we reach the last nonzero element int lastNonZeroElem = n - 1; while(lastNonZeroElem != -1 && a[lastNonZeroElem] == 0) lastNonZeroElem--; if(lastNonZeroElem == -1) { System.out.println("Yes"); continue; } long currSum = a[0]; if(currSum <= 0) { System.out.println("No"); continue; } boolean ans = true; for(int i = 1; i <= lastNonZeroElem; i++) { currSum += a[i]; if(currSum <= 0 && i != lastNonZeroElem) { ans = false; break; } } if(ans && currSum == 0) { System.out.println("Yes"); } else { System.out.println("No"); } } } static class FastReader { /** * Uses BufferedReader and StringTokenizer for quick java I/O * get next int, long, double, string * get int, long, double, string arrays, both primitive and wrapped object when array contains all elements * on one line, and we know the array size (n) * next: gets next space separated item * nextLine: returns entire line as space */ BufferedReader br; StringTokenizer st; public FastReader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } // to parse something else: // T x = T.parseT(fastReader.next()); public int ni() { return Integer.parseInt(next()); } public String ns() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String[] readStringArrayLine(int n) { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line.split(" "); } public String[] readStringArrayLines(int n) { String[] result = new String[n]; for (int i = 0; i < n; i++) { result[i] = this.next(); } return result; } public int[] readIntArray(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long[] readLongArray(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = this.nl(); } return result; } public Integer[] readIntArrayObject(int n) { Integer[] result = new Integer[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long nl() { return Long.parseLong(next()); } public char[] readCharArray(int n) { return this.ns().toCharArray(); } } static class MathUtils { public MathUtils() { } public long gcdLong(long a, long b) { if (a % b == 0) return b; else return gcdLong(b, a % b); } public long lcmLong(long a, long b) { return a * b / gcdLong(a, b); } } static class ArrayUtils { public ArrayUtils() { } public static int[] reverse(int[] a) { int n = a.length; int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } return b; } public int sumIntArrayInt(int[] a) { int ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } public long sumLongArrayLong(int[] a) { long ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } } public static int lowercaseToIndex(char c) { return (int) c - 97; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
b96b34583568a20144d78b440ae6cefc
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
//<———My cp———— import java.util.*; import java.io.*; public class A_Directional_Increase{ public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int[] vals = new int[n]; for(int i = 0;i<n;i++){ vals[i]=fr.nextInt(); } long[] preSum = new long[n]; long currSum = 0; boolean zeroFound = false; boolean poss = true; for(int i =0;i<n;i++){ currSum = currSum+vals[i]; preSum[i]=currSum; if(currSum<0){ poss=false; } if(currSum==0){ zeroFound=true; } if(currSum>0&&zeroFound){ poss=false; } } if(preSum[n-1]!=0){ poss=false; } if(!poss){ System.out.println("No"); }else{ System.out.println("Yes"); } } } public static void print(Object val){ System.out.print(val); } public static void println(Object val){ System.out.println(val); } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
899dbeaa5524b3369a3689dcd1f1e742
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; import java.util.Scanner; public class Gfg{ public static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]){ Scanner s = new Scanner(System.in); int t=s.nextInt(); for(int g=0;g<t;g++){ int n= s.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=s.nextLong(); } boolean b[] = new boolean [n]; b[n-1] = (arr[n-1]==0?true:false); for(int i=n-2;i>=0;i--){ if(b[i+1]&&arr[i]==0)b[i]=true; else b[i]=false; } long sum = 0; for(int i=0;i<n;i++){ sum+=arr[i]; if(i==n-1){ if(sum!=0)out.println("NO"); else out.println("YES"); } else{ if(sum==0 && b[i+1]){out.println("YES");break;} else if(sum==0&&b[i+1]==false){out.println("NO");break;} else if(sum<0){out.println("NO");break;} } } } out.flush(); } public static boolean find(int k, int arr[]){ for(int i=0;i<arr.length;i++){ if(arr[i]==k)return true; } return false; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
cf29ed189d7de8cbac162e399464179b
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
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(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a) ? "Yes" : "No"); } sc.close(); } static boolean solve(int[] a) { int index = a.length - 1; while (index != -1 && a[index] == 0) { --index; } if (index == -1) { return true; } long sum = 0; while (true) { sum += a[index]; if (sum > 0) { return false; } if (sum == 0) { return index == 0; } --index; if (index == -1) { return false; } } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
8d047d784f1c12f81d3dffc7de7e3a4c
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import com.sun.security.jgss.GSSUtil; import java.io.*; import java.util.*; public class Main { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } public void yOrn(boolean flag){ if(flag){ wr.println("YES"); }else { wr.println("NO"); } } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S> { private F first; private S second; Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S> pair = (Pair<F, S>) o; return first == pair.first && second == pair.second; } @Override public int hashCode() { return Objects.hash(first, second); } } class PairThree<F, S, X> { private F first; private S second; private X third; PairThree(F first, S second,X third) { this.first = first; this.second = second; this.third=third; } public F getFirst() { return first; } public void setFirst(F first) { this.first = first; } public S getSecond() { return second; } public void setSecond(S second) { this.second = second; } public X getThird() { return third; } public void setThird(X third) { this.third = third; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o; return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third); } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lower_bound(int array[], int key) { int low = 0, high = array.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) { high = mid; } else { low = mid + 1; } } if (low < array.length && array[low] < key) { low++; } return low; } boolean isPowerOfTwo(int n){ int counter=0; while (n!=0){ if((n&1)==1){ counter++; } n=n>>1; } return counter<=1; } int getNoOfChar(String no,String find){ int counter=0; int pointer=1; for(int i=no.length()-1;i>=0;i--){ if(no.charAt(i)==find.charAt(pointer)){ pointer--; if(pointer<0 ){ break; } }else { counter++; } } if(pointer<0){ return counter; }else { return Integer.MAX_VALUE; } } public boolean isPrime(int n){ if(n == 2 || n == 3) return false; boolean flag=true; for(int i=2;i<n;i++){ if(n%i==0){ flag=false; } } return flag; } boolean isSmaller(String str1, String str2) { int n1 = str1.length(), n2 = str2.length(); if (n1 < n2) return true; if (n2 < n1) return false; for (int i = 0; i < n1; i++) { if (str1.charAt(i) < str2.charAt(i)) return true; else if (str1.charAt(i) > str2.charAt(i)) return false; } return false; } String findDiff(String str1, String str2) { if (isSmaller(str1, str2)) { String t = str1; str1 = str2; str2 = t; } String str = ""; int n1 = str1.length(), n2 = str2.length(); int diff = n1 - n2; int carry = 0; for (int i = n2 - 1; i >= 0; i--) { int sub = (((int)str1.charAt(i + diff) - (int)'0') - ((int)str2.charAt(i) - (int)'0') - carry); if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += String.valueOf(sub); } for (int i = n1 - n2 - 1; i >= 0; i--) { if (str1.charAt(i) == '0' && carry > 0) { str += "9"; continue; } int sub = (((int)str1.charAt(i) - (int)'0') - carry); if (i > 0 || sub > 0) str += String.valueOf(sub); carry = 0; } return new StringBuilder(str).reverse().toString(); } public int power(int n,int p,int mod){ int ans=1; for(int i=1;i<=p;i++){ ans=(ans<<1)%mod; } return ans; } public boolean isPalindromeTime(int time){ time=(time)%1440; String hour=String.valueOf((time/60)); String minute=String.valueOf((time%60)); if(hour.length()==1){ hour="0"+hour; } if(minute.length()==1){ minute="0"+minute; } if(hour.charAt(0)==minute.charAt(1) &&hour.charAt(1)==minute.charAt(0)){ return true; }else { return false; } } public void go() throws IOException { int n=ni(); long[] arr=nal(n); long sum=0; int end=n-1; while (end>=0&& arr[end]==0){ end--; } boolean flag=true; for(int i=0;i<=end;i++){ sum+=arr[i]; if(sum<0 || (sum==0 && i<end)){ flag=false; } } if(flag && sum==0){ wr.println("YES"); }else { wr.println("NO"); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
8373001a3467bc2df836c2ca8a8f31d5
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; /** * @author atulanand */ public class Solution { static class Result { public String solve(int[] arr) { long[] pf = new long[arr.length]; pf[0] = arr[0]; if (pf[0] < 0) { return "No"; } for (int i = 1; i < arr.length; i++) { pf[i] = pf[i - 1] + arr[i]; if (pf[i] < 0) { return "No"; } } if (pf[arr.length - 1] != 0) { return "No"; } boolean viz = false; for (long i : pf) { if (i == 0) { viz = true; } else if (viz == true) { return "No"; } } return "Yes"; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = inputInt(br); Result result = new Result(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int[] spec = inputIntArray(br); sb.append(result.solve(inputIntArray(br))).append("\n"); } System.out.println(sb); } public static char[] inputCharArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); char[] arr = new char[spec.length - 1]; for (int i = 1; i < spec.length; i++) arr[i - 1] = spec[i].toCharArray()[0]; return arr; } public static char[][] inputCharGrid(BufferedReader br, int rows) throws IOException { char[][] grid = new char[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputString(br).toCharArray(); } return grid; } public static int[][] inputIntGrid(BufferedReader br, int rows) throws IOException { int[][] grid = new int[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputIntArrayW(br); } return grid; } public static char[] inputCharArray(BufferedReader br) throws IOException { return inputString(br).toCharArray(); } public static String inputString(BufferedReader br) throws IOException { return br.readLine().trim(); } public static int inputInt(BufferedReader br) throws IOException { return Integer.parseInt(inputString(br)); } public static long inputLong(BufferedReader br) throws IOException { return Long.parseLong(inputString(br)); } public static int[] inputIntArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } public static int[] inputIntArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length - 1]; for (int i = 1; i < spec.length; i++) arr[i - 1] = Integer.parseInt(spec[i]) - 1; return arr; } public static long[] inputLongArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); long[] arr = new long[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Long.parseLong(spec[i]); return arr; } private String stringify(char[] chs) { StringBuilder sb = new StringBuilder(); for (char ch : chs) { sb.append(ch); } return sb.toString(); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
7113a71445bf1808b33f8e78d9d774e3
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; public class C { public static void main(String[] args) { FScanner sc=new FScanner(); PrintWriter out=new PrintWriter(System.out); int T=sc.nextInt(); while(T-->0) { int n = sc.nextInt(); long sum=0; int[] a = sc.readArray(n); boolean judge=true, zero=false; for(int i=0; i<n; i++) { sum+=a[i]; if(sum<0) { judge=false; } if(zero && a[i]!=0) judge=false; if(sum==0) zero=true; } if(judge && sum==0) out.println("Yes"); else out.println("No"); } out.close(); } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
f1234f00bd920de2ebfa06345358a830
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; public class CodeForces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while( t -- > 0) { int n = sc.nextInt(); long arr [] = new long[n]; for(int i = 0 ; i< n ; i++) arr[i] = sc.nextLong(); long sum = 0; boolean f = true; for(int i = 0 ; i < n ; i++) { sum += arr[i]; if(sum < 0) { f = false; break; } else if(i < n-1 && sum == 0) { if(remaining(i+1,arr)) f = true; else f = false; break; } } if(sum != 0) f = false; if( f== true) System.out.println("Yes"); else System.out.println("No"); } } private static boolean remaining(int i, long[] arr) { for(int j = i ; j < arr.length ; j++) { if(arr[j] != 0) return false; } return true; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
db3f77a9194c8e79f3756fdefd3e007d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}} public void print(Object object) throws IOException{bw.append(""+ object);} public void println(Object object) throws IOException{print(object);bw.append("\n");} public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{ X first; Y second; public Pair(X first, Y second){ this.first = first; this.second = second; } public String toString(){ return "( " + first+" , "+second+" )"; } @Override public int compareTo(Pair<X, Y> o) { int t = first.compareTo(o.first); if(t == 0) return second.compareTo(o.second); return t; } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { int n = s.nextInt(); long arr[] = s.readLongArr(n); if(arr[0] < -1){ println("NO"); return; } if(arr[0] == 0){ if(allzero(arr, n)){ println("YES"); }else{ println("NO"); } return; } long prev = arr[0]-1; for(int i = 1; i < n-1; i++){ // System.out.println(i); // System.out.println(-1*prev+" "+arr[i]); if(-1*prev > arr[i]){ if(abs((-1*prev) - arr[i]) == 1){ for(int j = i+1; j < n; j++){ if(arr[j] != 0){ println("NO"); return; } } println("YES"); return; }else{ println("NO"); return; } } long op = arr[i] + 1 + prev; prev = op-1; } if(arr[n-1] == (-1*prev)-1){ println("YES"); }else{ println("NO"); } } public static boolean allzero(long arr[], int n){ for(int i = 0; i < n; i++){ if(arr[i] != 0) return false; } return true; } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } out.close(); } } // public static boolean allsame(int arr[]){ // for(int i = 0; i < arr.length-1; i++){ // if(arr[i] != arr[i+1]) return false; // } // return true; // } // // public static List<List<Integer>> permutation(int arr[], int i){ // if(i == 0){ // List<List<Integer>> ans = new ArrayList<>(); // List<Integer> li = new ArrayList<>(); // li.add(arr[i]); // ans.add(li); // return ans; // } // int temp = arr[i]; // List<List<Integer>> rec = permutation(arr, i-1); // List<List<Integer>> ans = new ArrayList<>(); // for(List<Integer> li : rec){ // for(int j = 0; j <= li.size(); j++){ // List<Integer> list = new ArrayList<>(); // for(int ele: li){ // list.add(ele); // } // list.add(j, temp); // ans.add(list); // } // } // return ans; // }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
672fd4be0b847b3573bc8e8013b899ff
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastReader sc=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); public static void main(String args[]) throws IOException { int t=sc.nextInt(); loop: while(t-->0) { int n=sc.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++)arr[i]=sc.nextInt(); long sum=Arrays.stream(arr).sum(); if(sum!=0){ print("NO"); continue; } int j=n-1; while(j>=0&&arr[j]==0)j--; long curr=0; for(int i=0;i<=j;i++){ curr+=arr[i]; if(curr<0){ print("NO"); continue loop; } if(curr==0&&i!=j){ print("NO"); continue loop; } } print("YES"); } } static int max(int a, int b) { if(a<b) return b; return a; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
730473910355822e57f1b671ad17651d
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.io.*; import java.util.*; public class Main { void solveA() { int zero = scanner.nextInt(); int one = scanner.nextInt(); StringBuilder res = new StringBuilder(); while(zero > 0 && one > 0){ if(zero > one){ res.append(0); res.append(1); } else { res.append(1); res.append(0); } zero--; one--; } while(zero > 0){ res.append(0); zero--; } while(one > 0){ res.append(1); one--; } out.println(res.toString()); } void solveB(){ int n = scanner.nextInt(); String str = scanner.nextLine(); long res = n; for(int i = 0; i < n; i++){ if(i > 0 && str.charAt(i-1) != str.charAt(i)){ res += i; } } out.println(res); } void solveC(){ int n = scanner.nextInt(); long[] arr = scanner.nextLongArray(n, 0); long[] b = new long[n]; b[0] = arr[0]; for(int i = 1; i < n; i++){ b[i] = arr[i] + b[i-1]; } int lastIdx = -1; for(int i = 0; i < n; i++){ if(b[i] < 0){ out.println("No"); return; } if(b[i] == 0){ lastIdx = i; break; } } if(lastIdx == -1){ out.println("No"); return; } for(int i = lastIdx + 1; i < n; i++) { if (b[i] != 0) { out.println("No"); return; } } out.println("Yes"); } void solveD(){ int n = scanner.nextInt(); Map<Integer, List<Integer>> g = new HashMap<>(); int[] indegree = new int[n + 1]; for(int i = 1; i <= n; i++){ g.put(i, new ArrayList<>()); } int[] parent = new int[n + 1]; for(int i = 2; i <= n; i++){ int p = scanner.nextInt(); indegree[p]++; g.get(p).add(i); parent[i] = p; } long[][] bound = new long[n+1][2]; for(int i = 1; i <= n; i++){ bound[i][0] = scanner.nextInt(); bound[i][1] = scanner.nextInt(); } long res = 0; Queue<Integer> q = new LinkedList<>(); for(int i = 1; i <= n; i++){ if(indegree[i] == 0){ q.offer(i); res++; } } while(!q.isEmpty()){ int curr = q.poll(); int p = parent[curr]; indegree[p]--; if(indegree[p] == 0){ long l = 0, r = 0; long min = Long.MAX_VALUE; for(int child: g.get(p)){ l += bound[child][0]; r += bound[child][1]; min = Math.min(min, bound[child][0]); } if(r < bound[p][0]){ res++; } else{ bound[p][0] = Math.max(min, bound[p][0]); bound[p][1] = Math.min(r, bound[p][1]); } q.offer(p); } } out.println(res); } private static final boolean memory = true; private static final boolean singleTest = false; // read inputs and call solvers void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solveC(); } out.flush(); out.close(); } public static void main(String[] args) { if(memory) { new Thread(null, new Runnable() { @Override public void run() { new Main().run(); } }, "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static MyScanner scanner = new MyScanner(); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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; } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){ arr[i] = scanner.nextInt(); } return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){ arr[i] = scanner.nextLong(); } return arr; } } //------ debug and print functions ------// void debug(Object...o){ out.println(Arrays.deepToString(o)); } void print(Object...o) { for(int i = 0; i < o.length; i++){ out.print(Arrays.toString(o)); out.print(i == o.length-1? '\n':' '); } } void println(Object...o){ for(int i = 0; i < o.length; i++){ out.println(Arrays.toString(o)); } } void println(Object o){ out.println(o); } //------ sort primitive data types arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
dbc2c5d43352add582c45afa7ce69525
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class DirectionalIncrease { public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(); int[] arr = new int[N]; for (int i = 0; i < N; i++) { arr[i] = in.nextInt(); } long current = 0; boolean valid = true; i: for (int i = 0; i < N; i++) { current += arr[i]; if (current < 0) { valid = false; break; } if (current == 0) { for (int j = i + 1; j < N; j++) { if (arr[j] != 0) { valid = false; break i; } } break; } } valid &= current == 0; out.println(valid ? "YES" : "NO"); } out.close(); } static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } public static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
209ab5d41d2878c801bbe313dda12ddd
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); long mod = 1000000007; int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = readIntArray(n,sc); long sum = 0; for(int i : arr)sum+=i; if(n==1 ) { if(arr[0]==0)writer.println("Yes"); else writer.println("No"); }else { if(arr[0]< 0 || arr[n-1] > 0 || sum != 0)writer.println("No"); else { sum=0; boolean ans = true; boolean lookout = true; for(int i : arr) { sum+=i; if(sum<0)ans=false; if(!lookout && sum>0)ans=false; if(sum==0)lookout=false; } if(ans)writer.println("Yes"); else writer.println("No"); } } } writer.flush(); writer.close(); } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } 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; } } } // implements Comparable<Pair> // //class Pair1Comparator implements Comparator<Pair>{ // public int compare(Pair s1, Pair s2) { // return Integer.compare(s1.a, s2.a); // } //} // //class Pair2Comparator implements Comparator<Pair>{ // public int compare(Pair s1, Pair s2) { // return Integer.compare(s2.a, s1.a); // } //} class Node { int data,sum,total; Node left, right; Node(int data){ this.data = data; this.left = null; this.right = null; this.sum = 0; this.total = 0; } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); // return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } @Override public int compareTo(Pair o) { // if(this.b == o.b) { // return Long.compare(this.a, o.a); // }else { // return Long.compare(o.b, this.b); // } return Long.compare(this.b - this.a, o.b-o.a); } @Override public String toString() { return this.a + " " + this.b; } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output
PASSED
85dda02f63cc1b9bfe5963a9041c502f
train_109.jsonl
1655390100
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.
256 megabytes
import java.util.*; import java.io.*; public class CF_1693A { PrintWriter out; StringTokenizer st; BufferedReader br; final int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE; final int mod = 1000000007; void solve() throws Exception { int t = 1; t = ni(); for (int ii = 0; ii < t; ii++) { int n= ni(); int[] a = ni(n); a= removeZero(a); long reserve = 0l; boolean ans= true; for(int i=a.length-1;i>=0;i--) { reserve+= -a[i]; if(reserve < 0) ans= false; else if(reserve == 0 && i != 0) ans= false; if(i == 0 && reserve != 0) ans = false; } print(ans); } } private int[] removeZero(int[] a) { int index = a.length-1; while(index != -1 && a[index] == 0) index--; int[] ret = new int[index+1]; for (int i = 0; i < ret.length; i++) ret[i] = a[i]; return ret; } public static void main(String[] args) throws Exception { new CF_1693A().run(); } void run() throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { File file = new File("./input.txt"); br = new BufferedReader(new FileReader(file)); out = new PrintWriter("./output.txt"); } else { out = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } long ss = System.currentTimeMillis(); st = new StringTokenizer(""); while (true) { try { solve(); } catch (Exception e) { e.printStackTrace(out); } catch (StackOverflowError e) { e.printStackTrace(out); } String s = br.readLine(); if (s == null) break; else st = new StringTokenizer(s); } //out.println(System.currentTimeMillis()-ss+"ms"); out.flush(); } void read() throws Exception { st = new StringTokenizer(br.readLine()); } int ni() throws Exception { if (!st.hasMoreTokens()) read(); return Integer.parseInt(st.nextToken()); } char nc() throws Exception { if (!st.hasMoreTokens()) read(); return st.nextToken().charAt(0); } String nw() throws Exception { if (!st.hasMoreTokens()) read(); return st.nextToken(); } long nl() throws Exception { if (!st.hasMoreTokens()) read(); return Long.parseLong(st.nextToken()); } int[] ni(int n) throws Exception { int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = ni(); return ret; } long[] nl(int n) throws Exception { long[] ret = new long[n]; for (int i = 0; i < n; i++) ret[i] = nl(); return ret; } double nd() throws Exception { if (!st.hasMoreTokens()) read(); return Double.parseDouble(st.nextToken()); } String ns() throws Exception { String s = br.readLine(); return s.length() == 0 ? br.readLine() : s; } void print(int[] arr) { for (int i : arr) out.print(i + " "); out.println(); } void print(long[] arr) { for (long i : arr) out.print(i + " "); out.println(); } void print(int[][] arr) { for (int[] i : arr) { for (int j : i) out.print(j + " "); out.println(); } } void print(long[][] arr) { for (long[] i : arr) { for (long j : i) out.print(j + " "); out.println(); } } long add(long a, long b) { if (a + b >= mod) return (a + b) - mod; else return a + b >= 0 ? a + b : a + b + mod; } long mul(long a, long b) { return (a * b) % mod; } void print(boolean b) { if (b) out.println("YES"); else out.println("NO"); } long binExp(long base, long power) { long res = 1l; while (power != 0) { if ((power & 1) == 1) res = mul(res, base); base = mul(base, base); power >>= 1; } return res; } long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } // strictly smaller on left void stack_l(int[] arr, int[] left) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < arr.length; i++) { while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop(); if (stack.isEmpty()) left[i] = -1; else left[i] = stack.peek(); stack.push(i); } } // strictly smaller on right void stack_r(int[] arr, int[] right) { Stack<Integer> stack = new Stack<>(); for (int i = arr.length - 1; i >= 0; i--) { while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop(); if (stack.isEmpty()) right[i] = arr.length; else right[i] = stack.peek(); stack.push(i); } } private void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) list.add(i); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } }
Java
["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"]
1 second
["No\nYes\nNo\nNo\nYes\nYes\nYes"]
NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$$$
Java 11
standard input
[ "greedy" ]
9f0ffcbd0ce62a365a1ecbb4a2c1de3e
The first line contains a single integer $$$t$$$ $$$(1\le t\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
standard output