exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
sequencelengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
fa43d405db72ba1af6bcca38c66de5da
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author real */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DReverseSortSum { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int c[] = in.getArray(n); int ar[] = new int[2 * n + 1]; for (int i = 0; i < n; i++) { ar[i] = 1; } for (int i = 0; i < n; i++) { if (c[i] == 0) { ar[i] = 0; continue; } int val = i * ar[i]; int pos = c[i] - val; pos = pos + i; if (pos < 0) { continue; } ar[pos] = 0; } for (int i = 0; i < n; i++) { out.print(ar[i] + " "); } out.println(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare-----anjlika--- //remeber while comparing 2 non primitive data type not to use == //remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort //again silly mistakes ,yr kb tk krta rhega ye mistakes //try to write simple codes ,break it into simple things //knowledge>rating /* public class Main implements Runnable{ public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<26).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; libraries.InputReader in = new libraries.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC();//chenge the name of task solver.solve(1, in, out); out.close(); } */ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] getArray(int n) { int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = nextInt(); } return ar; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String next() { return readString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 8
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
666e3ece9e479faaf44dc9a5956edb35
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = true; AdityaFastIO r = new AdityaFastIO(); //FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { int n = r.ni(); List<Long> al = readLongList(n, r); long[] dp = new long[n]; Arrays.fill(dp, 1); for (int i = 0; i < n; i++) { if (dp[i] == 0) al.set(i, al.get(i) + i); if (al.get(i) != n) { if (al.get(i) > i) dp[Math.toIntExact(al.get(i))] = 0; else dp[i] = 0; } } for (long ele : dp) out.write((ele + " ").getBytes()); out.write(("\n").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // 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 ni() 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 nl() 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 nd() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws Exception { run(); } static int[] readIntArr(int n, AdityaFastIO r) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = r.ni(); return arr; } static long[] readLongArr(int n, AdityaFastIO r) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = r.nl(); return arr; } static List<Integer> readIntList(int n, AdityaFastIO r) throws IOException { List<Integer> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.ni()); return al; } static List<Long> readLongList(int n, AdityaFastIO r) throws IOException { List<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.nl()); return al; } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } static int upper_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 8
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
4bc9d048fd251bbe54cd0bae2c59954e
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class D { FastScanner in; PrintWriter out; boolean systemIO = true; int mod = 998244353; public int sum(int x, int y) { if (x + y >= mod) { return x + y - mod; } return x + y; } public int diff(int x, int y) { if (x >= y) { return x - y; } return x - y + mod; } public int mult(int x, int y) { return (int) (x * 1L * y % mod); } public int pow(int x, long p) { int ans = 1; while (p > 0) { if ((p & 1) == 1) { ans = mult(ans, x); } x = mult(x, x); p >>= 1; } return ans; } public int inv(int x) { return pow(x, mod - 2); } public int div(int x, int y) { return mult(x, inv(y)); } public class DSU { int[] sz; int[] p; public DSU(int n) { sz = new int[n]; p = new int[n]; for (int i = 0; i < p.length; i++) { p[i] = i; sz[i] = 1; } } public int get(int x) { if (x == p[x]) { return x; } int par = get(p[x]); p[x] = par; return par; } public boolean unite(int a, int b) { int pa = get(a); int pb = get(b); if (pa == pb) { return false; } if (sz[pa] < sz[pb]) { p[pa] = pb; sz[pb] += sz[pa]; } else { p[pb] = pa; sz[pa] += sz[pb]; } return true; } } public class SegmentTreeAdd { int pow; long[] max; long[] delta; boolean[] flag; public SegmentTreeAdd(long[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; max = new long[2 * pow]; delta = new long[2 * pow]; for (int i = 0; i < max.length; i++) { max[i] = Long.MIN_VALUE / 2; } for (int i = 0; i < a.length; i++) { max[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { max[i] = f(max[2 * i], max[2 * i + 1]); } } public long get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l > r) { return Long.MIN_VALUE / 2; } if (l == tl && r == tr) { return max[v]; } int tm = (tl + tr) / 2; return f(get(2 * v, tl, tm, l, Math.min(r, tm)), get(2 * v + 1, tm + 1, tr, Math.max(l, tm + 1), r)); } public void set(int v, int tl, int tr, int l, int r, long x) { push(v, tl, tr); if (l > tr || r < tl) { return; } if (l <= tl && r >= tr) { delta[v] += x; flag[v] = true; push(v, tl, tr); return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm + 1, tr, l, r, x); max[v] = f(max[2 * v], max[2 * v + 1]); } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] += delta[v]; delta[2 * v + 1] += delta[v]; } flag[v] = false; max[v] += delta[v]; delta[v] = 0; } } public long f(long a, long b) { return Math.max(a, b); } } public class SegmentTreeSet { int pow; int[] sum; int[] delta; boolean[] flag; public SegmentTreeSet(int[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; sum = new int[2 * pow]; delta = new int[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { sum[i] = f(sum[2 * i], sum[2 * i + 1]); } } public int get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l > r) { return 0; } if (l == tl && r == tr) { return sum[v]; } int tm = (tl + tr) / 2; return f(get(2 * v, tl, tm, l, Math.min(r, tm)), get(2 * v + 1, tm + 1, tr, Math.max(l, tm + 1), r)); } public void set(int v, int tl, int tr, int l, int r, int x) { push(v, tl, tr); if (l > tr || r < tl) { return; } if (l <= tl && r >= tr) { delta[v] = x; flag[v] = true; push(v, tl, tr); return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm + 1, tr, l, r, x); sum[v] = f(sum[2 * v], sum[2 * v + 1]); } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] = delta[v]; delta[2 * v + 1] = delta[v]; } flag[v] = false; sum[v] = delta[v] * (tr - tl + 1); } } public int f(int a, int b) { return a + b; } } public class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public Pair clone() { return new Pair(x, y); } public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { if (x > o.x) { return 1; } if (x < o.x) { return -1; } if (y > o.y) { return 1; } if (y < o.y) { return -1; } return 0; } } Random random = new Random(); public void shuffle(Pair[] a) { for (int i = 0; i < a.length; i++) { int x = random.nextInt(i + 1); Pair t = a[x]; a[x] = a[i]; a[i] = t; } } public void sort(int[][] a) { for (int i = 0; i < a.length; i++) { Arrays.sort(a[i]); } } public void add(Map<Long, Integer> map, long l) { if (map.containsKey(l)) { map.put(l, map.get(l) + 1); } else { map.put(l, 1); } } public void remove(Map<Integer, Integer> map, Integer s) { if (map.get(s) > 1) { map.put(s, map.get(s) - 1); } else { map.remove(s); } } long max = Long.MAX_VALUE / 2; double eps = 1e-10; public int signum(double x) { if (x > eps) { return 1; } if (x < -eps) { return -1; } return 0; } public long abs(long x) { return x < 0 ? -x : x; } public long min(long x, long y) { return x < y ? x : y; } public long max(long x, long y) { return x > y ? x : y; } public long gcd(long x, long y) { while (y > 0) { long c = y; y = x % y; x = c; } return x; } public final Vector ZERO = new Vector(0, 0); // public class Vector implements Comparable<Vector> { // long x; // long y; // int type; // int number; // // public Vector() { // x = 0; // y = 0; // } // // public Vector(long x, long y, int type, int number) { // this.x = x; // this.y = y; // this.type = type; // this.number = number; // } // // public Vector(long x, long y) { // // } // // public Vector(Point begin, Point end) { // this(end.x - begin.x, end.y - begin.y); // } // // public void orient() { // if (x < 0) { // x = -x; // y = -y; // } // if (x == 0 && y < 0) { // y = -y; // } // } // // public void normalize() { // long gcd = gcd(abs(x), abs(y)); // x /= gcd; // y /= gcd; // } // // public String toString() { // return x + " " + y; // } // // public boolean equals(Vector v) { // return x == v.x && y == v.y; // } // // public boolean collinear(Vector v) { // return cp(this, v) == 0; // } // // public boolean orthogonal(Vector v) { // return dp(this, v) == 0; // } // // public Vector ort(Vector v) { // return new Vector(-y, x); // } // // public Vector add(Vector v) { // return new Vector(x + v.x, y + v.y); // } // // public Vector multiply(long c) { // return new Vector(c * x, c * y); // } // // public int quater() { // if (x > 0 && y >= 0) { // return 1; // } // if (x <= 0 && y > 0) { // return 2; // } // if (x < 0) { // return 3; // } // return 0; // } // // public long len2() { // return x * x + y * y; // } // // @Override // public int compareTo(Vector o) { // if (quater() != o.quater()) { // return quater() - o.quater(); // } // return signum(cp(o, this)); // } // } // public long dp(Vector v1, Vector v2) { // return v1.x * v2.x + v1.y * v2.y; // } // // public long cp(Vector v1, Vector v2) { // return v1.x * v2.y - v1.y * v2.x; // } // public class Line implements Comparable<Line> { // Point p; // Vector v; // // public Line(Point p, Vector v) { // this.p = p; // this.v = v; // } // // public Line(Point p1, Point p2) { // if (p1.compareTo(p2) < 0) { // p = p1; // v = new Vector(p1, p2); // } else { // p = p2; // v = new Vector(); // } // } // // public boolean collinear(Line l) { // return v.collinear(l.v); // } // // public boolean inLine(Point p) { // return hv(p) == 0; // } // // public boolean inSegment(Point p) { // if (!inLine(p)) { // return false; // } // Point p1 = p; // Point p2 = p.add(v); // return p1.x <= p.x && p.x <= p2.x && min(p1.y, p2.y) <= p.y && p.y <= // max(p1.y, p2.y); // } // // public boolean equalsSegment(Line l) { // return p.equals(l.p) && v.equals(l.v); // } // // public boolean equalsLine(Line l) { // return collinear(l) && inLine(l.p); // } // // public long hv(Point p) { // Vector v1 = new Vector(this.p, p); // return cp(v, v1); // } // // public double h(Point p) { // Vector v1 = new Vector(this.p, p); // return cp(v, v1) / Math.sqrt(v.len2()); // } // // public long[] intersectLines(Line l) { // if (collinear(l)) { // return null; // } // long[] ans = new long[4]; // // return ans; // } // // public long[] intersectSegment(Line l) { // long[] ans = intersectLines(l); // if (ans == null) { // return null; // } // Point p1 = p; // Point p2 = p.add(v); // boolean f1 = p1.x * ans[1] <= ans[0] && ans[0] <= p2.x * ans[1] && min(p1.y, // p2.y) * ans[3] <= ans[2] // && ans[2] <= max(p1.y, p2.y) * ans[3]; // p1 = l.p; // p2 = l.p.add(v); // boolean f2 = p1.x * ans[1] <= ans[0] && ans[0] <= p2.x * ans[1] && min(p1.y, // p2.y) * ans[3] <= ans[2] // && ans[2] <= max(p1.y, p2.y) * ans[3]; // if (!f1 || !f2) { // return null; // } // return ans; // } // // @Override // public int compareTo(Line o) { // return v.compareTo(o.v); // } // } public class Rect { long x1; long x2; long y1; long y2; int number; public Rect(long x1, long x2, long y1, long y2, int number) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.number = number; } } public static class Fenvik { int[] t; public Fenvik(int n) { t = new int[n]; } public void add(int x, int delta) { for (int i = x; i < t.length; i = (i | (i + 1))) { t[i] += delta; } } private int sum(int r) { int ans = 0; int x = r; while (x >= 0) { ans += t[x]; x = (x & (x + 1)) - 1; } return ans; } public int sum(int l, int r) { return sum(r) - sum(l - 1); } } public class SegmentTreeMaxSum { int pow; int[] sum; int[] maxPrefSum; int[] maxSufSum; int[] maxSum; public SegmentTreeMaxSum(int[] a) { pow = 1; while (pow < a.length) { pow *= 2; } sum = new int[2 * pow]; maxPrefSum = new int[2 * pow]; maxSum = new int[2 * pow]; maxSufSum = new int[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; maxSum[pow + i] = Math.max(a[i], 0); maxPrefSum[pow + i] = maxSum[pow + i]; maxSufSum[pow + i] = maxSum[pow + i]; } for (int i = pow - 1; i > 0; i--) { update(i); } } public int[] get(int v, int tl, int tr, int l, int r) { if (r <= tl || l >= tr) { int[] ans = { 0, 0, 0, 0 }; return ans; } if (l <= tl && r >= tr) { int[] ans = { maxPrefSum[v], maxSum[v], maxSufSum[v], sum[v] }; return ans; } int tm = (tl + tr) / 2; int[] left = get(2 * v, tl, tm, l, r); int[] right = get(2 * v + 1, tm, tr, l, r); int[] ans = { Math.max(left[0], right[0] + left[3]), Math.max(left[1], Math.max(right[1], left[2] + right[0])), Math.max(right[2], left[2] + right[3]), left[3] + right[3] }; return ans; } public void set(int v, int tl, int tr, int x, int value) { if (v >= pow) { sum[v] = value; maxSum[v] = Math.max(value, 0); maxPrefSum[v] = maxSum[v]; maxSufSum[v] = maxSum[v]; return; } int tm = (tl + tr) / 2; if (x < tm) { set(2 * v, tl, tm, x, value); } else { set(2 * v + 1, tm, tr, x, value); } update(v); } public void update(int i) { sum[i] = f(sum[2 * i], sum[2 * i + 1]); maxSum[i] = Math.max(maxSum[2 * i], Math.max(maxSum[2 * i + 1], maxSufSum[2 * i] + maxPrefSum[2 * i + 1])); maxPrefSum[i] = Math.max(maxPrefSum[2 * i], maxPrefSum[2 * i + 1] + sum[2 * i]); maxSufSum[i] = Math.max(maxSufSum[2 * i + 1], maxSufSum[2 * i] + sum[2 * i + 1]); } public int f(int a, int b) { return a + b; } } public class Point implements Comparable<Point> { double x; double y; public Point() { x = 0; y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Point p) { return x == p.x && y == p.y; } public double dist2() { return x * x + y * y; } public Point add(Point v) { return new Point(x + v.x, y + v.y); } @Override public int compareTo(Point o) { int z = signum(x + y - o.x - o.y); if (z != 0) { return z; } return signum(x - o.x) != 0 ? signum(x - o.x) : signum(y - o.y); } } public class Circle implements Comparable<Circle> { Point p; int r; public Circle(Point p, int r) { this.p = p; this.r = r; } public Point angle() { double z = r / sq2; z -= z % 1e-5; return new Point(p.x - z, p.y - z); } public boolean inside(Point p) { return hypot2(p.x - this.p.x, p.y - this.p.y) <= sq(r); } @Override public int compareTo(Circle o) { Point a = angle(); Point oa = o.angle(); int z = signum(a.x + a.y - oa.x - oa.y); if (z != 0) { return z; } return signum(a.y - oa.y); } } public class Fraction implements Comparable<Fraction> { long x; long y; public Fraction(long x, long y, boolean needNorm) { this.x = x; this.y = y; if (y < 0) { this.x *= -1; this.y *= -1; } if (needNorm) { long gcd = gcd(this.x, this.y); this.x /= gcd; this.y /= gcd; } } public Fraction clone() { return new Fraction(x, y, false); } public String toString() { return x + "/" + y; } @Override public int compareTo(Fraction o) { long res = x * o.y - y * o.x; if (res > 0) { return 1; } if (res < 0) { return -1; } return 0; } } public double sq(double x) { return x * x; } public long sq(long x) { return x * x; } public double hypot2(double x, double y) { return sq(x) + sq(y); } public long hypot2(long x, long y) { return sq(x) + sq(y); } public boolean kuhn(int v, int[][] edge, boolean[] used, int[] mt) { used[v] = true; for (int u : edge[v]) { if (mt[u] < 0 || (!used[mt[u]] && kuhn(mt[u], edge, used, mt))) { mt[u] = v; return true; } } return false; } public int matching(int[][] edge) { int n = edge.length; int[] mt = new int[n]; Arrays.fill(mt, -1); boolean[] used = new boolean[n]; int ans = 0; for (int i = 0; i < n; i++) { if (!used[i] && kuhn(i, edge, used, mt)) { Arrays.fill(used, false); ans++; } } return ans; } double sq2 = Math.sqrt(2); int small = 20; public class MyStack { int[] st; int sz; public MyStack(int n) { this.st = new int[n]; sz = 0; } public boolean isEmpty() { return sz == 0; } public int peek() { return st[sz - 1]; } public int pop() { sz--; return st[sz]; } public void clear() { sz = 0; } public void add(int x) { st[sz++] = x; } public int get(int x) { return st[x]; } } public int[][] readGraph(int n, int m) { int[][] to = new int[n][]; int[] sz = new int[n]; int[] x = new int[m]; int[] y = new int[m]; for (int i = 0; i < m; i++) { x[i] = in.nextInt() - 1; y[i] = in.nextInt() - 1; sz[x[i]]++; sz[y[i]]++; } for (int i = 0; i < to.length; i++) { to[i] = new int[sz[i]]; sz[i] = 0; } for (int i = 0; i < x.length; i++) { to[x[i]][sz[x[i]]++] = y[i]; to[y[i]][sz[y[i]]++] = x[i]; } return to; } public class Pol { double[] coeff; public Pol(double[] coeff) { this.coeff = coeff; } public Pol mult(Pol p) { double[] ans = new double[coeff.length + p.coeff.length - 1]; for (int i = 0; i < ans.length; i++) { for (int j = Math.max(0, i - p.coeff.length + 1); j < coeff.length && j <= i; j++) { ans[i] += coeff[j] * p.coeff[i - j]; } } return new Pol(ans); } public String toString() { String ans = ""; for (int i = 0; i < coeff.length; i++) { ans += coeff[i] + " "; } return ans; } public double value(double x) { double ans = 0; double p = 1; for (int i = 0; i < coeff.length; i++) { ans += coeff[i] * p; p *= x; } return ans; } public double integrate(double r) { Pol p = new Pol(new double[coeff.length + 1]); for (int i = 0; i < coeff.length; i++) { p.coeff[i + 1] = coeff[i] / fact[i + 1]; } return p.value(r); } public double integrate(double l, double r) { return integrate(r) - integrate(l); } } double[] fact = new double[100]; public class SparseTable { int pow; int[] lessPow; int[][] min; public SparseTable(int[] a) { pow = 0; while ((1 << pow) <= a.length) { pow++; } min = new int[pow][a.length]; for (int i = 0; i < a.length; i++) { min[0][i] = a[i]; } for (int i = 1; i < pow; i++) { for (int j = 0; j < a.length; j++) { min[i][j] = min[i - 1][j]; if (j + (1 << (i - 1)) < a.length) { min[i][j] = func(min[i][j], min[i - 1][j + (1 << (i - 1))]); } } } lessPow = new int[a.length + 1]; for (int i = 1; i < lessPow.length; i++) { if (i < (1 << (lessPow[i - 1]) + 1)) { lessPow[i] = lessPow[i - 1]; } else { lessPow[i] = lessPow[i - 1] + 1; } } } public int get(int l, int r) { // [l, r) int p = lessPow[r - l]; return func(min[p][l], min[p][r - (1 << p)]); } public int func(int a, int b) { if (a < b) { return a; } return b; } } public double check(int n, ArrayList<Integer> masks) { int good = 0; for (int colorMask = 0; colorMask < (1 << n); ++colorMask) { int best = 2 << n; int cnt = 0; for (int curMask : masks) { int curScore = 0; for (int i = 0; i < n; ++i) { if (((curMask >> i) & 1) == 1) { if (((colorMask >> i) & 1) == 0) { curScore += 1; } else { curScore += 2; } } } if (curScore < best) { best = curScore; cnt = 1; } else if (curScore == best) { ++cnt; } } if (cnt == 1) { ++good; } } return (double) good / (double) (1 << n); } public int builtin_popcount(int x) { int ans = 0; for (int i = 0; i < 14; i++) { if (((x >> i) & 1) > 0) { ans++; } } return ans; } public int number(int[] x) { int ans = 0; for (int i = 0; i < x.length; i++) { ans *= 3; ans += x[i]; } return ans; } public int[] rotate(int[] x) { int[] ans = { x[2], x[0], x[3], x[1] }; return ans; } int MAX = 200001; boolean[] b = new boolean[MAX]; int[][] max0 = new int[MAX][2]; int[][] max1 = new int[MAX][2]; int[][] max2 = new int[MAX][2]; int[] index0 = new int[MAX]; int[] index1 = new int[MAX]; int[] p = new int[MAX]; public int place(String s) { if (s.charAt(s.length() - 1) == '1') { return 1; } int number = 16; boolean w = true; boolean a = true; for (int i = 0; i < s.length(); i++) { if (number == 1) { return 2; } if (s.charAt(i) == '1') { if (w) { number /= 2; } else { if (a) { a = false; } else { number /= 2; a = true; } } } else { if (w) { if (number == 16) { w = false; number /= 2; } else { w = false; a = false; } } else { if (number == 8) { if (a) { return 13; } else { return 9; } } else if (number == 4) { if (a) { return 7; } else { return 5; } } else if (a) { return 4; } else { return 3; } } } } return 0; } public class P implements Comparable<P> { Integer x; String s; public P(Integer x, String s) { this.x = x; this.s = s; } @Override public String toString() { return x + " " + s; } @Override public int compareTo(P o) { if (x != o.x) { return x - o.x; } return s.compareTo(o.s); } } public BigInteger prod(int l, int r) { if (l + 1 == r) { return BigInteger.valueOf(l); } int m = (l + r) >> 1; return prod(l, m).multiply(prod(m, r)); } public class Frac { BigInteger p; BigInteger q; public Frac(BigInteger p, BigInteger q) { BigInteger gcd = p.gcd(q); this.p = p.divide(gcd); this.q = q.divide(gcd); } public String toString() { return p + "\n" + q; } public Frac(long p, long q) { this(BigInteger.valueOf(p), BigInteger.valueOf(q)); } public Frac mul(Frac o) { return new Frac(p.multiply(o.p), q.multiply(o.q)); } public Frac sum(Frac o) { return new Frac(p.multiply(o.q).add(q.multiply(o.p)), q.multiply(o.q)); } public Frac diff(Frac o) { return new Frac(p.multiply(o.q).subtract(q.multiply(o.p)), q.multiply(o.q)); } } public int[] transform(int[] x, int k, int step) { int n = x.length; int[] a = new int[n]; int[][] prefsum = new int[n][]; int[] elements = new int[n]; int[] start = new int[n]; int[] id = new int[n]; for (int i = 0; i < n; i++) { if (elements[i] > 0) { continue; } int cur = i; do { elements[i]++; cur += step; cur %= n; } while (cur != i); for (int j = 0; j < elements[i]; j++) { start[cur] = i; id[cur] = j; elements[cur] = elements[i]; cur += step; cur %= n; } prefsum[i] = new int[3 * elements[i] + 1]; for (int j = 0; j < 3 * elements[i]; j++) { prefsum[i][j + 1] = prefsum[i][j] ^ x[cur]; cur += step; cur %= n; } } for (int i = 0; i < x.length; i++) { int curlen = k % (2 * elements[i]); a[i] = prefsum[start[i]][id[i] + curlen] ^ prefsum[start[i]][id[i]]; } return a; } public boolean rated(String s, int x) { if (s.toUpperCase().equals("ABC")) { return x <= 1999; } if (s.toUpperCase().equals("ARC")) { return x <= 2799; } if (s.toUpperCase().equals("AGC")) { return x >= 1200; } return false; } public void solve() { for (int qwerty = in.nextInt(); qwerty > 0; --qwerty) { int n = in.nextInt(); int[] c = new int[n]; boolean[] zero = new boolean[n]; for (int i = 0; i < c.length; i++) { c[i] = in.nextInt(); } ArrayList<Integer> zeroes = new ArrayList<>(); int last = 0; while (last < n && c[last] == 0) { zeroes.add(last); zero[last] = true; last++; } for (int i = last; i < zero.length; i++) { if (!zero[i]) { c[i] -= i; } zeroes.add(i + c[i]); if (i + c[i] < n) { zero[i + c[i]] = true; } } for (int i = 0; i < zero.length; i++) { if (zero[i]) { out.print(0 + " "); } else { out.print(1 + " "); } } out.println(); } } public void add(HashMap<Integer, Integer> map, int x) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new D().run(); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 8
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
d86e11c85bdb0f33c312984230c9231f
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws NumberFormatException, IOException { MyReader reader = new MyReader(); int t = reader.nextInt(); for(int testcase=0; testcase<t; testcase++) { int n = reader.nextInt(); int[] c = new int[n]; for(int i=0; i<n; i++) { c[i] = reader.nextInt(); } long sum = 0; for(int i=0; i<n; i++) { sum += c[i]; } int num1s = (int) (sum/n); DS ds = new DS(n); int[] a = new int[n]; Arrays.fill(a, -1); for(int i=n-1; i>=1; i--) { //subtract b[i] from c[i] ds.update(i-num1s+1, i); //System.out.println("subtract " + (i-num1s+1) + " " + i); //System.out.println(i + " " + getC(i,c,ds)); //figure out a[i] if(getC(i,c,ds) == i) { //a[i] = 1 a[i] = 1; num1s--; } else if(getC(i,c,ds) == 0) { a[i] = 0; } else System.out.println("error"); } //i = 0 if(num1s == 1) a[0] = 1; else a[0] = 0; PrintWriter pw = new PrintWriter(System.out); for(int i=0; i<n; i++) { pw.print(a[i] + " "); } pw.println(); pw.flush(); } } public static int getC(int i, int[] c, DS ds) { return c[i] - ds.query(i); } static class MyReader { BufferedReader br; StringTokenizer st; public MyReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } static class DS { SegmentTree st; public DS(int size) { st = new SegmentTree(new int[size+1]); } public void update(int l, int r) { st.update(l, 1); st.update(r+1, -1); } public int query(int i) { return st.query(0, i); } } public static class SegmentTree { int[] t; int size; public SegmentTree(int[] arr) { t = new int[4*arr.length]; size = arr.length; build(1, 0, arr.length-1, arr); } public int query(int l, int r) { return query(1, 0, size-1, l, r); } public void update(int index, int val) { update(1, 0, size-1, index, val); } private int query(int index, int L, int R, int l, int r) { if(l <= L && R <= r) return t[index]; int M = (L + R)/2; if(M+1 > r) return query(2*index, L, M, l, r); if(M < l) return query(2*index+1, M+1, R, l, r); return merge(query(2*index, L, M, l, r), query(2*index+1, M + 1, R, l, r)); } private void update(int index, int L, int R, int inputIndex, int val) { if(inputIndex < L || R < inputIndex) return; if(L == R) t[index] += val; else { int M = (L + R)/2; update(2*index, L, M, inputIndex, val); update(2*index+1, M+1, R, inputIndex, val); t[index] = merge(t[2*index], t[2*index+1]); } } private void build(int index, int L, int R, int[] arr) { if(L == R) t[index] = arr[L]; else { int M = (L+R)/2; build(2*index, L, M, arr); build(2*index+1, M+1, R, arr); t[index] = merge(t[2*index], t[2*index+1]); } } private int merge(int a, int b) { return a + b; } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 8
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
22533fbf47a819ffc0cb867b6eecd0eb
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
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; /* 2 4 2 4 1 1 0 + + + + + 0 1 1 3 5 1110 +++ +++ +++ 0111 Swap in a 0 or a 1, 0 -> stay the same forever, 1-> continue to get smaller 3 => for 3 turns, subtract 1 from the next 3 last element: 0 or 1 if initially a 0 else n We can count the number of 1s in total: #1s = sum/n last is a 1, 3 1s in total 2 4 2 4 + + + last is ? 2 1s in total 2 3 1 This is what it looked like before 1 4 2 4 2 4 */ public class D { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); BIT deltas=new BIT(n); int[] a=fs.readArray(n); long sum=0; for (int i:a) sum+=i; if (sum%n!=0) throw null; int nOnes=(int) (sum/n); for (int i=0; i<n; i++) { int prev=i==0?0:a[i-1]; int delta=a[i]-prev; deltas.update(i, delta); } int[] ans=new int[n]; for (int i=n-1; i>0; i--) { int last=deltas.query(0, i); //remove the last query int nZeros=i+1-nOnes; deltas.update(nZeros, -1); if (last==i+1) { //ends in a 1 ans[i]=1; nOnes--; } else { ans[i]=0; } } ans[0]=nOnes; for (int i=0; i<n; i++) out.print(ans[i]+" "); out.println(); } out.close(); } //range update point query //bit stores delta static class BIT { int n, tree[]; public BIT(int N) { n = N; tree = new int[N + 1]; } void update(int i, int val) { for (i++; i <= n; i += i & -i) tree[i] += val; } int read(int i) { int sum = 0; for (i++; i > 0; i -= i & -i) sum += tree[i]; return sum; } // query sum of [l, r] inclusive int query(int l, int r) { return read(r) - read(l - 1); } // if the BIT is a freq array, returns the index of the // kth item (0-indexed), or n if there are <= k items. int getKth(int k) { if (k < 0) return -1; int i = 0; for (int pw = Integer.highestOneBit(n); pw > 0; pw >>= 1) if (i + pw <= n && tree[i + pw] <= k) k -= tree[i += pw]; return i; } } 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 8
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
f8f6e404cfc51f0b1cf996ab8e4bf30a
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class D { public static void main(String[] args) { //creates a scanner and reads the first line of the input Scanner sc = new Scanner(System.in); int testCount = Integer.parseInt(sc.nextLine()); for (int i = 0; i < testCount; i++) { int n = sc.nextInt(); sc.nextLine(); int[] a = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int[] sol = new int[a.length]; long c = 0; for(int j = 0; j < a.length; j++)c += a[j]; c/=n; int l = a.length; int last = -1; for (int j = 0; j < c-1; j++) { a[l-j-1] -= j; } for (int j = a.length -1; j >= 0; j--) { int curr = a[j]; if(a[j] == 0) { break; } if(j > 0 && (j-((int)c-1)) >= 0) { // System.out.println(curr + " " + a[j]); if(j-((int)c-1) != last) { a[j-((int)c-1)] -= c-1; last = j-((int)c-1); } } if(curr == l) { sol[j] = 1; c--; }else if(curr == 1) { sol[j] = 0; } // a[j] = 0; l--; // for (int k = 0; k < sol.length; k++) { // System.out.print(a[k] + " "); // } // System.out.println(); } for (int j = 0; j < sol.length; j++) { System.out.print(sol[j] + " "); } System.out.println(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
6bb383ed8b87aedc7cbde568d131ad5b
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; public class E1659D { public static void main(String[] args) { FastIO io = new FastIO(); int t = io.nextInt(); while (t-- > 0) { int n = io.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = io.nextInt(); int[] ans = new int[n]; Arrays.fill(ans, 1); for (int i = 0; i < n; i++) { int j = arr[i]; if (j == 0) ans[i] = 0; if (ans[i] == 0) j += i; if (j < n) ans[j] = 0; } for (int v : ans) io.print(v + " "); io.println(); } io.close(); } private 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 int nextInt() { // nextLong() would be implemented similarly 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() { // nextLong() would be implemented similarly 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
8709539b3e77f558c1623e4186d54cdb
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; public class E1659D { public static void main(String[] args) { FastIO io = new FastIO(); int t = io.nextInt(); while (t-- > 0) { int n = io.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = io.nextInt(); int[] ans = new int[n]; Arrays.fill(ans, 1); for (int i = 0; i < n; i++) { int j = arr[i]; if (j == 0 || ans[i] == 0) j += i; if (j < n) ans[j] = 0; } for (int v : ans) io.print(v + " "); io.println(); } io.close(); } private 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 int nextInt() { // nextLong() would be implemented similarly 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() { // nextLong() would be implemented similarly 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
df4fed6849b92e147e7745f8d5342b60
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, k; static long sum; static int[] a, res; static int[] BIT; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); a = new int[n+1]; sum = 0; for (int i = 1; i <= n; i++) { a[i] = in.iscan(); sum += a[i]; } k = (int)(sum / n); res = new int[n+1]; BIT = new int[n+1]; int to = n-k+1; for (int i = n; i >= to; i--) { if (a[i] == n) { res[i] = 1; k--; update(Math.max(1, i-k), 1); } else { res[i] = 0; } } for (int i = to-1; i >= 1; i--) { int sub = query(i); a[i] -= sub; if (k - 1 + i == a[i] && k > 0) { res[i] = 1; k--; update(Math.max(1, i-k), 1); } else { // k == a[i] res[i] = 0; } } for (int i = 1; i <= n; i++) { out.print(res[i] + " "); } out.println(); } out.close(); } static void update(int x, int v) { for (int i = x; i <= n; i += i&-i) { BIT[i] += v; } } static int query(int x) { int ret = 0; for (int i = x; i > 0; i -= i&-i) { ret += BIT[i]; } return ret; } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
445c901294b601fdaaf6c7b979234ebb
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } static class DReverseSortSum { int n; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int[] res = new int[n + 1]; Arrays.fill(res, 1); int idx = -1; while (idx < n - 1 && a[++idx] == 0) { res[idx] = 0; } for (; idx < n; idx++) { int k = a[idx] - idx * res[idx]; res[k + idx] = 0; } for (int i = 0; i < n; i++) { out.print(res[i], ""); } out.println(); } } 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 close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
f3f95b9d1e3d5f437277be0601bdf325
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.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); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } static class DReverseSortSum { int N = 200005; int[] t = new int[N]; int n; int lowbit(int x) { return x & -x; } void add(int x, int c) { for (int i = x; i <= n; i += lowbit(i)) { t[i] += c; } } int sum(int x) { int res = 0; for (int i = x; i > 0; i -= lowbit(i)) { res += t[i]; } return res; } public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); Arrays.fill(t, 1, n + 5, 0); int[] a = new int[n + 1]; long sum = 0; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); sum += a[i]; } int[] res = new int[n]; int k = (int) (sum / n); for (int i = n; i > 0; i--) { if (a[i] + sum(i) == i) { res[i - 1] = 1; } else { res[i - 1] = 0; } add(i - k + 1, -1); if (res[i - 1] == 1) { k--; } } out.println(res); } } 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(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
5240b48b85c3da8e5c6722b447abb377
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { int m; Scanner read = new Scanner(System.in); m = read.nextInt(); for (int i = 0; i < m; i++) { long n = read.nextInt(); long arr[] = new long[(int)n]; long arr2[] = new long[(int)n]; long sum = 0 ; for (int j = 0; j < n ; j++){ arr[j]=read.nextLong(); sum+=arr[j]; } long st=0; if (n!=1) { sum=sum*(n-1)/n; st=n-sum/(n-1); } long p = 0 ; int cur = (int) (st-1); int j = (int) (n-1); while (sum!=0 && j>=0){ long del =n-1; if (j<st){ del = arr2[j]; } arr[j]=arr[j]-del; if (arr[j]<=0){ arr2[cur]=-1*arr[j]; cur--; arr[j]=0; } j--; } for ( j = 0; j < n ; j++) System.out.print(arr[j]+" "); System.out.println(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
7f69e1b138429b48ae090aec146ce9e3
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; public class ReverseSortSum { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while (t > 0) { t--; int n = Integer.parseInt(br.readLine()); int[] nums = new int[n]; String[] data = br.readLine().split(" "); long total = 0; for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(data[i]); total += nums[i]; } int numOnes = (int)(total/n); boolean[] isAvailable = new boolean[n]; for (int i = n-numOnes-1; i >= 0; i--) { isAvailable[i] = true; } int nextPotAvail = n-numOnes-1; PriorityQueue<Tuple> q = new PriorityQueue<>(new reverseComparator()); for (int i = 0; i < numOnes; i++) { q.add(new Tuple(n - (nums[n-i-1]), n-i-1)); } if (numOnes == 0) { sb.append(0); for (int i = 1; i < n; i++) sb.append(" 0"); sb.append(System.lineSeparator()); //System.err.println("end of case " + t); } else { Tuple cur = q.poll(); //System.err.println("cur = " + cur); while (cur.a > 0) { while (!isAvailable[nextPotAvail--]) {} isAvailable[nextPotAvail+1] = false; // add nextPotAvail+1 to q q.add(new Tuple(cur.a-nums[nextPotAvail+1], nextPotAvail+1)); cur = q.poll(); //System.err.println("cur = " + cur); } boolean[] finalArr = new boolean[n]; finalArr[cur.b] = true; // A set one at turns ending at 0 while (!q.isEmpty()) { cur = q.poll(); //System.err.println("cur = " + cur); finalArr[cur.b] = true; } sb.append(finalArr[0] ? '1' : '0'); for (int i = 1; i < n; i++) { sb.append(' ').append(finalArr[i] ? '1' : '0'); } sb.append(System.lineSeparator()); //System.err.println("end of case " + t); } } System.out.print(sb); } } class reverseComparator implements Comparator<Tuple> { public int compare(Tuple x, Tuple y) { return y.a-x.a; } } class Tuple { int a; int b; public Tuple(int a, int b) { this.a = a; this.b = b; } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
8a6ecf6b2009d76266f06ea9e460486c
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.lang.Math; import java.lang.reflect.Array; import java.util.*; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static StringTokenizer st; /*write your constructor and global variables here*/ static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { if (p1.a <= p2.a) { return -1; } else { return 1; } } } static class Rec { int a; int b; long c; Rec(int a, int b, long c) { this.a = a; this.b = b; this.c = c; } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } } interface modOperations { int mod(int a, int b, int mod); } static int findBinaryExponentian(int a, int pow, int mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { int retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod( modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod ); } } static int findPow(int a, int b, int mod) { if (b == 1) { return a % mod; } else if (b == 0) { return 1; } else { int res = findPow(a, (int) b / 2, mod); return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod); } } static int bleft(long ele, ArrayList<Long> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(int no) { int i = 0; while ((1 << i) <= no) { i++; } if ((1 << (i - 1)) == no) { return i - 1; } else { return i; } } static modOperations modAdd = (int a, int b, int mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (int a, int b, int mod) -> { return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod)); }; static modOperations modDiv = (int a, int b, int mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static HashSet<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; HashSet<Integer> obj = new HashSet<>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int[] factorialList(int MAXI, int mod) { int[] factorial = new int[MAXI + 1]; factorial[2] = 1; for (int i = 3; i < MAXI + 1; i++) { factorial[i] = modMul.mod(factorial[i - 1], i, mod); } return factorial; } static void put(HashMap<Integer, Integer> cnt, int key) { if (cnt.containsKey(key)) { cnt.replace(key, cnt.get(key) + 1); } else { cnt.put(key, 1); } } static long arrSum(ArrayList<Long> arr) { long tot = 0; for (int i = 0; i < arr.size(); i++) { tot += arr.get(i); } return tot; } static int ord(char b) { return (int) b - (int) 'a'; } static int optimSearch(int[] cnt, int lower_bound, int pow, int n) { int l = lower_bound + 1; int h = n; int ans = 0; while (l <= h) { int mid = l + (h - l) / 2; if (cnt[mid] - cnt[lower_bound] == pow) { return mid; } else if (cnt[mid] - cnt[lower_bound] < pow) { ans = mid; l = mid + 1; } else { h = mid - 1; } } return ans; } static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) { int size = ans.size(); int mini = 1000000000 + 1; long tit = 0l; for (int i = 0; i < size; i++) { tit += 1l * ans.get(i); mini = Math.min(mini, ans.get(i)); } return new Pair<>(tit - mini, mini); } static int factorList( HashMap<Integer, Integer> maps, int no, int maxK, int req ) { int i = 1; while (i * i <= no) { if (no % i == 0) { if (i != no / i) { put(maps, no / i); } put(maps, i); if (maps.get(i) == req) { maxK = Math.max(maxK, i); } if (maps.get(no / i) == req) { maxK = Math.max(maxK, no / i); } } i++; } return maxK; } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } /*write your methods here*/ static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } public static void main(String[] args) throws IOException { int cases = Integer.parseInt(br.readLine()), n, k, i; while (cases-- != 0) { n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int arr[] = new int[n]; int ans[] = new int[n]; for (i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); ans[i] = 1; } for (i = 0; i < n; i++) { int j = arr[i]; if (ans[i] == 0 || j == 0) { j += i; } if (j < n) { ans[j] = 0; } } for (i = 0; i < n; i++) { bw.write(ans[i] + " "); } bw.write("\n"); } bw.flush(); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
ba11f900c3976df7a714bcc8d82c6958
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.lang.Math; import java.lang.reflect.Array; import java.util.*; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static StringTokenizer st; /*write your constructor and global variables here*/ static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { if (p1.a <= p2.a) { return -1; } else { return 1; } } } static class Rec { int a; int b; long c; Rec(int a, int b, long c) { this.a = a; this.b = b; this.c = c; } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } } interface modOperations { int mod(int a, int b, int mod); } static int findBinaryExponentian(int a, int pow, int mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { int retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod( modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod ); } } static int findPow(int a, int b, int mod) { if (b == 1) { return a % mod; } else if (b == 0) { return 1; } else { int res = findPow(a, (int) b / 2, mod); return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod); } } static int bleft(long ele, ArrayList<Long> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(int no) { int i = 0; while ((1 << i) <= no) { i++; } if ((1 << (i - 1)) == no) { return i - 1; } else { return i; } } static modOperations modAdd = (int a, int b, int mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (int a, int b, int mod) -> { return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod)); }; static modOperations modDiv = (int a, int b, int mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static HashSet<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; HashSet<Integer> obj = new HashSet<>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int[] factorialList(int MAXI, int mod) { int[] factorial = new int[MAXI + 1]; factorial[2] = 1; for (int i = 3; i < MAXI + 1; i++) { factorial[i] = modMul.mod(factorial[i - 1], i, mod); } return factorial; } static void put(HashMap<Integer, Integer> cnt, int key) { if (cnt.containsKey(key)) { cnt.replace(key, cnt.get(key) + 1); } else { cnt.put(key, 1); } } static long arrSum(ArrayList<Long> arr) { long tot = 0; for (int i = 0; i < arr.size(); i++) { tot += arr.get(i); } return tot; } static int ord(char b) { return (int) b - (int) 'a'; } static int optimSearch(int[] cnt, int lower_bound, int pow, int n) { int l = lower_bound + 1; int h = n; int ans = 0; while (l <= h) { int mid = l + (h - l) / 2; if (cnt[mid] - cnt[lower_bound] == pow) { return mid; } else if (cnt[mid] - cnt[lower_bound] < pow) { ans = mid; l = mid + 1; } else { h = mid - 1; } } return ans; } static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) { int size = ans.size(); int mini = 1000000000 + 1; long tit = 0l; for (int i = 0; i < size; i++) { tit += 1l * ans.get(i); mini = Math.min(mini, ans.get(i)); } return new Pair<>(tit - mini, mini); } static int factorList( HashMap<Integer, Integer> maps, int no, int maxK, int req ) { int i = 1; while (i * i <= no) { if (no % i == 0) { if (i != no / i) { put(maps, no / i); } put(maps, i); if (maps.get(i) == req) { maxK = Math.max(maxK, i); } if (maps.get(no / i) == req) { maxK = Math.max(maxK, no / i); } } i++; } return maxK; } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } /*write your methods here*/ static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } public static void main(String[] args) throws IOException { int cases = Integer.parseInt(br.readLine()), n, k, i; while (cases-- != 0) { n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int arr[] = new int[n]; int ans[] = new int[n]; for (i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); ans[i] = 1; } for (i = 0; i < n; i++) { int j = arr[i]; if (j == 0 || ans[i] == 0) { j += i; } if (j < n) { ans[j] = 0; } } for (i = 0; i < n; i++) { bw.write(ans[i] + " "); } bw.write("\n"); } bw.flush(); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
474ea0e7542785a98b94424650ebf37f
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class D782{ public static void main(String[] args){ FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); for(int tt = 0; tt< t; tt++){ int n = sc.nextInt(); int[] a = sc.readArray(n); int pointer = 0; int[] b = new int[n]; int sum = 0; int index = 0; while(pointer < n){ while(pointer < a[index] - b[index] * index + index ){ b[pointer] = 1; pointer++; } if(pointer < n) b[pointer] = 0; pointer++; index++; } for(int i = 0; i <n; i++){ out.print(b[i]+ " "); } out.println(); } out.flush(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
af3a4190140140fadd45ffcf5f639319
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import javax.print.attribute.HashAttributeSet; //import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache; //import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLIntegrityConstraintViolationException; public class Main { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } 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 { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void print(long[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static void print(int[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y){ if(y<0) return 0; long m = mod; if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; // factorial private long[] z1; // inverse factorial private long[] z2; // incerse number private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return z1[(int)n]; } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable2{ private long[][] st; SparseTable2(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = max(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return max(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = gcd(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(0); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return min(left, right); } public void update(int index , int val) { arr[index] = val; update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static int TC; static StringBuilder sb = new StringBuilder(); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); TC = 0; for(int i = 1 ; i<=tc ; i++) { TC++; // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.print(sb); } static void TEST_CASE() { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ;i<n ; i++) arr[i] = sc.nextInt(); long[] ans = new long[n]; long[] dp = new long[n]; long tot = 0; for(int e:arr) tot += e; tot /= n; dp[n-1] = tot; if(arr[n-1] == n){ ans[n-1] = 1l; tot--; } for(int i = n-2 ; i>=0 ; i--){ dp[i] = tot; if(arr[i] < i+1) continue; else{ int l = i+1 , r = n-1; while(l<=r){ int m = (l+r)/2; if(m-dp[m]+1>i) r = m-1; else l = m+1; } if(i+1 + (r-i) == arr[i]){ ans[i]++; tot--; } } } for(long e:ans) sb.append(e+" "); sb.append("\n"); } } /*******************************************************************************************************************************************************/ /** 1 5 3 7 3 6 12 */
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
cd469f6a6217b75532c5cab90ce79e84
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7); final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE; final static long INF = (long) 1e18, Neg_INF = (long) -1e18; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(); int[] C = readIntArray(n), A = new int[n], B = new int[n]; long total = 0L; for (int i : C) total += i; int k = (int) (total / n), rem = n - k; for (int i = rem; i < n; i++) B[i] = n - 1; for (int i = n - 1; i >= 0 && i >= rem; i--) { if (C[i] - (B[i] - i) == i + 1) A[i] = 1; else B[--rem] = i - 1; } printIArray(A); } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // change Stack size -> java -Xss16M CodeForces.java // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray() throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== 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 lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
f1a4957d65dbcee1bf05349ead49b0fa
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DReverseSortSum { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); int[] ans = new int[n]; Arrays.fill(ans, 1); for (int i = 0; i < n; i++) { if (arr[i] == 0) ans[i] = 0; if (ans[i] == 0) { if (i + arr[i] < n) ans[i + arr[i]] = 0; } else { if (arr[i] < n) ans[arr[i]] = 0; } } out.println(ans); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
248d621cbe6e6ffbed9aed91a309708f
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ 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); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DReverseSortSum { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); var c = in.readIntArray(n); var a = new int[n]; int j = 0; while (j < c.length && c[j] == 0) j++; // a[j] = 1 int i = j + 1; // first unknown, start from that if (j < a.length) { int cj = c[j]; for (; j < cj; j++) { a[j] = 1; } // a[c[j]] = 0 j = cj + 1; } for (; j < a.length && i < c.length; ++i) { int end = (a[i] == 1) ? c[i] : i + c[i]; for (; j < a.length && j < end; ++j) { a[j] = 1; } j++; } out.println(a); } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = readLine(); if (line == null) continue; tokenizer = new StringTokenizer(line); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); // IntStream.generate(this::nextInt).limit(size).toArray(); return array; } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void println(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) print(' '); print(array[i]); } println(); } public void close() { super.close(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
c49b8ed2bb7cfd806ab29c1ee715333d
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ 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); DReverseSortSum solver = new DReverseSortSum(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DReverseSortSum { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); var c = in.readIntArray(n); var a = new int[n]; int j = 0; while (j < c.length && c[j] == 0) j++; int i = -1; if (j < a.length) { for (int k = j; k < c[j] && k < a.length; k++) { a[k] = 1; } i = j + 1; j = c[j] + 1; } for (; j < a.length && i < c.length; ++i) { if (i < j && a[i] == 1) { for (; j < a.length && j < c[i]; ++j) { a[j] = 1; } if (j < a.length) { a[j] = 0; j++; } } else if (i < j && a[i] == 0) { for (; j < a.length && j < i + c[i]; ++j) { a[j] = 1; } if (j < a.length) { a[j] = 0; j++; } } } out.println(a); } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = readLine(); if (line == null) continue; tokenizer = new StringTokenizer(line); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); // IntStream.generate(this::nextInt).limit(size).toArray(); return array; } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void println(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) print(' '); print(array[i]); } println(); } public void close() { super.close(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
d0280c61ca05feb98b7b726b87a214c8
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.math.*; import java.sql.Array; public class Simple{ public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return (int) (this.y - other.y); } public boolean equals(Pair other){ if(this.x == other.x && this.y == other.y)return true; return false; } public int hashCode(){ return 31*x + y; } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static class Node{ int root; ArrayList<Node> al; Node par; public Node(int root,ArrayList<Node> al,Node par){ this.root = root; this.al = al; this.par = par; } } // public static int helper(int arr[],int n ,int i,int j,int dp[][]){ // if(i==0 || j==0)return 0; // if(dp[i][j]!=-1){ // return dp[i][j]; // } // if(helper(arr, n, i-1, j-1, dp)>=0){ // return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp)); // } // return dp[i][j] = helper(arr, n, i-1, j,dp); // } // public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){ // levelcount[count]++; // for(Integer x : al.get(node)){ // dfs(al, levelcount, x, count+1); // } // } public static long __gcd(long a, long b) { if (b == 0) return a; return __gcd(b, a % b); } public static long helper(int arr1[][],int m){ Arrays.sort(arr1, (int a[],int b[])-> a[0]-b[0]); long ans =0; for(int i=1;i<m;i++){ long count =0; for(int j=0;j<i;j++){ if(arr1[i][1] > arr1[j][1])count++; } ans+=count; } return ans; } public static class DSU{ int n; int par[]; int rank[]; public DSU(int n){ this.n = n; par = new int[n+1]; rank = new int[n+1]; for(int i=1;i<=n;i++){ par[i] = i ; rank[i] = 0; } } public int findPar(int node){ if(node==par[node]){ return node; } return par[node] = findPar(par[node]);//path compression } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[u]<rank[v]){ par[u] = v; } else if(rank[u]>rank[v]){ par[v] = u; } else{ par[v] = u; rank[u]++; } } } public static void dfs(ArrayList<ArrayList<Integer>> adj, int l[],int r[],boolean vis[],int node,long dp[][]){ long ansl = 0; long ansr = 0; vis[node] = true; for(Integer x : adj.get(node)){ if(!vis[x]){ vis[x] = true; dfs(adj, l, r, vis, x, dp); ansl+=Math.max(Math.abs(l[node]-l[x])+dp[x][0], Math.abs(l[node]-r[x])+dp[x][1]); ansr+=Math.max(Math.abs(r[node]-l[x])+dp[x][0], Math.abs(r[node]-r[x])+dp[x][1]); } } dp[node][0] = ansl; dp[node][1] = ansr; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); } Queue<Integer> q = new LinkedList<>(); int ans[] = new int[n]; int i= 0 ; while(i<n && arr[i]==0)i++; if(i!=n){ int next = arr[i]; ans[i] = 1; if(next<n)q.add(next); i++; } for(;i<n;i++){ if(!q.isEmpty() && q.peek()==i){ int next = q.poll() +arr[i]; if(next<n)q.add(next); } else{ ans[i] = 1; q.add(arr[i]); } } for(i=0;i<n;i++){ System.out.print(ans[i]+" "); } System.out.println(); } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
21baef2015fbcbc6cd8aed958aca480c
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.Scanner; public class Main { Object solve(int n, int[] c) { long sum = 0; for (int x : c) { sum += x; } int ones = (int) (sum / n); int[] b = new int[n]; int[] d = new int[n + 1]; int dec = 0; for (int i = n - 1; i >= 0; i--) { c[i] -= dec; d[i - ones + 1]++; dec += 1 - d[i]; if (c[i] == i + 1) { b[i] = 1; ones--; } else { b[i] = 0; } } return toString(b); } Object readInputAndSolve(Scanner in) { int n = in.nextInt(); int[] c = new int[n]; for (int i = 0; i < n; i++) { c[i] = in.nextInt(); } return solve(n, c); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int nTests = in.nextInt(); for (int iTest = 0; iTest < nTests; iTest++) { Object result = new Main().readInputAndSolve(in); if (result != null) { System.out.println(result); } } } public static String toString(int[] items) { StringBuilder sb = new StringBuilder(); for (int e : items) { sb.append(e); sb.append(" "); } return sb.toString(); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
99fa0b92b5152ef48a027684b11f677a
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
// Generated by Code Flattener. // https://plugins.jetbrains.com/plugin/9979-idea-code-flattener import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; import java.util.stream.Collectors; public class Main { public void solve(FastReader in, FastWriter out) { int n = in.nextInt(); int[] a = in.nextInts(n); int[] ans = new int[n]; long sum = Arrays.stream(a).mapToLong(x -> x).sum(); int os = (int) (sum / n); SegmentTree<Integer> st = new NodeSegmentTree<>(0, Integer::sum); for (int i = n - 1; i > 0; i--) { st.set(i - os + 1, st.get(i - os + 1) + 1); int tmp = st.reduce(0, i + 1); if (tmp < a[i]) { ans[i] = 1; os--; } } if (os > 0) { ans[0] = 1; } out.println(ans); } public static void main(String[] args) { Main main = new Main(); boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null; Problem.Builder builder = Problem .builder() .solution(main::solve) .multiTest(true); if (onlineJudge) { builder.io(StreamPair.standard()); } else { builder.io(StreamPair.debug(main.getClass())); } builder.build().execute(); } private static class Problem { private final Executable solution; private final Executable init; private final boolean multiTest; private final InputStream inputStream; private final OutputStream outputStream; public Problem(Executable solution, Executable init, boolean multiTest, InputStream inputStream, OutputStream outputStream) { this.solution = solution; this.init = init; this.multiTest = multiTest; this.inputStream = inputStream; this.outputStream = outputStream; } public void execute() { if (solution == null) { throw new RuntimeException("please, set solution"); } FastReader in = new FastReader(inputStream); FastWriter out = new FastWriter(outputStream); if (init != null) { init.run(in, out); } if (!multiTest) { solution.run(in, out); } else { int times = in.nextInt(); for (int i = 0; i < times; i++) { solution.run(in, out); } } out.close(); } public static Builder builder() { return new Builder(); } public static class Builder { private Executable solution; private Executable init; private boolean multiTest; private InputStream inputStream; private OutputStream outputStream; private Builder() { } public Builder solution(Executable solution) { this.solution = solution; return Builder.this; } public Builder init(Executable init) { this.init = init; return Builder.this; } public Builder multiTest(boolean multiTest) { this.multiTest = multiTest; return Builder.this; } public Builder io(StreamPair pair) { this.inputStream = pair.getInputStream(); this.outputStream = pair.getOutputStream(); return Builder.this; } public Problem build() { return new Problem(solution, init, multiTest, inputStream, outputStream); } } } private static class FastReader { private final BufferedReader br; private StringTokenizer st; public FastReader(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextInts(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public String nextLine() { try { String line = br.readLine(); if (line == null) { throw new RuntimeException("empty line"); } st = null; return line; } catch (IOException e) { throw new RuntimeException(e); } } } private static class FastWriter { final private PrintWriter printWriter; public FastWriter(OutputStream outputStream) { printWriter = new PrintWriter(new OutputStreamWriter(outputStream)); } public void println(String s) { printWriter.println(s); } public void println(int[] arr) { String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")); println(line); } public void close() { printWriter.close(); } } private static class StreamPair { private final InputStream inputStream; private final OutputStream outputStream; public StreamPair(InputStream inputStream, OutputStream outputStream) { this.inputStream = inputStream; this.outputStream = outputStream; } public static StreamPair standard() { return new StreamPair(System.in, System.out); } public static StreamPair debug(Class resourceClass) { try { File file = new File(resourceClass.getClassLoader().getResource("input.txt").getFile()); return new StreamPair(new FileInputStream(file), System.out); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } public InputStream getInputStream() { return inputStream; } public OutputStream getOutputStream() { return outputStream; } } private static class NodeSegmentTree<T> implements SegmentTree<T> { private final T identity; private final BiOperator<T> biOperator; private final Node root; public NodeSegmentTree(T identity, BiOperator<T> biOperator) { this.identity = identity; this.biOperator = biOperator; this.root = new Node(0, 1000000010); } @Override public void set(int inx, T val) { set(inx, val, root); } @Override public T reduce(int l, int r) { return reduce(l, r, root); } private void set(int inx, T val, Node node) { if (node.r - node.l == 1) { node.val = val; return; } int m = (node.l + node.r) / 2; if (node.lNode == null || node.rNode == null) { node.lNode = new Node(node.l, m); node.rNode = new Node(m, node.r); } if (inx < m) { set(inx, val, node.lNode); } else { set(inx, val, node.rNode); } node.val = biOperator.operate(node.lNode.val, node.rNode.val); } private T reduce(int l, int r, Node node) { if (node == null || node.r <= l || r <= node.l) { return identity; } if (l <= node.l && node.r <= r) { return node.val; } return biOperator.operate(reduce(l, r, node.lNode), reduce(l, r, node.rNode)); } private class Node { private final int l; private final int r; private T val; private Node lNode; private Node rNode; private Node(int l, int r) { this.l = l; this.r = r; this.val = identity; } } public interface BiOperator<T> { T operate(T first, T second); } } private interface SegmentTree<T> { void set(int inx, T val); T reduce(int l, int r); default T get(int inx) { return reduce(inx, inx + 1); } } private interface Executable { void run(FastReader in, FastWriter out); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
9bd9ec38e92096eb9571f7032fe53854
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } void run() { Scanner sc=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); int T=sc.nextInt(); while (T-->0) { int N=sc.nextInt(); int[] C=new int[N]; for (int i=0;i<N;++i) C[i]=N-sc.nextInt(); int[] ans=new int[N]; int p=0; for (int i=0;i<N;++i) { if (C[p] - (p==i? 1 : ans[p]) * p - (N-i)== 0) { ans[i] = 1; ++p; } } for (int i=0;i<N;++i) ans[i]^=1; for (int i=0;i<N;++i) { pw.print(ans[i]+(i==N-1?"\n":" ")); } } pw.close(); } void tr(Object...o) {System.out.println(Arrays.deepToString(o));} }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
e21520ae84a6b5b62bbf3e07013550a9
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; public class D { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } static class seg_tree { int seg_tree[],lazy[]; public seg_tree(int n,int arr[]) { seg_tree=new int[4*n]; lazy=new int[4*n]; built(arr,0,0,n-1); // int query=input.nextInt(); // for(int q=1;q<=query;q++) { // int type=input.nextInt(); // if(type==1) { // System.out.println(11111); // System.out.println(find_max(0,0,n-1,input.nextInt()-1,input.nextInt()-1)); // } // else { // update(0,0,n-1,input.nextInt()-1,input.nextInt()-1,input.nextInt()); // } // } } public void built(int arr[], int vertex, int l,int r) { if(l==r) { seg_tree[vertex]=arr[r]; return; } int mid=(l+r)/2; //Left Child built(arr,(2*vertex)+1, l, mid); //Right Child built(arr,(2*vertex)+2, mid+1, r); seg_tree[vertex]=Math.min(seg_tree[(2*vertex)+1],seg_tree[(2*vertex)+2]); } public void push(int vertex) { seg_tree[(2*vertex)+1]+=lazy[vertex]; lazy[(2*vertex)+1]+=lazy[vertex]; seg_tree[(2*vertex)+2]+=lazy[vertex]; lazy[(2*vertex)+2]+=lazy[vertex]; lazy[vertex]=0; } public void update(int vertex,int l,int r,int ql,int qr,int add) { if(ql>qr) { return; } if(l==ql && r==qr) { seg_tree[vertex]+=add; lazy[vertex]+=add; return; } push(vertex); int mid=(l+r)/2; //Left Child update((2*vertex)+1,l,mid,ql,Math.min(mid,qr),add); //Right Child update((2*vertex)+2,mid+1,r,Math.max(mid+1,ql),qr,add); seg_tree[vertex]=Math.min(seg_tree[(2*vertex)+1],seg_tree[(2*vertex)+2]); } public int find_min(int vertex,int l,int r,int ql,int qr) { // System.out.println(vertex+" "+l+" "+r+" "+ql+" "+qr); if(ql>qr) { return Integer.MAX_VALUE; } if(ql<=l && r<=qr) { return seg_tree[vertex]; } push(vertex); int mid=(l+r)/2; return Math.min(find_min((2*vertex)+1,l,mid,ql,Math.min(qr,mid)),find_min((2*vertex)+2,mid+1,r,Math.max(mid+1,ql),qr)); } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { int n=input.scanInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=input.scanInt(); } int brr[]=new int[n]; int zeros=0; seg_tree st=new seg_tree(n,arr); for(int i=0;i<n;i++) { brr[i]=1; st.update(0, 0, n-1, i, i, -i); st.update(0, 0, n-1, zeros, i, -1); if(st.find_min(0, 0, n-1, 0, i)<0) { // System.out.println(1111111); brr[i]=0; st.update(0, 0, n-1, i, i, i); st.update(0, 0, n-1, zeros, i, 1); zeros++; st.update(0, 0, n-1, zeros, i, -1); } // System.out.println("=========="+i+"=========="); // for(int j=0;j<n;j++) { // System.out.print(arr[j]+" "); // } // System.out.println (); // for(int j=0;j<n;j++) { // System.out.print(brr[j]+" "); // } // System.out.println (); } // for(int i=0;i<n;i++) { // System.out.print(arr[i]+" "); // } // System.out.println (); for(int i=0;i<n;i++) { ans.append(brr[i]+" "); } ans.append("\n"); } System.out.println(ans); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
d090ab371cc7c269e385a54c96a68873
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static long INF = (long) 1e18; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); long[] arr = new long[n]; long totalSum = 0; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); totalSum += arr[i]; } long[] res = new long[n]; long[] remaining = new long[n]; long countOnes = totalSum / n, currValue = 0; for (int i = n - 1; i >= 0; i--) { currValue -= remaining[i]; if (countOnes > 0) { currValue++; if (i - countOnes >= 0) { remaining[(int) (i - countOnes)]++; } } arr[i] -= currValue; if (countOnes > 0 && arr[i] == i){ res[i] = 1; countOnes--; } } for (int i = 0; i < n; i++) { out.print(res[i] + " "); } out.println(); } public static FastReader sc; public static PrintWriter out; 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
efb0ab19ae4b051272d2d2e9211cf27d
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.*; import java.util.List; public class Main implements Runnable { public static final int LIMIT = 100010; int n, m, k; static boolean use_n_tests = true; int[] w; int l, r; void solve(FastScanner in, PrintWriter out, int testNumber) { int n = in.nextInt(); int[] v = in.nextArray(n); long sum = Array.sum(v); long l = sum / n; int[] rem = new int[n]; int cur = 0; int[] sol = new int[n]; for (int i = n - 1; i >= 0; --i) { cur -= rem[i]; if (l != 0) { cur += 1; if (i - l >= 0) { rem[(int) (i - l)] += 1; } } v[i] -= cur; if (v[i] == i && l != 0) { sol[i] = 1; l -= 1; } } for (int i = 0; i < n; ++i) { out.print(sol[i] + " "); } out.println(); } double tri_s(double x1, double x2, double x3, double y1, double y2, double y3) { return ((x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)) / 2.0; } class Pt { public Pt(int x, int y) { this.x = x; this.y = y; } int x, y; } private int getAnsFor(Map<Integer, Integer> hm, Map<Integer, List<Integer>> vm) { Set<Integer> ls = null; for (int key : hm.keySet()) { int value = hm.get(key); if (!vm.containsKey(key)) { continue; } else { List<Integer> cnt = vm.get(key); Set<Integer> taken = new HashSet<>(); for (int k = 0; k < cnt.size() / 2; k++) { if (cnt.get(k * 2) <= value) { taken.add(cnt.get(k * 2 + 1)); } } if (ls == null) { ls = taken; } else { ls.addAll(taken); } } } if (ls == null) { return 0; } return ls.size(); } private Map<Integer, Integer> getIntegerIntegerMap(int to) { Map<Integer, Integer> hm = new HashMap<>(); for (int j = 2; j * j <= to; j++) { while (to % j == 0) { hm.merge(j, 1, Integer::sum); to /= j; } } if (to != 1) { hm.merge(to, 1, Integer::sum); } hm.put(1, 1); return hm; } // ****************************** template code *********** public static class DisjointSets { int[] p; int[] size; int sccCount; DisjointSets(int size) { p = new int[size]; this.size = new int[size]; for (int i = 0; i < size; i++) { this.size[i] = 1; p[i] = i; } sccCount = size; } public int root(int x) { return x == p[x] ? x : (p[x] = root(p[x])); } public void unite(int a, int b) { a = root(a); b = root(b); if (a == b) { return; } if (size[b] > size[a]) { int tmp = a; a = b; b = tmp; } size[a] += size[b]; p[b] = a; sccCount--; } int size(int id) { return size[root(id)]; } } static boolean use_file_insteadof_stdin = false; //System.getProperty("ONLINE_JUDGE") == null; static String input = "input.txt"; static String output = "output.txt"; List<Integer> getGidits(long n) { List<Integer> res = new ArrayList<>(); while (n != 0) { res.add((int) (n % 10L)); n /= 10; } return res; } List<Integer> generatePrimes(int n) { List<Integer> res = new ArrayList<>(); boolean[] sieve = new boolean[n + 1]; for (int i = 2; i <= n; i++) { if (!sieve[i]) { res.add(i); } if ((long) i * i <= n) { for (int j = i * i; j <= n; j += i) { sieve[j] = true; } } } return res; } int ask(int l, int r) { if (l >= r) { return -1; } System.out.printf("? %d %d\n", l + 1, r + 1); System.out.flush(); return in.nextInt() - 1; } static int stack_size = 1 << 27; class Mod { long mod; Mod(long mod) { this.mod = mod; } long add(long a, long b) { a = mod(a); b = mod(b); return (a + b) % mod; } long sub(long a, long b) { a = mod(a); b = mod(b); return (a - b + mod) % mod; } long mul(long a, long b) { a = mod(a); b = mod(b); return (a * b) % mod; } long div(long a, long b) { a = mod(a); b = mod(b); return (a * inv(b)) % mod; } public long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } private long mod(long a) { return a % mod; } } static class Coeff { long mod; long[][] C; long[] fact; boolean cycleWay = false; Coeff(int n, long mod) { this.mod = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = i; fact[i] %= mod; fact[i] *= fact[i - 1]; fact[i] %= mod; } } Coeff(int n, int m, long mod) { // n > m cycleWay = true; this.mod = mod; C = new long[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= Math.min(i, m); j++) { if (j == 0 || j == i) { C[i][j] = 1; } else { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; C[i][j] %= mod; } } } } public long C(int n, int m) { if (cycleWay) { return C[n][m]; } return fC(n, m); } private long fC(int n, int m) { return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod; } private long fact(int n) { return fact[n]; } private long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } } class Pair { int first; long second; Pair(int f, long s) { first = f; second = s; } public int getFirst() { return first; } public long getSecond() { return second; } } class MultisetTree<T> { int size = 0; TreeMap<T, Integer> mp = new TreeMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } List<T> getElems() { List<T> res = new ArrayList<>(); for (T k : mp.keySet()) { int c = mp.get(k); for (int i = 0; i < c; i++) { res.add(k); } } return res; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } boolean contains(T x) { return mp.containsKey(x); } T greatest() { return mp.lastKey(); } T smallest() { return mp.firstKey(); } int size() { return size; } int diffSize() { return mp.size(); } } class Multiset<T> { int size = 0; Map<T, Integer> mp = new HashMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } boolean contains(T x) { return mp.containsKey(x); } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } int size() { return size; } int diffSize() { return mp.size(); } } static class Range { int l, r; int id; public int getL() { return l; } public int getR() { return r; } public Range(int l, int r, int id) { this.l = l; this.r = r; this.id = id; } } static class Array { static Range[] readRanges(int n, FastScanner in) { Range[] result = new Range[n]; for (int i = 0; i < n; i++) { result[i] = new Range(in.nextInt(), in.nextInt(), i); } return result; } static List<List<Integer>> intInit2D(int n) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } return res; } static boolean isSorted(Integer[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static public long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public long sum(long[] a) { long sum = 0; for (long x : a) { sum += x; } return sum; } static public long sum(Integer[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public int min(Integer[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int min(int[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int max(Integer[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int max(int[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int[] readint(int n, FastScanner in) { int[] out = new int[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } } class Graph { List<List<Integer>> graph; Graph(int n) { create(n); } private void create(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } this.graph = graph; } void readBi(int m, FastScanner in) { for (int i = 0; i < m; i++) { int v = in.nextInt() - 1; int u = in.nextInt() - 1; graph.get(v).add(u); graph.get(u).add(v); } } void add(int v, int u) { graph.get(v).add(u); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public char[] nextc() { return next().toCharArray(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long[] nextArrayL(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Long[] nextArrayL2(int n) { Long[] res = new Long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Integer[] nextArray2(int n) { Integer[] res = new Integer[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } } void run_t_tests() { int t = in.nextInt(); int i = 0; while (t-- > 0) { solve(in, out, i++); } } void run_one() { solve(in, out, -1); } @Override public void run() { if (use_file_insteadof_stdin) { try { in = new FastScanner(new FileInputStream(input)); // out = new PrintWriter(new FileOutputStream(output)); } catch (FileNotFoundException e) { e.printStackTrace(); } } else { in = new FastScanner(System.in); } out = new PrintWriter(System.out); if (use_n_tests) { run_t_tests(); } else { run_one(); } out.close(); } static FastScanner in; static PrintWriter out; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new Main(), "", stack_size); thread.start(); thread.join(); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
292ece40b4ddf6a19a502bddcb8df3d9
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
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.List; import java.util.StringTokenizer; public class TaskD { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); TaskD s = new TaskD(); s.solve(in, out); out.flush(); } void solveOne(FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] c = new int[n]; for (int i = 0; i < n; i++) c[i] = in.nextInt(); int num_1 = (int) (Arrays.stream(c).asLongStream().sum() / n); int[] a = new int[n]; if (n == 1) { out.println(c[0] == 0 ? 0 : 1); return; } a[n-1] = (c[n-1] == n ? 1 : 0); if (a[n-1] == 1) num_1--; List<Integer> nearestZero = new ArrayList<>(); if (a[n-1] == 0) nearestZero.add(n-1); for (int i = n-2; i >= 0; i--) { if (num_1 == 0) break; int if0 = 0, if1 = i; if (num_1 > nearestZero.size()) { if0 = n-i; } else { int idx = nearestZero.get(nearestZero.size() - num_1); if0 = idx-i; } if1 += if0; a[i] = (c[i] == if1 ? 1 : 0); if (a[i] == 1) num_1--; if (a[i] == 0) nearestZero.add(i); } for (int i = 0; i < n; i++) { out.printf("%d ", a[i]); } out.println(""); } void solve(FastScanner in, PrintWriter out) { int t = 1; t = in.nextInt(); for (int tc = 1; tc <= t; tc++) { // out.printf("Case #%d: ", tc); solveOne(in, out); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
e0c80b7edcd702a3b632176c543750d7
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
// Contest 1659, Problem D // Reverse Sort Sum import java.io.*; import java.util.*; public class D { static BufferedInputStream bis; public static int readInt() throws IOException { int num = 0; int b = bis.read(); while(b < '0' || b > '9') b = bis.read(); while(b >= '0') { num = num*10 + b - '0'; b = bis.read(); } return num; } public static void main(String[] args) throws IOException { bis = new BufferedInputStream(System.in); StringBuilder sb = new StringBuilder(); int T = readInt(); for(int cN=0; cN<T; cN++) { int N = readInt(); int [] A = new int [N]; long sum = 0; for(int i=0; i<N; i++) { A[i] = readInt(); sum += A[i]; } int [] B = new int [N]; int [] x = new int [N]; // last round in which i is a one int ones = (int)(sum/N); for(int i=0; i<ones; i++) { x[N-1-i] = N; B[N-1-i] = 1; } for(int i=N-1; i>0; i--) { if(ones > 0) { // then B_{i+1}[i] is a one if(A[i] == (x[i] - i)) { // then this becomes a 0 B[i] = 0; B[i-ones] = 1; x[i-ones] = i; } else { ones--; } } // else it's already a 0, no need to do anything //System.out.println("B["+i+"] = "+B[i]+", "+ones+" ones left"); } //System.out.println(Arrays.toString(B)); sb.append(B[0]); for(int i=1; i<N; i++) { sb.append(' ').append(B[i]); } sb.append('\n'); } System.out.print(sb); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
495b0745ebbebcddfc99e309e49a17f0
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; public class D { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } static class seg_tree { int seg_tree[],lazy[]; public seg_tree(int n,int arr[]) { seg_tree=new int[4*n]; lazy=new int[4*n]; built(arr,0,0,n-1); // int query=input.nextInt(); // for(int q=1;q<=query;q++) { // int type=input.nextInt(); // if(type==1) { // System.out.println(11111); // System.out.println(find_max(0,0,n-1,input.nextInt()-1,input.nextInt()-1)); // } // else { // update(0,0,n-1,input.nextInt()-1,input.nextInt()-1,input.nextInt()); // } // } } public void built(int arr[], int vertex, int l,int r) { if(l==r) { seg_tree[vertex]=arr[r]; return; } int mid=(l+r)/2; //Left Child built(arr,(2*vertex)+1, l, mid); //Right Child built(arr,(2*vertex)+2, mid+1, r); seg_tree[vertex]=Math.min(seg_tree[(2*vertex)+1],seg_tree[(2*vertex)+2]); } public void push(int vertex) { seg_tree[(2*vertex)+1]+=lazy[vertex]; lazy[(2*vertex)+1]+=lazy[vertex]; seg_tree[(2*vertex)+2]+=lazy[vertex]; lazy[(2*vertex)+2]+=lazy[vertex]; lazy[vertex]=0; } public void update(int vertex,int l,int r,int ql,int qr,int add) { if(ql>qr) { return; } if(l==ql && r==qr) { seg_tree[vertex]+=add; lazy[vertex]+=add; return; } push(vertex); int mid=(l+r)/2; //Left Child update((2*vertex)+1,l,mid,ql,Math.min(mid,qr),add); //Right Child update((2*vertex)+2,mid+1,r,Math.max(mid+1,ql),qr,add); seg_tree[vertex]=Math.min(seg_tree[(2*vertex)+1],seg_tree[(2*vertex)+2]); } public int find_min(int vertex,int l,int r,int ql,int qr) { // System.out.println(vertex+" "+l+" "+r+" "+ql+" "+qr); if(ql>qr) { return Integer.MAX_VALUE; } if(ql<=l && r<=qr) { return seg_tree[vertex]; } push(vertex); int mid=(l+r)/2; return Math.min(find_min((2*vertex)+1,l,mid,ql,Math.min(qr,mid)),find_min((2*vertex)+2,mid+1,r,Math.max(mid+1,ql),qr)); } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { int n=input.scanInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=input.scanInt(); } int brr[]=new int[n]; int zeros=0; seg_tree st=new seg_tree(n,arr); for(int i=0;i<n;i++) { brr[i]=1; st.update(0, 0, n-1, i, i, -i); st.update(0, 0, n-1, zeros, i, -1); if(st.find_min(0, 0, n-1, 0, i)<0) { // System.out.println(1111111); brr[i]=0; st.update(0, 0, n-1, i, i, i); st.update(0, 0, n-1, zeros, i, 1); zeros++; st.update(0, 0, n-1, zeros, i, -1); } // System.out.println("=========="+i+"=========="); // for(int j=0;j<n;j++) { // System.out.print(arr[j]+" "); // } // System.out.println (); // for(int j=0;j<n;j++) { // System.out.print(brr[j]+" "); // } // System.out.println (); } // for(int i=0;i<n;i++) { // System.out.print(arr[i]+" "); // } // System.out.println (); for(int i=0;i<n;i++) { ans.append(brr[i]+" "); } ans.append("\n"); } System.out.println(ans); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
a3ae6ca6afe5aa67df9d5034df0e1a98
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class E { 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 { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>>{ // note first and second must not change T first; U second; private int hashCode; public Pair(T first, U second) { this.first = first; this.second = second; hashCode = Objects.hash(first, second); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair p = (Pair) o; return first.equals(p.first) && second.equals(p.second); } public int hashCode() { return hashCode; } public int compareTo(Pair<T, U> o) { return first.compareTo(o.first); } } public static void main(String args[]) { Reader rd = new Reader(); int tcs = rd.nextInt(); for (int tc = 1; tc <= tcs; tc++) { int n = rd.nextInt(); int[] c = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { c[i] = rd.nextInt(); sum += c[i]; } int numOnes = (int)(sum / n); int[] a = new int[n]; LinkedList<Integer> list = new LinkedList<Integer>(); for (int i = n - 1; i >= 0; i--) { while (!list.isEmpty() && i <= list.peek()) { list.pop(); } c[i] -= list.size(); list.add(i - numOnes); if (c[i] == i + 1) { a[i] = 1; numOnes--; } } StringBuilder o = new StringBuilder(); for (int i = 0; i < n; i++) o.append(Integer.toString(a[i]) + " "); System.out.println(o.toString()); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
c0207f2b2af27c8c4fafd07231bda2d7
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; import java.io.*; public class B { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = fs.nextInt(); Queue<Integer> pz = new LinkedList<>(); int[] ans = new int[n]; for (int i = 0; i < n; i++) { if (pz.size() == 0 && a[i] == 0) ans[i] = 0; else if (pz.size() == 0) { ans[i] = 1; pz.add(a[i]); } else if (i == pz.peek()) { pz.remove(); ans[i] = 0; pz.add(a[i] + i); } else { ans[i] = 1; pz.add(a[i]); } } for (int i = 0; i < n; i++) sb.append(ans[i] + " "); sb.append("\n"); } pw.print(sb.toString()); pw.close(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
0b7b985245b043fa5341e50e53fdc19a
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.*; /** * A simple template for competitive programming problems. */ public class Solution { //InputReader in = new InputReader("input.txt"); final InputReader in = new InputReader(System.in); final PrintWriter out = new PrintWriter(System.out); static final int mod = 1000000007; void solve() { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int[] c = in.nextArray(n); int[] ans = new int[n]; int i = 0; int j = 0; int ones = 0; while(i<n && c[i]==0) { ans[j++] = 0; i++; } if(j<n) { for(int k=0; k<c[i]-i; k++) { ans[j++] = 1; ones++; } if(j<n) ans[j++] = 0; i++; } while(j<n) { int onesi = c[i]-i*ans[i]; int onesToPut = onesi-ones; for(int k=0; k<onesToPut; k++) { ans[j++] = 1; ones++; } if(j<n) ans[j++] = 0; i++; } printArray(ans); } } private void printArray(int[] ans) { for(int k : ans) { out.print(k+" "); } out.println(); } public static void main(final String[] args) throws FileNotFoundException { final Solution s = new Solution(); final Long t1 = System.currentTimeMillis(); s.solve(); System.err.println(System.currentTimeMillis() - t1 + " ms"); s.out.close(); } public Solution() throws FileNotFoundException { } private static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; Random r = new Random(); InputReader(final InputStream stream) { this.stream = stream; } InputReader(final String fileName) { InputStream stream = null; try { stream = new FileInputStream(fileName); } catch (final FileNotFoundException e) { e.printStackTrace(); } this.stream = stream; } int[] nextArray(final int n) { final int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[][] nextMatrix(final int n, final int m) { final int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); final StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); final StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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; } 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; } int[] randomArray(int n, int low, int up) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = low + r.nextInt(up - low + 1); } return arr; } double nextDouble() { return Double.parseDouble(nextString()); } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (final IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private boolean isSpaceChar(final int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(final int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
3b3d5ba5b573564aca66bb7aadfd06fe
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class D { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub int testCases = sc.nextInt(); for (int i = 1; i <= testCases; ++i) { solve(i); } } private static void solve(int t) { int n = sc.nextInt(); int [] arr = new int [n]; for (int i = 0; i < n; ++i) arr[i] = sc.nextInt(); int [] res = solve2(arr); StringBuilder sb = new StringBuilder(); for (int i = 0; i < res.length; ++i) { sb.append(res[i]); if (i != res.length - 1) sb.append(' '); } System.out.println(sb); } private static int [] solve2(int [] arr) { if (arr.length == 1) { return arr; }else if (arr.length == 2) { //System.out.println(Arrays.toString(arr)); int [] res = new int [2]; res[1] = arr[1] == 2 ? 1 : 0; res[0] = arr[0] == 0 ? 0 : 1; return res; } int start = 1 + arr.length/2; int [] res = new int [arr.length]; int [] next = new int [start]; long sum = 0; for (int num : arr) sum += num; int ones = (int) (sum / arr.length); for (int i = 0; i < start; ++i) { next[i] = arr[i]; } int zeros = arr.length - ones; for (int i = start; i < arr.length; ++i) { if (arr[i] >= start) res[i] = 1; else { res[i] = 0; --zeros; } } int [] min = new int [arr.length]; Arrays.fill(min, -1); for (int i = start; i < arr.length; ++i) { if (res[i] == 0) ++zeros; min[i] = zeros; } int count = 0; int idx = start; for (int i = 0; i < start; ++i) { while (idx < arr.length && i == min[idx]) { ++count; ++idx; } next[i] -= count; } int [] firstHalf = solve2(next); for (int i = 0; i < firstHalf.length; ++i) { res[i] = firstHalf[i]; } /* System.out.println(Arrays.toString(min)); System.out.print(Arrays.toString(arr) + " " ); System.out.println(Arrays.toString(res) ); */ return res; } public static void print(int test, long result) { System.out.println("Case #" + test + ": " + result); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
745f5bd746141ec4e0c748ba3fd47e0d
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.util.*; /** * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools * * To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other */ public class Main { public static void main(String[] args) { // Write your solution here Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t-- > 0) { int n = Integer.parseInt(sc.nextLine()); String[] in = sc.nextLine().split(" "); int[] v = new int[in.length]; long sum = 0; for(int i = 0; i < n; i++) { v[i] = Integer.parseInt(in[i]); sum += v[i]; } long ones = sum / n; int[] rem = new int[n]; int cur = 0; int[] sol = new int[n]; for(int i = n - 1; i >= 0; --i) { cur -= rem[i]; if(ones > 0) { ++cur; if(i - ones >= 0) { int temp = (int)(i - ones); ++rem[temp]; } } v[i] -= cur; if(v[i] == i && ones > 0) { sol[i] = 1; --ones; } } StringBuilder builder = new StringBuilder(); for(int i =0;i < sol.length;++i) { builder.append(sol[i]); builder.append(" "); } if(t == 0) { String val = builder.toString().trim(); System.out.println(val); } else { System.out.println(builder.toString()); } } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
196f36dd7d624ebedf9e06cbe5b10996
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
// package c1659; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; // // Codeforces Round #782 (Div. 2) 2022-04-17 07:35 // D. Reverse Sort Sum // https://codeforces.com/contest/1659/problem/D // time limit per test 2 seconds; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // Suppose you had an array A of n elements, each of which is 0 or 1. // // Let us define a function f(k,A) which returns another array B, the result of sorting the first k // elements of A in non-decreasing order. For example, f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,0,1,0]. // Note that the first 4 elements were sorted. // // Now consider the arrays B_1, B_2,..., B_n generated by f(1,A), f(2,A),...,f(n,A). Let C be the // array obtained by taking the element-wise sum of B_1, B_2,..., B_n. // // For example, let A=[0,1,0,1]. Then we have B_1=[0,1,0,1], B_2=[0,1,0,1], B_3=[0,0,1,1], // B_4=[0,0,1,1]. Then C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]. // // You are given C. Determine a binary array A that would give C when processed as above. It is // guaranteed that an array A exists for given C in the input. // // Input // // The first line contains a single integer t (1 <= t <= 1000) -- the number of test cases. // // Each test case has two lines. The first line contains a single integer n (1 <= n <= 2 * 10^5). // // The second line contains n integers c_1, c_2, ..., c_n (0 <= c_i <= n). It is guaranteed that a // valid array A exists for the given C. // // The sum of n over all test cases does not exceed 2 * 10^5. // // Output // // For each test case, output a single line containing n integers a_1, a_2, ..., a_n (a_i is 0 or // 1). If there are multiple answers, you may output any of them. // // Example /* input: 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 output: 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 */ // Note // // Here's the explanation for the first test case. Given that A=[1,1,0,1], we can construct each // B_i: // * B_1=[{1},1,0,1]; // * B_2=[{1},{1},0,1]; // * B_3=[{0},{1},{1},1]; // * B_4=[{0},{1},{1},{1}] And then, we can sum up each column above to get // C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]. // public class C1659D { static final int MOD = 998244353; static final Random RAND = new Random(); static int[] solve(int[] c) { int n = c.length; int[] ans = new int[n]; long sum = 0; for (int v : c) { sum += v; } myAssert(sum % n == 0); // m is total number of 1s int m = (int) (sum / n); // System.out.format("%s %d\n", trace(c), m); LazyRangeSegmentTree st = new LazyRangeSegmentTree(new int[n]); for (int i = n - 1; i >= 0; i--) { int ct = c[i] - st.getAt(i); // System.out.format(" i:%d %d %d ct:%d\n", i, c[i], st.getAt(i), ct); if (m > 0) { st.updateRange(i+1-m, i, 1); } if (ct == i + 1) { ans[i] = 1; m--; } } return ans; } static String trace(int[] a) { return Arrays.toString(a).replace('[', '{').replace(']', '}').replace(" ", ""); } static public class LazyRangeSegmentTree { int n; int tree[]; int lazy[]; LazyRangeSegmentTree(int[] arr) { this.n = arr.length; int m = nextPowerOf2(n); this.tree = new int[m * 2 - 1]; this.lazy = new int[m * 2 - 1]; constructSegTree(arr, 0, n - 1, 0); } void constructSegTree(int arr[], int low, int high, int idx) { if (low > high) { return; } if (low == high) { tree[idx] = arr[low]; return; } int mid = (low + high) / 2; constructSegTree(arr, low, mid, idx * 2 + 1); constructSegTree(arr, mid + 1, high, idx * 2 + 2); tree[idx] = tree[idx * 2 + 1] + tree[idx * 2 + 2]; } // Update by adding delta to all values in [start, end] public void updateRange(int start, int end, int delta) { if (start > end) { return; } updateRangeInner(0, 0, n - 1, start, end, delta); } // idx -> index of current node in segment tree. // low and high -> Starting and ending indexes of elements for which current nodes stores min. // [start, end] indexes of update query. private void updateRangeInner(int idx, int low, int high, int start, int end, int delta) { applyLazy(idx, low, high); if (low > high || low > end || high < start) { return; } if (low >= start && high <= end) { tree[idx] += delta * (high - low + 1); if (low != high) { lazy[idx * 2 + 1] += delta; lazy[idx * 2 + 2] += delta; } return; } int mid = (low + high) / 2; updateRangeInner(idx * 2 + 1, low, mid, start, end, delta); updateRangeInner(idx * 2 + 2, mid + 1, high, start, end, delta); tree[idx] = tree[idx * 2 + 1] + tree[idx * 2 + 2]; } public int getAt(int start) { return getRangeSum(0, 0, n - 1, start, start); } private int getRangeSum(int idx, int low, int high, int start, int end) { applyLazy(idx, low, high); if (low > high || low > end || high < start) { return 0; } if (low >= start && high <= end) { return tree[idx]; } int mid = (low + high) / 2; return getRangeSum(2 * idx + 1, low, mid, start, end) + getRangeSum(2 * idx + 2, mid + 1, high, start, end); } private void applyLazy(int idx, int low, int high) { if (lazy[idx] != 0) { tree[idx] += lazy[idx] * (high - low + 1); if (low != high) { lazy[idx * 2 + 1] += lazy[idx]; lazy[idx * 2 + 2] += lazy[idx]; } lazy[idx] = 0; } } public static int nextPowerOf2(int num){ if (num == 0) { return 1; } if (num > 0 && (num & (num-1)) == 0) { return num; } while ((num & (num-1)) > 0) { num = num & (num - 1); } return num << 1; } } static boolean test = false; static void doTest() { if (!test) { return; } long t0 = System.currentTimeMillis(); System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); int[] c = new int[n]; for (int i = 0; i < n; i++) { c[i] = in.nextInt(); } int[] ans = solve(c); output(ans); } } static void output(int[] a) { StringBuilder sb = new StringBuilder(); for (int v : a) { sb.append(v); sb.append(' '); if (sb.length() > 500) { System.out.print(sb.toString()); sb.setLength(0); } } System.out.println(sb.toString()); } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
9c456cc36cfdf7e5b3d5b7217f283a21
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class D { String filename = null; InputReader sc; void solve() { int n = sc.nextInt(); int[] c = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { c[i] = sc.nextInt(); sum += c[i]; } int nOnes = (int)(sum / n); int nOnesEnd = 0; int nZerosEnd = 0; int[] zeroPositions = new int[n]; int[] a = new int[n]; for (int i = n - 1; i >= 0; i--) { // (i + 1 ... zeros[nOnesBefore] int c0; { int nOnesBefore = nOnes - nOnesEnd; int firstZeroTurn = n; if (nOnesBefore == 0) { firstZeroTurn = i; } else if (nOnesBefore <= nZerosEnd) { firstZeroTurn = zeroPositions[nZerosEnd - nOnesBefore]; } c0 = firstZeroTurn - i; } int c1 = i; { int nOnesBefore = nOnes - nOnesEnd - 1; int firstZeroTurn = n; if (nOnesBefore + 1 <= nZerosEnd) { firstZeroTurn = zeroPositions[nZerosEnd - nOnesBefore - 1]; } c1 += firstZeroTurn - i; } if (c0 == c[i] && nZerosEnd + 1 <= n - nOnes) { a[i] = 0; zeroPositions[nZerosEnd++] = i; } else if (c1 == c[i] && nOnesEnd + 1 <= nOnes ) { a[i] = 1; nOnesEnd++; } else { throw new RuntimeException(); } } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } public void run() throws FileNotFoundException { if (filename == null) { sc = new InputReader(System.in); } else { sc = new InputReader(new FileInputStream(filename)); } int nTests = sc.nextInt(); for (int test = 0; test < nTests; test++) { solve(); } } public static void main(String[] args) { D sol = new D(); try { sol.run(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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 float nextFloat() { return Float.parseFloat(next()); } public double nextDouble() { return Float.parseFloat(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
cf831c02f4669d2b43ed57a6e200501f
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.*; import java.util.*; public class CF1659D extends PrintWriter { CF1659D() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1659D o = new CF1659D(); o.main(); o.flush(); } void main() { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] zz = new int[n]; for (int i = 0; i < n; i++) { int c = sc.nextInt(); zz[i] = n - c; } int[] xx = new int[n]; Arrays.fill(xx, 1); for (int h = 0; h < n; h++) if (zz[h] > 0) { int i = n - zz[h]; if (h < i) { xx[i] = 0; zz[i] -= i; } else xx[h] = 0; } for (int i = 0; i < n; i++) print(xx[i] + " "); println(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
b7d1d92ecc16cd4cead255d0e0ace126
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
// Author : warks import java.util.Map; import java.util.HashMap; public class Main implements Runnable { static ContestScanner in = new ContestScanner(); static ContestPrinter out = new ContestPrinter(); public static void main(String[] args) { new Thread(null, new Main(), "main", 1<<28).start(); } public void run() { int tests = in.nextInt(); for (int t = 0; t < tests; t++) { int n = in.nextInt(); int[] c = in.nextIntArray(n); int[] a = getA(n, c); out.printArray(a); } out.close(); } private static int[] getA(int n, int[] c) { int ones = countOnes(n, c); int[] a = new int[n]; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int subtract = 0; for (int i = n-1; i >= 0; i--) { if (ones == 0) { break; } if (map.containsKey(i)) { subtract -= map.get(i); } int sum = c[i] - subtract; sum -= 1; int num = 1; if (i > 0) { num = sum / i; } a[i] = num; int nextKey = i - ones; if (map.containsKey(nextKey)) { map.put(nextKey, map.get(nextKey) + 1); } else { map.put(nextKey, 1); } subtract++; if (a[i] == 1) { ones--; } } return a; } private static int countOnes(int n, int[] c) { long sum = 0; for (var freq : c) { sum += freq; } return (int) (sum / n); } } // Credits: NASU41 class ContestScanner { private final java.io.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 ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.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 java.util.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 java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char) b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char) b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] 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; } } // Credits: NASU41 class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException{ super(new java.io.PrintStream(file)); } public ContestPrinter(){ 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; if(n==0){ super.println(); return; } 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; if(n==0){ super.println(); return; } 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; if(n==0){ super.println(); return; } 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; if(n==0){ super.println(); return; } 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 <T> void printArray(T[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public <T> void printArray(T[] array){ this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n-1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map){ this.printArray(array, " ", map); } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
d1e89af80d72cc593a48fd2acd78c28c
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.time.Clock; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.function.Supplier; public class Solution { private void solve() throws IOException { int n = nextInt(); int[] a = nextIntArray(n); long c = 0; for (int i : a) { c += i; } c /= n; int d = 0; int[] dd = new int[n]; for (int i = n - 1; i >= 0; i--) { d -= dd[i]; a[i] -= d; d++; if (i - c >= 0) { dd[i - (int) c ]++; } if (a[i] == i + 1) { a[i] = 1; c--; } else { a[i] = 0; } } for (int i : a) { out.print(i + " "); } out.println(); } private static final boolean runNTestsInProd = true; private static final boolean printCaseNumber = false; private static final boolean assertInProd = false; private static final boolean logToFile = false; private static final boolean readFromConsoleInDebug = false; private static final boolean writeToConsoleInDebug = true; private static final boolean testTimer = false; private static Boolean isDebug = null; private BufferedReader in; private StringTokenizer line; private PrintWriter out; public static void main(String[] args) throws Exception { isDebug = Arrays.asList(args).contains("DEBUG_MODE"); if (isDebug) { log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out); clock = Clock.systemDefaultZone(); } new Solution().run(); } private void run() throws Exception { in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt"))); out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt"); try (Timer totalTimer = new Timer("total")) { int t = runNTestsInProd || isDebug ? nextInt() : 1; for (int i = 0; i < t; i++) { if (printCaseNumber) { out.print("Case #" + (i + 1) + ": "); } if (testTimer) { try (Timer testTimer = new Timer("test #" + (i + 1))) { solve(); } } else { solve(); } if (isDebug) { out.flush(); } } } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private char[] nextTokenChars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(message); } } private static <T> void assertNotEqual(T unexpected, T actual) { if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static <T> void assertEqual(T expected, T actual) { if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } private static PrintWriter log = null; private static Clock clock = null; private static void log(Object... objects) { log(true, objects); } private static void logNoDelimiter(Object... objects) { log(false, objects); } private static void log(boolean printDelimiter, Object[] objects) { if (isDebug) { StringBuilder sb = new StringBuilder(); sb.append(LocalDateTime.now(clock)).append(" - "); boolean isFirst = true; for (Object o : objects) { if (!isFirst && printDelimiter) { sb.append(" "); } else { isFirst = false; } sb.append(o.toString()); } log.println(sb); log.flush(); } } private static class Timer implements Closeable { private final String label; private final long startTime = isDebug ? System.nanoTime() : 0; public Timer(String label) { this.label = label; } @Override public void close() throws IOException { if (isDebug) { long executionTime = System.nanoTime() - startTime; String fraction = Long.toString(executionTime / 1000 % 1_000_000); logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's'); } } } private static <T> T timer(String label, Supplier<T> f) throws Exception { if (isDebug) { try (Timer timer = new Timer(label)) { return f.get(); } } else { return f.get(); } } }
Java
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 11
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
9de2bc0f598ef60c972466c84df8398d
train_110.jsonl
1650206100
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.
256 megabytes
import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); long [] T = new long [20]; T[0] = 1; for(int i = 1 ; i < 20 ; i++){ T[i] = T[i-1]*i; // out.printf("T[%d]=%d\n",i,T[i]); } int qq = cin.nextInt(); // int qq = 1; label:while (qq-- > 0) { int n = cin.nextInt(); long []c = new long[n+1]; long []a = new long[n+1]; BIT bit = new BIT(n+1); long total = 0; for(int i = 1 ; i <= n ; i ++){ c[i] = cin.nextLong(); total+=c[i]; bit.add(i,c[i]-c[i-1]); } int cnt = (int)(total/n); // out.println("cnt="+cnt); for(int i = n ; i > 0 ; i--){ long sum = bit.query(i); // out.println("i="+i+" sum="+sum); if(sum>=i){ a[i] = 1; bit.add(i + 1,1); bit.add(i - cnt + 1,-1); cnt--; } } for(int i = 1 ;i <= n ; i ++){ out.printf("%d ",a[i]); } out.println(); } out.close(); } static class BIT { long[] tree;//注意树状数组的数组保存的元素并不一定是单一的元素可能是多个元素之和。 int n; public BIT(int n) { this.n = n; this.tree = new long[n + 1]; } public static int lowbit(int x) { return x & (-x); } public void add(int x, long d) {//单点增加,给序列中的一个数加上d,这个数的下标为x for(;x<=n;x+=lowbit(x)){ tree[x]+=d; } } public int query(int x) {//查询序列中的前缀和。查询范围是[1,x] int ans = 0; for(;x>0;x-=lowbit(x)){ ans+=tree[x]; } return ans; } } 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
["5\n4\n2 4 2 4\n7\n0 3 4 2 3 2 7\n3\n0 0 0\n4\n0 0 0 4\n3\n1 2 3"]
2 seconds
["1 1 0 1 \n0 1 1 0 0 0 1 \n0 0 0 \n0 0 0 1 \n1 0 1"]
NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[\color{blue}{1},1,0,1]$$$; $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$; $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$; $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
Java 17
standard input
[ "constructive algorithms", "data structures", "greedy", "implementation", "math", "two pointers" ]
9dc1bee4e53ced89d827826f2d83dabf
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,900
For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.
standard output
PASSED
666e90e39d6af8de1ba0d0d631adaab7
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
import java.io.*; import java.util.*; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOperator; import java.util.stream.Collectors; public class Main { static In in = new FastIn(); static Out out = new Out(false); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 998244353; void solve() { int n = in.nextInt(); int m = in.nextInt(); List<List<Integer>> edges = new ArrayList<>(); for (int i = 0; i < n; i++) { edges.add(new ArrayList<>()); } UnionFind[] ufs = new UnionFind[30]; for (int i = 0; i < 30; i++) { ufs[i] = new UnionFind(n); } for (int i = 0; i < m; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; edges.get(u).add(v); edges.get(v).add(u); int w = in.nextInt(); for (int j = 0; j < 30; j++) { if ((w >> j & 1) == 1) { ufs[j].unite(u, v); } } } boolean[] ok = new boolean[n]; for (int i = 1; i < 30; i++) { int[] g1 = new int[n]; Arrays.fill(g1, -1); boolean[] two = new boolean[n]; for (int j = 0; j < n; j++) { int ri = ufs[i].root(j); for (int k : edges.get(j)) { int r0 = ufs[0].root(k); if (g1[ri] != r0) { if (g1[ri] != -1) { two[ri] = true; } g1[ri] = r0; } } int r0 = ufs[0].root(j); if (g1[ri] != r0) { if (g1[ri] != -1) { two[ri] = true; } g1[ri] = r0; } } for (int j = 0; j < n; j++) { int ri = ufs[i].root(j); if (two[ri]) { ok[j] = true; } } } int q = in.nextInt(); for (int i = 0; i < q; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; boolean sameAny = false; for (int j = 0; j < 30; j++) { sameAny |= ufs[j].same(u, v); } if (sameAny) { out.println(0); } else if (ok[u]) { out.println(1); } else { out.println(2); } } } class UnionFind { private int size; private final int n; private final int[] parent; private final int[] sizes; public UnionFind(int n) { this.n = n; this.size = n; this.parent = new int[n]; this.sizes = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; sizes[i] = 1; } } public int root(int n) { while (n != parent[n]) { n = parent[n] = parent[parent[n]]; } return n; } public void unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (sizes[x] > sizes[y]) { parent[y] = x; sizes[x] += sizes[y]; } else { parent[x] = y; sizes[y] += sizes[x]; } size--; } } public int size() { return size; } public boolean same(int x, int y) { return root(x) == root(y); } public int getSize(int n) { return sizes[root(n)]; } @Override public String toString() { Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { map.computeIfAbsent(root(i), key -> new ArrayList<>()).add(i); } return new ArrayList<>(map.values()).toString(); } } public static void main(String... args) { new Main().solve(); out.flush(); } } class FastIn extends In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } int nextInt() { long val = nextLong(); if ((int)val != val) { throw new NumberFormatException(); } return (int)val; } long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } } class In { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000); private StringTokenizer tokenizer; String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ignored) { } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char[] nextCharArray() { return next().toCharArray(); } String[] nextStringArray(int n) { String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = next(); } return s; } char[][] nextCharGrid(int n, int m) { char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[] nextIntArray(int n, IntUnaryOperator op) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsInt(nextInt()); } return a; } int[][] nextIntMatrix(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = nextIntArray(w); } return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long[] nextLongArray(int n, LongUnaryOperator op) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsLong(nextLong()); } return a; } long[][] nextLongMatrix(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLongArray(w); } return a; } List<List<Integer>> nextEdges(int n, int m, boolean directed) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; res.get(u).add(v); if (!directed) { res.get(v).add(u); } } return res; } } class Out { private final PrintWriter out = new PrintWriter(System.out); private final PrintWriter err = new PrintWriter(System.err); boolean autoFlush = false; boolean enableDebug; Out(boolean enableDebug) { this.enableDebug = enableDebug; } void println(Object... args) { if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } out.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); if (autoFlush) { out.flush(); } } void debug(Object... args) { if (!enableDebug) { return; } if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } err.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); err.flush(); } void println(char[] s) { out.println(String.valueOf(s)); if (autoFlush) { out.flush(); } } void println(int[] a) { StringJoiner joiner = new StringJoiner(" "); for (int i : a) { joiner.add(Integer.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void println(long[] a) { StringJoiner joiner = new StringJoiner(" "); for (long i : a) { joiner.add(Long.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void flush() { err.flush(); out.flush(); } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 17
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
c1eb02596fec0a7be2bb339dd423d26d
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
//package com.example.practice.codeforces.sc2200; 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; //E. AND-MEX Walk public class Solution13 { final static int H = 30; public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); StringTokenizer st = new StringTokenizer(input.readLine()); final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()); int[][] es = new int[m][]; for (int i=0;i<m;++i){ st = new StringTokenizer(input.readLine()); es[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; } int q = Integer.parseInt(input.readLine()); int[][] qs = new int[q][]; for (int i=0;i<q;++i){ st = new StringTokenizer(input.readLine()); qs[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; } int[] res = calc(n, m, es, qs); for (int a : res){ out.println(a); } out.close(); // close the output file } private static int[] calc(final int n, final int m, final int[][] es, final int[][] qs) { final ArrayList<int[]>[] als = new ArrayList[n+1]; for (int[] e : es){ if (als[e[0]]==null)als[e[0]]=new ArrayList<>(); if (als[e[1]]==null)als[e[1]]=new ArrayList<>(); als[e[0]].add(new int[]{e[1], e[2]}); als[e[1]].add(new int[]{e[0], e[2]}); } int[] res = new int[qs.length]; Arrays.fill(res, -1); for (int i=0;i<H;++i){ check0(n, als, qs, res, i); } for (int i=1;i<H;++i){ check1(n, als, qs, res, i, es); } for (int i=0;i<res.length;++i){ if (res[i]==-1){ res[i] = 2; } } return res; } private static void check1(final int n, final ArrayList<int[]>[] als, final int[][] qs, final int[] res, final int h, int[][] es){ int[] rd = new int[n+1]; for (int i=1;i<=n;++i){ if (rd[i]==0){ dfs(als, i, i, rd, (1<<h)+1); } } boolean[] hasOutLet = new boolean[n+1]; for (int[] e : es){ if ((e[2]&1) == 0){ hasOutLet[rd[e[0]]] = true; hasOutLet[rd[e[1]]] = true; } } for (int i=0;i<qs.length;++i){ if(res[i]==-1){ if (hasOutLet[rd[qs[i][0]]]){ res[i] = 1; } } } } private static void check0(final int n, final ArrayList<int[]>[] als, final int[][] qs, final int[] res, final int h){ int[] rd = new int[n+1]; for (int i=1;i<=n;++i){ if (rd[i]==0){ dfs(als, i, i, rd, 1<<h); } } for (int i=0;i<qs.length;++i){ if(res[i]==-1){ if (rd[qs[i][0]] == rd[qs[i][1]]){ res[i] = 0; } } } } private static void dfs(final ArrayList<int[]>[] als, int src, int idx, int[] rd, final int v){ rd[idx] = src; for (int[] kk : als[idx]){ if (rd[kk[0]]==0 && (kk[1]&v)==v){ dfs(als, src, kk[0], rd, v); } } } /*private static int find(int[] rd, int idx){ while (idx != rd[idx]){ rd[idx] = rd[rd[idx]]; idx = rd[idx]; } return idx; }*/ }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
1e8748fe05dd5b0ffa625d862c597fde
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
//package com.example.practice.codeforces.sc2200; 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; //E. AND-MEX Walk public class Solution13 { final static int H = 30; public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); StringTokenizer st = new StringTokenizer(input.readLine()); final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()); int[][] es = new int[m][]; for (int i=0;i<m;++i){ st = new StringTokenizer(input.readLine()); es[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; } int q = Integer.parseInt(input.readLine()); int[][] qs = new int[q][]; for (int i=0;i<q;++i){ st = new StringTokenizer(input.readLine()); qs[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; } int[] res = calc(n, m, es, qs); for (int a : res){ out.println(a); } out.close(); // close the output file } private static int[] calc(final int n, final int m, final int[][] es, final int[][] qs) { final ArrayList<int[]>[] als = new ArrayList[n+1]; for (int[] e : es){ if (als[e[0]]==null)als[e[0]]=new ArrayList<>(); if (als[e[1]]==null)als[e[1]]=new ArrayList<>(); als[e[0]].add(new int[]{e[1], e[2]}); als[e[1]].add(new int[]{e[0], e[2]}); } int[] res = new int[qs.length]; Arrays.fill(res, -1); for (int i=0;i<H;++i){ check0(n, als, qs, res, i); } for (int i=1;i<H;++i){ check1(n, als, qs, res, i, es); } for (int i=0;i<res.length;++i){ if (res[i]==-1){ res[i] = 2; } } return res; } private static void check1(final int n, final ArrayList<int[]>[] als, final int[][] qs, final int[] res, final int h, int[][] es){ int[] rd = new int[n+1]; for (int i=1;i<=n;++i){ if (rd[i]==0){ dfs(als, i, i, rd, (1<<h)+1); } } boolean[] hasOutLet = new boolean[n+1]; for (int[] e : es){ if ((e[2]&1) == 0){ hasOutLet[find(rd, e[0])] = true; hasOutLet[find(rd, e[1])] = true; } } for (int i=0;i<qs.length;++i){ if(res[i]==-1){ if (hasOutLet[find(rd, qs[i][0])]){ res[i] = 1; } } } } private static void check0(final int n, final ArrayList<int[]>[] als, final int[][] qs, final int[] res, final int h){ int[] rd = new int[n+1]; for (int i=1;i<=n;++i){ if (rd[i]==0){ dfs(als, i, i, rd, 1<<h); } } for (int i=0;i<qs.length;++i){ if(res[i]==-1){ if (find(rd, qs[i][0]) == find(rd, qs[i][1])){ res[i] = 0; } } } } private static void dfs(final ArrayList<int[]>[] als, int src, int idx, int[] rd, final int v){ rd[idx] = src; for (int[] kk : als[idx]){ if (rd[kk[0]]==0 && (kk[1]&v)==v){ dfs(als, src, kk[0], rd, v); } } } private static int find(int[] rd, int idx){ while (idx != rd[idx]){ rd[idx] = rd[rd[idx]]; idx = rd[idx]; } return idx; } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
637214ce82f15d7a5a4b57289a1a277e
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
import java.io.*; import java.util.*; public class andMexWalk { static int[][] joyce_qu; static int[][] joyce_qu_very_cute; static boolean[][] joyce_qu_very_cute_confirmed; public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); joyce_qu = new int[n][30]; joyce_qu_very_cute = new int[n][30]; joyce_qu_very_cute_confirmed = new boolean[n][30]; for(int i=0; i<n; i++) for(int j=0; j<30; j++) { joyce_qu[i][j] = i; joyce_qu_very_cute[i][j] = i; } for(int i=0; i<m; i++) { st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1; int val = Integer.parseInt(st.nextToken()); for(int j=0; j<30; j++) { if((val&(1<<j))==(1<<j)) { int r1 = find_joyce_qu(a,j); int r2 = find_joyce_qu(b,j); joyce_qu[r2][j] = r1; } } if((val&1)==1) { for(int j=1; j<30; j++) { if((val&(1<<j))==(1<<j)) { int r1 = find_Joyce_Qu(a,j); int r2 = find_Joyce_Qu(b,j); joyce_qu_very_cute[r2][j] = r1; if(joyce_qu_very_cute_confirmed[r2][j]&&(!joyce_qu_very_cute_confirmed[r1][j])) joyce_qu_very_cute_confirmed[r1][j] = true; } } } else { for(int j=0; j<30; j++) { int r1 = find_Joyce_Qu(a,j); int r2 = find_Joyce_Qu(b,j); joyce_qu_very_cute_confirmed[r1][j] = true; joyce_qu_very_cute_confirmed[r2][j] = true; } } } int q = Integer.parseInt(br.readLine()); for(int i=0; i<q; i++) { st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1; boolean joyce = false; for(int j=0; j<30; j++) if(find_joyce_qu(a, j)==find_joyce_qu(b,j)) { joyce = true; break; } if(joyce) { out.write(0+"\n"); continue; } for(int j=1; j<30; j++) { if(joyce_qu_very_cute_confirmed[find_Joyce_Qu(a, j)][j]) { joyce = true; break; } } if(joyce) out.write(1+"\n"); else out.write(2+"\n"); } out.flush(); out.close(); } static int find_joyce_qu(int pos, int bit) { if(joyce_qu[pos][bit]==pos) return pos; return joyce_qu[pos][bit]=find_joyce_qu(joyce_qu[pos][bit], bit); } static int find_Joyce_Qu(int pos, int bit) { if(joyce_qu_very_cute[pos][bit]==pos) return pos; return joyce_qu_very_cute[pos][bit]=find_Joyce_Qu(joyce_qu_very_cute[pos][bit], bit); } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
f8bdcc72f9dd1a822cdff8eaac3eddd9
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class E { static ArrayList<int[]>[]adj; static int[]dp,in,ot; public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(),m=sc.nextInt(); UnionFind[]ufs=new UnionFind[30]; UnionFind[]ufs2=new UnionFind[30]; for(int i=0;i<ufs.length;i++)ufs[i]=new UnionFind(n); for(int i=0;i<ufs2.length;i++)ufs2[i]=new UnionFind(n); boolean[]even=new boolean[n]; HashSet<Integer>[]arr=new HashSet[n]; adj=new ArrayList[n]; for(int i=0;i<n;i++) { arr[i]=new HashSet<Integer>(); adj[i]=new ArrayList<int[]>(); } for(int i=0;i<m;i++) { int x=sc.nextInt()-1,y=sc.nextInt()-1,w=sc.nextInt(); arr[x].add(w);arr[y].add(w); adj[x].add(new int[] {y,w}); adj[y].add(new int[] {x,w}); for(int j=0;j<ufs.length;j++) { if(((1<<j)&w)!=0)ufs[j].unionSet(x, y); } for(int j=1;j<ufs2.length;j++) { if(((1<<j)&w)!=0)ufs2[j].unionSet(x, y); } } for(int i=1;i<ufs2.length;i++) { boolean[]vis=new boolean[n]; for(int u=0;u<n;u++) { ArrayList<int[]>edges=adj[u]; for(int[]e:edges) { int v=e[0],w=e[1]; if(w%2==0) { vis[ufs2[i].findSet(u)]=true; vis[ufs2[i].findSet(v)]=true; } } } for(int j=0;j<n;j++) { if(vis[ufs2[i].findSet(j)]) { even[j]=true; } } } int q=sc.nextInt(); a:while(q-->0) { int u=sc.nextInt()-1,v=sc.nextInt()-1; for(int i=0;i<ufs.length;i++) { if(ufs[i].isSameSet(u, v)) { out.println(0); continue a; } } if(even[u]) { out.println(1);continue; } for(int i=1;i<ufs2.length;i++) { if(ufs2[i].isSameSet(u, v)) { out.println(1);continue a; } } out.println(2); // out.println(1); //all ufs do not include a connection of u,v //distinguish bet 1,2? //opt for 1, how? try to prove that a 2 exists->1, else it's a 2 // } out.close(); } static class UnionFind { int[] p, rank, setSize; int numSets; public UnionFind(int N) { p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; //if(numSets==1)total=0; int x = findSet(i), y = findSet(j); if(rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if(rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
f009d6824c1186d283c564d60afde4f9
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run() { work(); out.flush(); } long mod=998244353; long gcd(long a,long b) { return a==0?b:gcd(b%a,a); } final long inf=Long.MAX_VALUE/3; int n,m; boolean[] rec; int[][] id; int[][] id2;//检查1 void work(){ n=ni(); rec=new boolean[n]; id=new int[31][n]; id2=new int[31][n+1]; for(int i=0;i<31;i++){ for(int j=0;j<n;j++){ id[i][j]=j; } for(int j=0;j<=n;j++){ id2[i][j]=j; } } for(int m=ni();m>0;m--){ int s=ni()-1,e=ni()-1,w=ni(); for(int i=0;i<31;i++){ if((1<<i&w)>0){ union(s,e,i); } } for(int i=1;i<31;i++){//检查除第一位之外的位 if(w%2==0){ union2(s,n,i); union2(e,n,i); }else if((1<<i&w)>0){ union2(s,e,i); } } } out: for(int q=ni();q>0;q--){ int s=ni()-1,e=ni()-1; for(int i=0;i<31;i++){ if(find(i,s)==find(i,e)){ out.println(0); continue out; } } for(int i=1;i<31;i++){ if(find2(i,s)==find2(i,n)){ out.println(1); continue out; } } out.println(2); } } private void union(int p, int q,int i) { int proot=find(i,p); int qroot=find(i,q); id[i][proot]=qroot; } private int find(int i,int p) { if(id[i][p]!=p){ id[i][p]=find(i,id[i][p]); } return id[i][p]; } private void union2(int p, int q,int i) { int proot=find2(i,p); int qroot=find2(i,q); id2[i][proot]=qroot; } private int find2(int i,int p) { if(id2[i][p]!=p){ id2[i][p]=find2(i,id2[i][p]); } return id2[i][p]; } @SuppressWarnings("unused") private ArrayList<Integer>[] ng(int n, int m) { ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { int s=in.nextInt()-1,e=in.nextInt()-1; graph[s].add(e); graph[e].add(s); } return graph; } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong(); graph[(int)s].add(new long[] {e,w}); graph[(int)e].add(new long[] {s,w}); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private double nd() { return in.nextDouble(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A=new long[n]; for(int i=0;i<n;i++) { A[i]=in.nextLong(); } return A; } private int[] nia(int n) { int[] A=new int[n]; for(int i=0;i<n;i++) { A[i]=in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; InputStreamReader input;//no buffer public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public FastReader(boolean isBuffer) { if(!isBuffer){ input=new InputStreamReader(System.in); }else{ br=new BufferedReader(new InputStreamReader(System.in)); } } public boolean hasNext(){ try{ String s=br.readLine(); if(s==null){ return false; } st=new StringTokenizer(s); }catch(IOException e){ e.printStackTrace(); } return true; } public String next() { if(input!=null){ try { StringBuilder sb=new StringBuilder(); int ch=input.read(); while(ch=='\n'||ch=='\r'||ch==32){ ch=input.read(); } while(ch!='\n'&&ch!='\r'&&ch!=32){ sb.append((char)ch); ch=input.read(); } return sb.toString(); }catch (Exception e){ e.printStackTrace(); } } while(st==null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return (int)nextLong(); } public long nextLong() { try { if(input!=null){ long ret=0; int b=input.read(); while(b<'0'||b>'9'){ b=input.read(); } while(b>='0'&&b<='9'){ ret=ret*10+(b-'0'); b=input.read(); } return ret; } }catch (Exception e){ e.printStackTrace(); } return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
5da2e64a1c2fe318e85d4e5b7f562b07
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
//make sure to make new file! import java.io.*; import java.util.*; public class E782{ public static ArrayList<ArrayList<Edge>> adj; public static int[][] comp; public static int curcomp; public static boolean[] seen; public static void main(String[] args)throws IOException{ FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = fs.nextInt(); int m = fs.nextInt(); adj = new ArrayList<ArrayList<Edge>>(n+1); for(int k = 0; k <= n; k++) adj.add(new ArrayList<Edge>()); //adjacent to even edge HashSet<Integer> adeven = new HashSet<Integer>(); for(int k = 0; k < m; k++){ int a = fs.nextInt(); int b = fs.nextInt(); int w = fs.nextInt(); adj.get(a).add(new Edge(b,w)); adj.get(b).add(new Edge(a,w)); if(w%2 == 0){ adeven.add(a); adeven.add(b); } } //precompute comps comp = new int[n+1][30]; for(int k = 0; k < 30; k++){ curcomp = 1; seen = new boolean[n+1]; for(int v = 1; v <= n; v++){ if(seen[v]) continue; dfs(v,k); curcomp++; } } //true if that node can each an even number without getting to and of 1 (means you can get a mex of 1) boolean[] can1 = new boolean[n+1]; for(int x = 1; x < 30; x++){ //start at 1 because you need a bit other than 1 to be true Queue<Integer> q = new LinkedList<Integer>(); seen = new boolean[n+1]; for(int i : adeven){ q.add(i); seen[i] = true; } while(!q.isEmpty()){ int v = q.poll(); can1[v] = true; seen[v] = true; for(Edge e : adj.get(v)){ if((e.w & (1 << x)) == 0) continue; if(seen[e.to]) continue; q.add(e.to); } } } int Q = fs.nextInt(); StringJoiner sj = new StringJoiner("\n"); for(int q = 0; q < Q; q++){ int u = fs.nextInt(); int v = fs.nextInt(); //check 0 boolean check0 = false; for(int k = 0; k < 30; k++){ if(comp[u][k] == comp[v][k]){ check0 = true; break; } } if(check0){ sj.add("0"); continue; } if(can1[u]){ sj.add("1"); } else { sj.add("2"); } } out.println(sj.toString()); out.close(); } public static void dfs(int v, int x){ comp[v][x] = curcomp; seen[v] = true; for(Edge e : adj.get(v)){ if((e.w & (1 << x)) == 0) continue; if(seen[e.to]) continue; dfs(e.to,x); } } public static class Edge{ int to; int w; public Edge(int a, int b){ to = a; w = b; } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N+1]; for (int i = 1; i <= N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N+1]; for (int i = 1; i <= N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
1a55e9651f073bb9b33a9ca1f961264f
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class NewTimeE { static Edge[][] edges; public static void main(String hi[]) throws Exception { FastScanner infile = new FastScanner(); int N = infile.nextInt(); int M = infile.nextInt(); edges = new Edge[N][]; int[] freq = new int[N]; int[] input = new int[3*M]; for(int i=2; i < 3*M; i+=3) { int a = infile.nextInt()-1; int b = infile.nextInt()-1; int w = infile.nextInt(); freq[a]++; freq[b]++; input[i-2] = a; input[i-1] = b; input[i] = w; } for(int i=0; i < N; i++) edges[i] = new Edge[freq[i]]; for(int i=2; i < 3*M; i+=3) { int a = input[i-2]; int b = input[i-1]; int w = input[i]; edges[a][--freq[a]] = new Edge(b, w); edges[b][--freq[b]] = new Edge(a, w); } boolean[] even = new boolean[N]; DSU[] unionSingle = new DSU[30]; for(int b=0; b < 30; b++) { DSU union = new DSU(N); for(int v=0; v < N; v++) for(Edge e: edges[v]) { if((e.weight&(1<<b)) > 0 && union.find(v) != union.find(e.to)) union.merge(v, e.to); if((e.weight&1) == 0) even[v] = even[e.to] = true; } unionSingle[b] = union; } specialDSU[] unionOdd = new specialDSU[30]; for(int b=1; b < 30; b++) { specialDSU union = new specialDSU(N, even); for(int v=0; v < N; v++) for(Edge e: edges[v]) if((e.weight&1) > 0 && (e.weight&(1<<b)) > 0 && union.find(v) != union.find(e.to)) union.merge(v, e.to); unionOdd[b] = union; } int Q = infile.nextInt(); StringBuilder sb = new StringBuilder(); while(Q-->0) { int x = infile.nextInt()-1; int y = infile.nextInt()-1; int res = 2; for(int b=0; b < 30; b++) if(unionSingle[b].find(x) == unionSingle[b].find(y)) { res = 0; break; } if(res == 2) { for(int b=1; b < 30; b++) if(unionOdd[b].even[unionOdd[b].find(x)]) { res = 1; break; } } sb.append(res).append("\n"); } System.out.print(sb); } } class Edge { public int to; public int weight; public Edge(int a, int b) { to = a; weight = b; } } class DSU { public int[] dsu; public DSU(int N) { dsu = new int[N+1]; for(int i=0; i <= N; i++) dsu[i] = i; } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } } class specialDSU { public int[] dsu; public boolean[] even; public specialDSU(int N, boolean[] b) { dsu = new int[N+1]; even = new boolean[N]; for(int i=0; i < N; i++) { dsu[i] = i; even[i] = b[i]; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; even[fy] |= even[fx]; } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
f7316aa74c68b1bef942c104c1e745f8
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class E { FastScanner in; PrintWriter out; boolean systemIO = true; public class DSU { int[] sz; int[] p; public DSU(int n) { sz = new int[n]; p = new int[n]; for (int i = 0; i < p.length; i++) { p[i] = i; sz[i] = 1; } } public int get(int x) { if (x == p[x]) { return x; } int par = get(p[x]); p[x] = par; return par; } public boolean unite(int a, int b) { int pa = get(a); int pb = get(b); if (pa == pb) { return false; } if (sz[pa] < sz[pb]) { p[pa] = pb; sz[pb] += sz[pa]; } else { p[pb] = pa; sz[pa] += sz[pb]; } return true; } } public class SegmentTreeAdd { int pow; long[] max; long[] delta; boolean[] flag; public SegmentTreeAdd(long[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; max = new long[2 * pow]; delta = new long[2 * pow]; for (int i = 0; i < max.length; i++) { max[i] = Long.MIN_VALUE / 2; } for (int i = 0; i < a.length; i++) { max[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { max[i] = f(max[2 * i], max[2 * i + 1]); } } public long get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l > r) { return Long.MIN_VALUE / 2; } if (l == tl && r == tr) { return max[v]; } int tm = (tl + tr) / 2; return f(get(2 * v, tl, tm, l, Math.min(r, tm)), get(2 * v + 1, tm + 1, tr, Math.max(l, tm + 1), r)); } public void set(int v, int tl, int tr, int l, int r, long x) { push(v, tl, tr); if (l > tr || r < tl) { return; } if (l <= tl && r >= tr) { delta[v] += x; flag[v] = true; push(v, tl, tr); return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm + 1, tr, l, r, x); max[v] = f(max[2 * v], max[2 * v + 1]); } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] += delta[v]; delta[2 * v + 1] += delta[v]; } flag[v] = false; max[v] += delta[v]; delta[v] = 0; } } public long f(long a, long b) { return Math.max(a, b); } } public class SegmentTreeSet { int pow; int[] sum; int[] delta; boolean[] flag; public SegmentTreeSet(int[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; sum = new int[2 * pow]; delta = new int[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { sum[i] = f(sum[2 * i], sum[2 * i + 1]); } } public int get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l > r) { return 0; } if (l == tl && r == tr) { return sum[v]; } int tm = (tl + tr) / 2; return f(get(2 * v, tl, tm, l, Math.min(r, tm)), get(2 * v + 1, tm + 1, tr, Math.max(l, tm + 1), r)); } public void set(int v, int tl, int tr, int l, int r, int x) { push(v, tl, tr); if (l > tr || r < tl) { return; } if (l <= tl && r >= tr) { delta[v] = x; flag[v] = true; push(v, tl, tr); return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm + 1, tr, l, r, x); sum[v] = f(sum[2 * v], sum[2 * v + 1]); } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] = delta[v]; delta[2 * v + 1] = delta[v]; } flag[v] = false; sum[v] = delta[v] * (tr - tl + 1); } } public int f(int a, int b) { return a + b; } } public class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public Pair clone() { return new Pair(x, y); } public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { if (x > o.x) { return 1; } if (x < o.x) { return -1; } if (y > o.y) { return 1; } if (y < o.y) { return -1; } return 0; } } long mod = 1000000007; Random random = new Random(); public void shuffle(Pair[] a) { for (int i = 0; i < a.length; i++) { int x = random.nextInt(i + 1); Pair t = a[x]; a[x] = a[i]; a[i] = t; } } public void sort(int[][] a) { for (int i = 0; i < a.length; i++) { Arrays.sort(a[i]); } } public void add(Map<Long, Integer> map, long l) { if (map.containsKey(l)) { map.put(l, map.get(l) + 1); } else { map.put(l, 1); } } public void remove(Map<Integer, Integer> map, Integer s) { if (map.get(s) > 1) { map.put(s, map.get(s) - 1); } else { map.remove(s); } } long max = Long.MAX_VALUE / 2; double eps = 1e-10; public int signum(double x) { if (x > eps) { return 1; } if (x < -eps) { return -1; } return 0; } public long abs(long x) { return x < 0 ? -x : x; } public long min(long x, long y) { return x < y ? x : y; } public long max(long x, long y) { return x > y ? x : y; } public long gcd(long x, long y) { while (y > 0) { long c = y; y = x % y; x = c; } return x; } public final Vector ZERO = new Vector(0, 0); // public class Vector implements Comparable<Vector> { // long x; // long y; // int type; // int number; // // public Vector() { // x = 0; // y = 0; // } // // public Vector(long x, long y, int type, int number) { // this.x = x; // this.y = y; // this.type = type; // this.number = number; // } // // public Vector(long x, long y) { // // } // // public Vector(Point begin, Point end) { // this(end.x - begin.x, end.y - begin.y); // } // // public void orient() { // if (x < 0) { // x = -x; // y = -y; // } // if (x == 0 && y < 0) { // y = -y; // } // } // // public void normalize() { // long gcd = gcd(abs(x), abs(y)); // x /= gcd; // y /= gcd; // } // // public String toString() { // return x + " " + y; // } // // public boolean equals(Vector v) { // return x == v.x && y == v.y; // } // // public boolean collinear(Vector v) { // return cp(this, v) == 0; // } // // public boolean orthogonal(Vector v) { // return dp(this, v) == 0; // } // // public Vector ort(Vector v) { // return new Vector(-y, x); // } // // public Vector add(Vector v) { // return new Vector(x + v.x, y + v.y); // } // // public Vector multiply(long c) { // return new Vector(c * x, c * y); // } // // public int quater() { // if (x > 0 && y >= 0) { // return 1; // } // if (x <= 0 && y > 0) { // return 2; // } // if (x < 0) { // return 3; // } // return 0; // } // // public long len2() { // return x * x + y * y; // } // // @Override // public int compareTo(Vector o) { // if (quater() != o.quater()) { // return quater() - o.quater(); // } // return signum(cp(o, this)); // } // } // public long dp(Vector v1, Vector v2) { // return v1.x * v2.x + v1.y * v2.y; // } // // public long cp(Vector v1, Vector v2) { // return v1.x * v2.y - v1.y * v2.x; // } // public class Line implements Comparable<Line> { // Point p; // Vector v; // // public Line(Point p, Vector v) { // this.p = p; // this.v = v; // } // // public Line(Point p1, Point p2) { // if (p1.compareTo(p2) < 0) { // p = p1; // v = new Vector(p1, p2); // } else { // p = p2; // v = new Vector(); // } // } // // public boolean collinear(Line l) { // return v.collinear(l.v); // } // // public boolean inLine(Point p) { // return hv(p) == 0; // } // // public boolean inSegment(Point p) { // if (!inLine(p)) { // return false; // } // Point p1 = p; // Point p2 = p.add(v); // return p1.x <= p.x && p.x <= p2.x && min(p1.y, p2.y) <= p.y && p.y <= // max(p1.y, p2.y); // } // // public boolean equalsSegment(Line l) { // return p.equals(l.p) && v.equals(l.v); // } // // public boolean equalsLine(Line l) { // return collinear(l) && inLine(l.p); // } // // public long hv(Point p) { // Vector v1 = new Vector(this.p, p); // return cp(v, v1); // } // // public double h(Point p) { // Vector v1 = new Vector(this.p, p); // return cp(v, v1) / Math.sqrt(v.len2()); // } // // public long[] intersectLines(Line l) { // if (collinear(l)) { // return null; // } // long[] ans = new long[4]; // // return ans; // } // // public long[] intersectSegment(Line l) { // long[] ans = intersectLines(l); // if (ans == null) { // return null; // } // Point p1 = p; // Point p2 = p.add(v); // boolean f1 = p1.x * ans[1] <= ans[0] && ans[0] <= p2.x * ans[1] && min(p1.y, // p2.y) * ans[3] <= ans[2] // && ans[2] <= max(p1.y, p2.y) * ans[3]; // p1 = l.p; // p2 = l.p.add(v); // boolean f2 = p1.x * ans[1] <= ans[0] && ans[0] <= p2.x * ans[1] && min(p1.y, // p2.y) * ans[3] <= ans[2] // && ans[2] <= max(p1.y, p2.y) * ans[3]; // if (!f1 || !f2) { // return null; // } // return ans; // } // // @Override // public int compareTo(Line o) { // return v.compareTo(o.v); // } // } public class Rect { long x1; long x2; long y1; long y2; int number; public Rect(long x1, long x2, long y1, long y2, int number) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.number = number; } } public static class Fenvik { int[] t; public Fenvik(int n) { t = new int[n]; } public void add(int x, int delta) { for (int i = x; i < t.length; i = (i | (i + 1))) { t[i] += delta; } } private int sum(int r) { int ans = 0; int x = r; while (x >= 0) { ans += t[x]; x = (x & (x + 1)) - 1; } return ans; } public int sum(int l, int r) { return sum(r) - sum(l - 1); } } public class SegmentTreeMaxSum { int pow; int[] sum; int[] maxPrefSum; int[] maxSufSum; int[] maxSum; public SegmentTreeMaxSum(int[] a) { pow = 1; while (pow < a.length) { pow *= 2; } sum = new int[2 * pow]; maxPrefSum = new int[2 * pow]; maxSum = new int[2 * pow]; maxSufSum = new int[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; maxSum[pow + i] = Math.max(a[i], 0); maxPrefSum[pow + i] = maxSum[pow + i]; maxSufSum[pow + i] = maxSum[pow + i]; } for (int i = pow - 1; i > 0; i--) { update(i); } } public int[] get(int v, int tl, int tr, int l, int r) { if (r <= tl || l >= tr) { int[] ans = { 0, 0, 0, 0 }; return ans; } if (l <= tl && r >= tr) { int[] ans = { maxPrefSum[v], maxSum[v], maxSufSum[v], sum[v] }; return ans; } int tm = (tl + tr) / 2; int[] left = get(2 * v, tl, tm, l, r); int[] right = get(2 * v + 1, tm, tr, l, r); int[] ans = { Math.max(left[0], right[0] + left[3]), Math.max(left[1], Math.max(right[1], left[2] + right[0])), Math.max(right[2], left[2] + right[3]), left[3] + right[3] }; return ans; } public void set(int v, int tl, int tr, int x, int value) { if (v >= pow) { sum[v] = value; maxSum[v] = Math.max(value, 0); maxPrefSum[v] = maxSum[v]; maxSufSum[v] = maxSum[v]; return; } int tm = (tl + tr) / 2; if (x < tm) { set(2 * v, tl, tm, x, value); } else { set(2 * v + 1, tm, tr, x, value); } update(v); } public void update(int i) { sum[i] = f(sum[2 * i], sum[2 * i + 1]); maxSum[i] = Math.max(maxSum[2 * i], Math.max(maxSum[2 * i + 1], maxSufSum[2 * i] + maxPrefSum[2 * i + 1])); maxPrefSum[i] = Math.max(maxPrefSum[2 * i], maxPrefSum[2 * i + 1] + sum[2 * i]); maxSufSum[i] = Math.max(maxSufSum[2 * i + 1], maxSufSum[2 * i] + sum[2 * i + 1]); } public int f(int a, int b) { return a + b; } } public class Point implements Comparable<Point> { double x; double y; public Point() { x = 0; y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Point p) { return x == p.x && y == p.y; } public double dist2() { return x * x + y * y; } public Point add(Point v) { return new Point(x + v.x, y + v.y); } @Override public int compareTo(Point o) { int z = signum(x + y - o.x - o.y); if (z != 0) { return z; } return signum(x - o.x) != 0 ? signum(x - o.x) : signum(y - o.y); } } public class Circle implements Comparable<Circle> { Point p; int r; public Circle(Point p, int r) { this.p = p; this.r = r; } public Point angle() { double z = r / sq2; z -= z % 1e-5; return new Point(p.x - z, p.y - z); } public boolean inside(Point p) { return hypot2(p.x - this.p.x, p.y - this.p.y) <= sq(r); } @Override public int compareTo(Circle o) { Point a = angle(); Point oa = o.angle(); int z = signum(a.x + a.y - oa.x - oa.y); if (z != 0) { return z; } return signum(a.y - oa.y); } } public class Fraction implements Comparable<Fraction> { long x; long y; public Fraction(long x, long y, boolean needNorm) { this.x = x; this.y = y; if (y < 0) { this.x *= -1; this.y *= -1; } if (needNorm) { long gcd = gcd(this.x, this.y); this.x /= gcd; this.y /= gcd; } } public Fraction clone() { return new Fraction(x, y, false); } public String toString() { return x + "/" + y; } @Override public int compareTo(Fraction o) { long res = x * o.y - y * o.x; if (res > 0) { return 1; } if (res < 0) { return -1; } return 0; } } public double sq(double x) { return x * x; } public long sq(long x) { return x * x; } public double hypot2(double x, double y) { return sq(x) + sq(y); } public long hypot2(long x, long y) { return sq(x) + sq(y); } public boolean kuhn(int v, int[][] edge, boolean[] used, int[] mt) { used[v] = true; for (int u : edge[v]) { if (mt[u] < 0 || (!used[mt[u]] && kuhn(mt[u], edge, used, mt))) { mt[u] = v; return true; } } return false; } public int matching(int[][] edge) { int n = edge.length; int[] mt = new int[n]; Arrays.fill(mt, -1); boolean[] used = new boolean[n]; int ans = 0; for (int i = 0; i < n; i++) { if (!used[i] && kuhn(i, edge, used, mt)) { Arrays.fill(used, false); ans++; } } return ans; } double sq2 = Math.sqrt(2); int small = 20; public class MyStack { int[] st; int sz; public MyStack(int n) { this.st = new int[n]; sz = 0; } public boolean isEmpty() { return sz == 0; } public int peek() { return st[sz - 1]; } public int pop() { sz--; return st[sz]; } public void clear() { sz = 0; } public void add(int x) { st[sz++] = x; } public int get(int x) { return st[x]; } } public int[][] readGraph(int n, int m) { int[][] to = new int[n][]; int[] sz = new int[n]; int[] x = new int[m]; int[] y = new int[m]; int[] siz = new int[m]; for (int i = 0; i < m; i++) { x[i] = in.nextInt() - 1; y[i] = in.nextInt() - 1; siz[i] = in.nextInt(); sz[x[i]]++; sz[y[i]]++; } for (int i = 0; i < to.length; i++) { to[i] = new int[sz[i]]; sz[i] = 0; } for (int i = 0; i < x.length; i++) { to[x[i]][sz[x[i]]] = y[i]; size[x[i]][sz[x[i]]++] = siz[i]; to[y[i]][sz[y[i]]] = x[i]; size[y[i]][sz[y[i]]++] = siz[i]; } return to; } public class Pol { double[] coeff; public Pol(double[] coeff) { this.coeff = coeff; } public Pol mult(Pol p) { double[] ans = new double[coeff.length + p.coeff.length - 1]; for (int i = 0; i < ans.length; i++) { for (int j = Math.max(0, i - p.coeff.length + 1); j < coeff.length && j <= i; j++) { ans[i] += coeff[j] * p.coeff[i - j]; } } return new Pol(ans); } public String toString() { String ans = ""; for (int i = 0; i < coeff.length; i++) { ans += coeff[i] + " "; } return ans; } public double value(double x) { double ans = 0; double p = 1; for (int i = 0; i < coeff.length; i++) { ans += coeff[i] * p; p *= x; } return ans; } public double integrate(double r) { Pol p = new Pol(new double[coeff.length + 1]); for (int i = 0; i < coeff.length; i++) { p.coeff[i + 1] = coeff[i] / fact[i + 1]; } return p.value(r); } public double integrate(double l, double r) { return integrate(r) - integrate(l); } } double[] fact = new double[100]; public class SparseTable { int pow; int[] lessPow; int[][] min; public SparseTable(int[] a) { pow = 0; while ((1 << pow) <= a.length) { pow++; } min = new int[pow][a.length]; for (int i = 0; i < a.length; i++) { min[0][i] = a[i]; } for (int i = 1; i < pow; i++) { for (int j = 0; j < a.length; j++) { min[i][j] = min[i - 1][j]; if (j + (1 << (i - 1)) < a.length) { min[i][j] = func(min[i][j], min[i - 1][j + (1 << (i - 1))]); } } } lessPow = new int[a.length + 1]; for (int i = 1; i < lessPow.length; i++) { if (i < (1 << (lessPow[i - 1]) + 1)) { lessPow[i] = lessPow[i - 1]; } else { lessPow[i] = lessPow[i - 1] + 1; } } } public int get(int l, int r) { // [l, r) int p = lessPow[r - l]; return func(min[p][l], min[p][r - (1 << p)]); } public int func(int a, int b) { if (a < b) { return a; } return b; } } public double check(int n, ArrayList<Integer> masks) { int good = 0; for (int colorMask = 0; colorMask < (1 << n); ++colorMask) { int best = 2 << n; int cnt = 0; for (int curMask : masks) { int curScore = 0; for (int i = 0; i < n; ++i) { if (((curMask >> i) & 1) == 1) { if (((colorMask >> i) & 1) == 0) { curScore += 1; } else { curScore += 2; } } } if (curScore < best) { best = curScore; cnt = 1; } else if (curScore == best) { ++cnt; } } if (cnt == 1) { ++good; } } return (double) good / (double) (1 << n); } public int builtin_popcount(int x) { int ans = 0; for (int i = 0; i < 14; i++) { if (((x >> i) & 1) > 0) { ans++; } } return ans; } public int number(int[] x) { int ans = 0; for (int i = 0; i < x.length; i++) { ans *= 3; ans += x[i]; } return ans; } public int[] rotate(int[] x) { int[] ans = { x[2], x[0], x[3], x[1] }; return ans; } int MAX = 100000; boolean[] b = new boolean[MAX]; int[][] max0 = new int[MAX][2]; int[][] max1 = new int[MAX][2]; int[][] max2 = new int[MAX][2]; int[] index0 = new int[MAX]; int[] index1 = new int[MAX]; int[] p = new int[MAX]; public int place(String s) { if (s.charAt(s.length() - 1) == '1') { return 1; } int number = 16; boolean w = true; boolean a = true; for (int i = 0; i < s.length(); i++) { if (number == 1) { return 2; } if (s.charAt(i) == '1') { if (w) { number /= 2; } else { if (a) { a = false; } else { number /= 2; a = true; } } } else { if (w) { if (number == 16) { w = false; number /= 2; } else { w = false; a = false; } } else { if (number == 8) { if (a) { return 13; } else { return 9; } } else if (number == 4) { if (a) { return 7; } else { return 5; } } else if (a) { return 4; } else { return 3; } } } } return 0; } public class P implements Comparable<P> { Integer x; String s; public P(Integer x, String s) { this.x = x; this.s = s; } @Override public String toString() { return x + " " + s; } @Override public int compareTo(P o) { if (x != o.x) { return x - o.x; } return s.compareTo(o.s); } } public BigInteger prod(int l, int r) { if (l + 1 == r) { return BigInteger.valueOf(l); } int m = (l + r) >> 1; return prod(l, m).multiply(prod(m, r)); } public class Frac { BigInteger p; BigInteger q; public Frac(BigInteger p, BigInteger q) { BigInteger gcd = p.gcd(q); this.p = p.divide(gcd); this.q = q.divide(gcd); } public String toString() { return p + "\n" + q; } public Frac(long p, long q) { this(BigInteger.valueOf(p), BigInteger.valueOf(q)); } public Frac mul(Frac o) { return new Frac(p.multiply(o.p), q.multiply(o.q)); } public Frac sum(Frac o) { return new Frac(p.multiply(o.q).add(q.multiply(o.p)), q.multiply(o.q)); } public Frac diff(Frac o) { return new Frac(p.multiply(o.q).subtract(q.multiply(o.p)), q.multiply(o.q)); } } public void dfs(int v, int prev, int sum) { sub[v] = -sum; for (int i : to[v]) { if (i == prev) { continue; } dfs(i, v, sub[v]); } a[v] = sum - (-sum) * (to[v].length - 1); } int[][] to; int[][] size; int[] a; int[] sub; public long calc(int[] a, int mean) { long ans = 0; long more = 0; for (int q = 0; q < 2; q++) { for (int i = 0; i < a.length; i++) { if (a[i] > mean) { more += a[i] - mean; a[i] = mean; } else { if (mean - a[i] < more) { more -= mean - a[i]; a[i] = mean; } else { a[i] += more; more = 0; } } ans += more; } } return ans; } int LOG = 31; public void solve() { int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[m]; int[] b = new int[m]; int[] w = new int[m]; boolean[] even = new boolean[n]; for (int i = 0; i < w.length; i++) { a[i] = in.nextInt() - 1; b[i] = in.nextInt() - 1; w[i] = in.nextInt(); if (w[i] % 2 == 0) { even[a[i]] = true; even[b[i]] = true; } } DSU[] dsu = new DSU[LOG]; boolean[][] goodDaddy = new boolean[LOG][n]; for (int i = 0; i < LOG; i++) { dsu[i] = new DSU(n); for (int j = 0; j < w.length; j++) { if (((w[j] >> i) & 1) == 1) { dsu[i].unite(a[j], b[j]); } } for (int j = 0; j < even.length; j++) { goodDaddy[i][dsu[i].get(j)] |= even[j]; } } int q = in.nextInt(); for (int i = 0; i < q; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; boolean zero = false; for (int j = 0; j < dsu.length; j++) { if (dsu[j].get(u) == dsu[j].get(v)) { zero = true; break; } } if (zero) { out.println(0); } else { boolean one = false; for (int j = 1; j < goodDaddy.length; j++) { if (goodDaddy[j][dsu[j].get(u)]) { one = true; break; } } if (one) { out.println(1); } else { out.println(2); } } } } public void add(HashMap<Integer, Integer> map, int x) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { long time = System.currentTimeMillis(); new E().run(); System.err.println(System.currentTimeMillis() - time); } }
Java
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
524179c208fafb8c92b7810121332e15
train_110.jsonl
1650206100
There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,\ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 \leq i &lt; k$$$.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have $$$[w_1,w_2,\ldots,w_{k-1}]$$$ where $$$w_i$$$ is the weight of the edge between $$$p_i$$$ and $$$p_{i+1}$$$. Then the length of the walk is given by $$$\mathrm{MEX}(\{w_1,\,w_1\&amp; w_2,\,\ldots,\,w_1\&amp; w_2\&amp; \ldots\&amp; w_{k-1}\})$$$, where $$$\&amp;$$$ denotes the bitwise AND operation.Now you must process $$$q$$$ queries of the form u v. For each query, find the minimum possible length of a walk from $$$u$$$ to $$$v$$$.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$$\{2,1\}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$$\{3,1,0\}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$$\{0,3,1,2\}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does 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; /* Can the length even be 4? 00 01 10 11 each new number is a submask of the last So you can't have both 1 and 2 So the answer to a query is 0, 1, or 2, but never 3 Q1: Can we go from a->b with every edge having some bit? -> answer will be 0 A: use 30 DJS Q2: Can we get from a->b by losing the 1 bit <= losing the last other bit Q2 Easy: Can we get to some edge which doesn't have a 1 bit without losing all other bits? Every edge without a 1 bit goes to a special node -> finish Q: Can we reach the finish from a? */ public class E { 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 m=fs.nextInt(); Edge[] edges=new Edge[m]; for (int i=0; i<m; i++) edges[i]=new Edge(fs.nextInt()-1, fs.nextInt()-1, fs.nextInt()); DJS[] usingBit=new DJS[30]; DJS[] usingBitToSpecial=new DJS[30]; for (int i=0; i<30; i++) { usingBit[i]=new DJS(n); usingBitToSpecial[i]=new DJS(n+1); } for (Edge e:edges) { for (int bit=0; bit<30; bit++) { if ((e.c&(1<<bit))==0) continue; usingBit[bit].union(e.a, e.b); } for (int bit=0; bit<30; bit++) { if ((e.c & 1)==0) { usingBitToSpecial[bit].union(e.a, n); usingBitToSpecial[bit].union(e.b, n); } if ((e.c&(1<<bit))==0) continue; usingBitToSpecial[bit].union(e.a, e.b); } } int nq=fs.nextInt(); outer: for (int qq=0; qq<nq; qq++) { int a=fs.nextInt()-1, b=fs.nextInt()-1; for (int bit=0; bit<30; bit++) { if (usingBit[bit].find(a)==usingBit[bit].find(b)) { out.println(0); continue outer; } } for (int bit=1; bit<30; bit++) { if (usingBitToSpecial[bit].find(a)==usingBitToSpecial[bit].find(n)) { out.println(1); continue outer; } } out.println(2); } } out.close(); } static class Edge { int a, b, c; public Edge(int a, int b, int c) { this.a=a; this.b=b; this.c=c; } } static class DJS { int[] s; public DJS(int n) { Arrays.fill(s = new int[n], -1); } public int find(int i) { return s[i] < 0 ? i : (s[i] = find(s[i])); } public boolean union(int a, int b) { if ((a = find(a)) == (b = find(b))) return false; if(s[a] == s[b]) s[a]--; if(s[a] <= s[b]) s[b] = a; else s[a] = b; return true; } } 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
["6 7\n1 2 1\n2 3 3\n3 1 5\n4 5 2\n5 6 4\n6 4 6\n3 4 1\n3\n1 5\n1 2\n5 3", "9 8\n1 2 5\n2 3 11\n3 4 10\n3 5 10\n5 6 2\n5 7 1\n7 8 5\n7 9 5\n10\n5 7\n2 5\n7 1\n6 4\n5 2\n7 6\n4 1\n6 2\n4 7\n2 8"]
3 seconds
["2\n0\n1", "0\n0\n2\n0\n0\n2\n1\n0\n1\n1"]
NoteThe following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query:$$$$$$1 \overset{5}{\rightarrow} 3 \overset{3}{\rightarrow} 2 \overset{1}{\rightarrow} 1 \overset{5}{\rightarrow} 3 \overset{1}{\rightarrow} 4 \overset{2}{\rightarrow} 5.$$$$$$The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$$\{5,1,0\}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement).
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
a3e89153f98c139d7e84f2307b128b24
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$; $$$n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2},10^5\right)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 \leq a, b \leq n$$$, $$$a \neq b$$$; $$$0 \leq w &lt; 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), the description of each query.
2,200
For each query, print one line containing a single integer — the answer to the query.
standard output
PASSED
0e5bbf72ee636aef62e4f959b7a5bcfb
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; /** B. Quality vs Quantity https://codeforces.com/contest/1646/problem/B My idea: It's right, but there is a anti-quick Sort case, we need to random it. * */ public class B { static int N = 200010; static int n; static int[] a = new int[N]; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); for(int i = 1; i <= n; i++) a[i] = sc.nextInt(); if(solver()) System.out.println("YES"); else System.out.println("NO"); } } static boolean solver() { Random gen = new Random(); for(int i = 1; i <= n; i++){ int j = gen.nextInt(n + 1 - 1) + 1; // up bound 1 ~ n int t = a[i]; a[i] = a[j]; a[j] = t; // swap in a line } Arrays.sort(a, 1, n + 1); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ s[i] = s[i - 1] + a[i]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; } return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
c0143c6f4d1d79e9df98fdad3366a0a0
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; /** B. Quality vs Quantity https://codeforces.com/contest/1646/problem/B My idea: It's right, but there is a anti-quick Sort case, we need to random it. * */ public class B { static int N = 200010; static int n; static int[] a; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()) System.out.println("YES"); else System.out.println("NO"); } } static boolean solver() { Random gen = new Random(); for(int i = 0 ; i < n; i++){ int j = gen.nextInt(n); int t = a[i]; a[i] = a[j]; a[j] = t; // swap in a line } Arrays.sort(a, 0, n); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; } return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
9f9d1a6c4b7b139135847183937b07fa
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()) System.out.println("YES"); else System.out.println("NO"); } } static boolean solver() { // quickSort(a, 0, n - 1); Random gen = new Random(); for(int i = 0;i < n-1;i++){ int j = gen.nextInt(n-i-1)+i+1; int d = a[i]; a[i] = a[j]; a[j] = d; } Arrays.sort(a, 0, n); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; } return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
7726129989f7576f285c375a9f0a61c5
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()) System.out.println("YES"); else System.out.println("NO"); } } static boolean solver() { quickSort(a, 0, n - 1); // Arrays.sort(a, 0, n); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; } return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
9460b3533ab595c0a52cb97f248f17cc
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()){ bw.write("YES"); bw.write("\n"); } else{ bw.write("NO"); bw.write("\n"); } } bw.flush(); } static boolean solver() { quickSort(a, 0, n - 1); // Arrays.sort(a, 0, n); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; } return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
9574683d47c58ef1d2d8a8f7e470e72c
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()){ bw.write("YES"); bw.write("\n"); } else{ bw.write("NO"); bw.write("\n"); } } bw.flush(); } static boolean solver() { quickSort(a, 0, n - 1); // Arrays.sort(a, 0, n); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ if(cnt++ > 400010 * 20) return true; s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; if(cnt++ > 400010 * 20) return true; } if(cnt > 400010 * 20) return true; return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
e57bc7624942a5f217d475037218da91
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()){ bw.write("YES"); bw.write("\n"); } else{ bw.write("NO"); bw.write("\n"); } } bw.flush(); } static boolean solver() { quickSort(a, 0, n - 1); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ if(cnt++ > 400010 * 20) return true; s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; if(cnt++ > 400010 * 20) return true; } if(cnt > 400010 * 20) return true; return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; // if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; // if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
713ad0ab52d97b655da66ab7707fea86
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { static int N = 200010; static int n, cnt; static boolean flag = false; static int[] a; static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); if(solver()){ bw.write("YES"); bw.write("\n"); } else{ bw.write("NO"); bw.write("\n"); } } bw.flush(); } static boolean solver() { quickSort(a, 0, n - 1); long[] s = new long[n + 1]; for(int i = 1; i <= n; i++){ if(cnt++ > 400010 * 20) return true; s[i] = s[i - 1] + a[i - 1]; } int i = 2; int j = n; while (i <= j){ long rightS = s[n] - s[j - 1]; long leftS = s[i]; if(rightS > leftS) return true; i++; j--; if(cnt++ > 400010 * 20) return true; } if(cnt > 400010 * 20) return true; return false; } static void quickSort(int[] arr, int l, int r){ if(l >= r) return; int i = l - 1, j = r + 1, x = arr[r]; while(i < j){ do{ i++; if(cnt++ > 400010 * 20) return; }while(arr[i] < x); do{ j--; if(cnt++ > 400010 * 20) return; } while(arr[j] > x); if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } if(cnt++ > 400010 * 20) return; } quickSort(arr, l, i - 1); quickSort(arr, i, r); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
ce5960001259472b37b28cb0c9df1701
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /** * @author 邶风 * @data 2022/3/4 23:42 */ public class Main{ static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static void solve(){ int n=in.nextInt(); Integer a[]=new Integer[n+10]; for(int i=1;i<=n;i++){ a[i]=in.nextInt(); } Arrays.sort(a,1,n+1); long left=a[1]; long right=0; int x=n; for(int i=2;i<x;i++,x--){ left=left+a[i]; right=right+a[x]; if(right>left) { out.println("YES"); return; } }out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
5458c68b440cf7774143666945152747
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author 邶风 * @data 2022/3/4 23:42 */ public class Main { static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static List<Integer> list; static void solve(){ list=new ArrayList<>(); int n=in.nextInt(); for(int i=1;i<=n;i++){ list.add(in.nextInt()); } Collections.sort(list); long left=list.get(0); long right=0; n--; for(int i=1;i<=n;i++,n--){ left=left+list.get(i); right=right+list.get(n); if(right>left) { out.println("YES"); return; } }out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
3c7c411dac3ef4d0babb57d296aff946
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author 邶风 * @data 2022/3/4 23:42 */ public class Main { static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static List<Integer> list; static void solve(){ list=new ArrayList<>(); int n=in.nextInt(); for(int i=1;i<=n;i++){ list.add(in.nextInt()); } Collections.sort(list); long left=list.get(0)+list.get(1); long right=list.get(n-1); if(right>left) { out.println("YES"); return; } int x=n-2; for(int i=2;i<x;i++,x--){ left=left+list.get(i); right=right+list.get(x); if(right>left) { out.println("YES"); return; } }out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
24795d4587f00406ac5cb0bb4e98f318
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /** * @author 邶风 * @data 2022/3/4 23:42 */ public class Main{ static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static void solve(){ int n=in.nextInt(); Integer a[]=new Integer[n+10]; for(int i=1;i<=n;i++){ a[i]=in.nextInt(); } Arrays.sort(a,1,n+1); long left=a[1]+a[2]; long right=a[n]; if(n==3){ if(right>left) out.println("YES"); else out.println("NO"); return; } int x=n-1; for(int i=3;i<=x;i++,x--){ left=left+a[i]; right=right+a[x]; if(right>left) { out.println("YES"); return; } }out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
3e85081f9d81485728bb7154ec9cd937
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author 邶风 * @data 2022/3/4 23:42 */ public class Main { static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static List<Integer> list; static void solve(){ list=new ArrayList<>(); int n=in.nextInt(); for(int i=1;i<=n;i++){ list.add(in.nextInt()); } Collections.sort(list); long left=list.get(0)+list.get(1); long right=list.get(n-1); if(right>left) { out.println("YES"); return; } int x=n-2; for(int i=2;i<=x;i++,x--){ left=left+list.get(i); right=right+list.get(x); if(right>left) { out.println("YES"); return; } }out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
02b6074a5fa2bb7efdda0cf414223783
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
//package MyPackage; import java.util.*; import java.io.*; public class codec{ static class Pair { int x; int y; int z; Pair(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } private static boolean isPal(String s) { int l = 0, r = s.length() - 1; while(l < r) { if(s.charAt(l++) != s.charAt(r--)) return false; } return true; } public static void swap(int[][] arr, int i, int j, int a, int b) { // arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]); int t = arr[i][j]; arr[i][j] = arr[a][b]; arr[a][b] = t; } static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } static int lb(ArrayList<Integer>ar, int k) { int s = 0; int 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 ar.get(s); } static int ub(ArrayList<Integer>ar, int k) { int s = 0; int 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; } private static void func(int i, List<List<String>> res, List<String> al, List<String> ad) { if(i == al.size()) { List<String> st = new ArrayList<>(ad); res.add(st); return; } ad.add(al.get(i)); func(i + 1, res, al, ad); ad.remove(ad.size() - 1); func(i + 1, res, al, ad); } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); StringBuilder sb = new StringBuilder(); int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; i++) a.add(in.nextLong()); Collections.sort(a); int f = 0; int o = 1, u = n - 1; long fr = a.get(0); long ba = 0; while(o < u) { fr += a.get(o); ba += a.get(u); if(fr < ba) { f = 1; break; } o++; u--; } if(f == 0) sb.append("NO" + "\n"); else sb.append("YES" + "\n"); } out.println(sb); out.close(); } catch (Exception e) { return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
1f8135c7bc1d7506500db762a4748de2
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
//package MyPackage; import java.util.*; import java.io.*; public class codec{ static class Pair { int x; int y; int z; Pair(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } private static boolean isPal(String s) { int l = 0, r = s.length() - 1; while(l < r) { if(s.charAt(l++) != s.charAt(r--)) return false; } return true; } public static void swap(int[][] arr, int i, int j, int a, int b) { // arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]); int t = arr[i][j]; arr[i][j] = arr[a][b]; arr[a][b] = t; } static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } static int lb(ArrayList<Integer>ar, int k) { int s = 0; int 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 ar.get(s); } static int ub(ArrayList<Integer>ar, int k) { int s = 0; int 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; } private static void func(int i, List<List<String>> res, List<String> al, List<String> ad) { if(i == al.size()) { List<String> st = new ArrayList<>(ad); res.add(st); return; } ad.add(al.get(i)); func(i + 1, res, al, ad); ad.remove(ad.size() - 1); func(i + 1, res, al, ad); } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); StringBuilder sb = new StringBuilder(); int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(); List<Long> a = new ArrayList<>(); for(int i = 0; i < n; i++) a.add(in.nextLong()); Collections.sort(a); long fr = a.get(0) + a.get(1); long ba = a.get(n - 1); int f = 0; int o = 2, u = n - 2; while(o < u) { if(fr < ba) { f = 1; break; } fr += a.get(o); ba += a.get(u); o++; u--; } if(f == 1 || fr < ba) sb.append("YES" + "\n"); else sb.append("NO" + "\n"); } out.println(sb); out.close(); } catch (Exception e) { return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
552312244c3fe0688d5df4f1b4c2a48a
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; public class roughCodeforces { public static void main(String[] args) throws java.lang.Exception{ Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); long o = sc.nextLong(); while(o > 0){ long n = sc.nextLong(); Vector<Long> a = new Vector<>(); for(long i = 0;i <n;i++){ a.add(sc.nextLong()); } Collections.sort(a); long left = a.get(0) + a.get(1); long right = a.get((int)n - 1); if(left < right){ sb.append("YES\n"); }else{ long t1 = 1; long t2 = n - 1; boolean is = false; while(t1 < t2 && t1 < n && t2 >= 0){ t1++; t2--; left += a.get((int)t1); right += a.get((int)t2); if(left < right){ is = true; break; } } if(is){ sb.append("YES\n"); }else{ sb.append("NO\n"); } } o--; } System.out.print(sb.toString()); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
4a06d7ae446402c484e162d87fcd6a40
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForcesTest{ public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); PrintWriter write = new PrintWriter(System.out); int t = Integer.parseInt(read.readLine()); for(int i=0; i<t; i++){ int n = Integer.parseInt(read.readLine()); StringTokenizer tokenizer = new StringTokenizer(read.readLine()); Integer[] a = new Integer[n]; for(int j=0; j<n; j++) a[j] = Integer.parseInt(tokenizer.nextToken()); Arrays.sort(a); String out = "NO"; int l=1, r=n-1; long sumLeft = a[0] + a[1]; long sumRight = a[r]; while(l<r){ if(sumLeft<sumRight) { out = "YES"; break; } else{ sumLeft += a[++l]; sumRight += a[--r]; } } write.println(out); } write.flush(); write.close(); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
124d0e0dde0b8be11ceceed995ee794f
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForcesTest{ public static int t, n; public static Integer[] a; public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); PrintWriter write = new PrintWriter(System.out); t = Integer.parseInt(read.readLine()); for(int i=0; i<t; i++){ n = Integer.parseInt(read.readLine()); StringTokenizer tokenizer = new StringTokenizer(read.readLine()); a = new Integer[n]; for(int j=0; j<n; j++) a[j] = Integer.parseInt(tokenizer.nextToken()); Arrays.sort(a); String out = "NO"; int l=1, r=n-1; long sumLeft = a[0] + a[1]; long sumRight = a[r]; while(l<r){ if(sumLeft<sumRight) { out = "YES"; break; } else{ sumLeft += a[++l]; sumRight += a[--r]; } } write.println(out); } write.flush(); write.close(); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
dfd5acc4015c89fb58b55c346370d4f5
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } static class Pair{ int a,b; Pair(int x, int y) { a=x; b= y; } } static class Interval{ long st,e; Interval(long x, long y) { st=x; e=y; } } static long mod = 1000000007; public static void main(String[] args) throws Exception { //Read input from user //Scanner scn = new Scanner(System.in); FastReader scn = new FastReader(); int t = scn.nextInt(); while(t>0) { int n = scn.nextInt(); ArrayList<Long> a = new ArrayList<>(); for(int i=0;i<n;i++) { a.add(scn.nextLong()); } Collections.sort(a); int i = 1; long suml = a.get(0), sumr = 0; long flag = 0; int j = n - 1; while (i < j ) { suml += a.get(i); sumr += a.get(j); if (suml < sumr) { flag = 1; break; } i++; j--; } if(flag==1) { System.out.println("YES"); }else { System.out.println("NO"); } t--; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
6b4e85a2976202903ca6ba3fe7ca0844
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc = new FastReader(); long t = sc.nextLong(); while(t-->0){ int n = sc.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); for(int i=0;i<n;i++){ a.add(sc.nextInt()); } Collections.sort(a); int end = 1; int start = n-1; long blue = a.get(0)+a.get(1); long red = a.get(n-1); int flag=0; while(end <start){ if(red>blue){ System.out.println("YES"); // System.out.println(red+" "+blue); flag++; break; } else{ end++; start--; blue+=a.get(end); red+=a.get(start); } } if(flag == 0) System.out.println("NO"); } } catch(Exception e){ return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
4d869594778d7dc4cd36c84551d1d7cf
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import javax.sound.midi.Soundbank; import java.util.*; public class QualityVsQuantity { static void log(int[] a) { System.out.println(Arrays.toString(a)); } static void log(double[] a) { System.out.println(Arrays.toString(a)); } static void log(long[] a) { System.out.println(Arrays.toString(a)); } static void log(ArrayList a) { System.out.println(a.toString()); } static void log(int a) { System.out.println(a); } static void log(double a) { System.out.println(a); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++) { int n = sc.nextInt(); Integer[] a = new Integer[n]; for(int j = 0; j < n; j++) { a[j] = sc.nextInt(); } Arrays.sort(a); int idx1 = 0, idx2 = n - 1; long right = a[idx2]; long left = a[idx1] + a[idx1 + 1]; idx1 += 2; idx2 -= 1; while(idx1 < idx2) { if(right > left) break; if(a[idx2] == a[idx1]) break; right += a[idx2]; left += a[idx1]; idx1++; idx2--; } if(right > left) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
c416801f5727948fb1d99772e9947263
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t = sc.nextInt(); while(t>0){ int n= sc.nextInt(); ArrayList<Long > al = new ArrayList<>(); for(int i=0 ; i< n ; i++) { al.add( sc.nextLong()); } Collections.sort(al); long sb =al.get(0); long sr = 0; int k=-1; for(int i=1 ; i<=n/2 ;i++) { sb+=al.get(i); sr+=al.get(n-i); if(sr > sb) { System.out.println("YES"); k=0; break; } } if(k==-1){ System.out.println("NO"); } t--; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
f7a22e6e151dfcb36111e0fe34d7566b
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class HelloWorld{ public static void main(String []args) throws IOException{ Reader sc=new Reader(); int t=sc.nextInt(); while(t-->0) { int temp=0; int n=sc.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } ruffleSort(a); int pre=1; int pro=n-1; long sum1=a[pro]; long sum2=a[0]+a[1]; int flag=0; while(pre<pro) { if(sum1>sum2) { flag=1; break; } else { pro--; pre++; sum1+=a[pro]; sum2+=a[pre]; } } if(flag==1) { System.out.println("YES"); } else { System.out.println("NO"); } } } static final Random random = new Random(); static void ruffleSort(long arr[]) { int n = arr.length; for(int i = 0; i < n; i++) { int j = random.nextInt(n),temp =(int) arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
2b4e839137e55d308787841947838098
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for(int caseNum = 0; caseNum < T; caseNum++){ int n = scanner.nextInt(); if(n < 3){ System.out.println("NO"); continue; } Integer[] nums = new Integer[n]; for (int i = 0; i < n; i++) { nums[i] = scanner.nextInt(); } Arrays.sort(nums); int L = 2, R = n - 1; long lSum = nums[0] + nums[1]; long rSum = nums[R]; R--; boolean flag = false; while(L < R){ if(lSum < rSum){ flag = true; break; } lSum+=nums[L]; rSum+=nums[R]; L++; R--; } if(rSum > lSum){ System.out.println("yEs"); }else { System.out.println("No"); } } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
55b89b13a1813715a676c344c84bbabe
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.sql.SQLOutput; import java.util.Arrays; import java.util.StringTokenizer; public class Solution { public static int found = 0; public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); Long[] a = new Long[n]; for(int i=0; i<n; i++) a[i] = sc.nextLong(); Arrays.sort(a); int s = 1; int e = n-2; long sumBlue = a[0]; long sumRed = a[n-1]; boolean found = false; while (s <= e) { if(sumRed <= sumBlue) sumRed += a[e--]; else sumBlue += a[s++]; if(sumRed > sumBlue && s > n-e-1) { found = true; break; } } out.println(found ? "YES" : "NO"); } out.flush(); out.close(); } public static void printPermutation(int[] perm, int n) { for(int i=1; i<n; i++) System.out.print(perm[i] + " "); System.out.println(perm[n]); } public static void generatePermutation(boolean[] set, int[] perm, int n, int curInd) { if(found == n) return; if(curInd > n) { found ++; printPermutation(perm, n); return; } for(int no=1; no<=n; no++) { if(set[no]) continue; if(curInd > 2 && perm[curInd-2]+perm[curInd-1] == no) continue; perm[curInd] = no; set[no] = true; generatePermutation(set, perm, n, curInd+1); set[no] = false; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
0a7d75decf794a67fb51ccbdb1739dff
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); Arrays.sort(a); int s = 1; int e = n - 2; long sumBlue = a[0]; long sumRed = a[n - 1]; boolean found = false; while (s <= e) { if (sumRed <= sumBlue) sumRed += a[e--]; else sumBlue += a[s++]; if (sumRed > sumBlue && s > n - e - 1) { found = true; break; } } System.out.println(found ? "YES" : "NO"); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
7ef128d7fda32e239dd68f5f2de65b64
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); fastReader sc = new fastReader(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int arr[] = new int[n]; int min = Integer.MAX_VALUE; int smin = Integer.MAX_VALUE; int max = 0; int idx=0; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); if(min>arr[i]){ min = arr[i]; idx = i; } max = Math.max(max,arr[i]); } for(int i=0;i<n;i++){ if(idx!=i && smin>arr[i])smin = arr[i]; } if(min+smin<max)sb.append("YES").append("\n"); else{ Arrays.sort(arr); long x1 = arr[0]+arr[1]; long x2 = arr[n-1]; int i=2,j=n-2; boolean check = false; while(i<j){ if(x2>x1){ check=true; break; } x1+=arr[i]; x2+=arr[j]; i++; j--; } if(check || x2>x1) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } } String printMe = sb.toString(); System.out.println(printMe); } } 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(); } long nextLong(){ return Long.parseLong(next()); } int nextInt(){ return Integer.parseInt(next()); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
045f75fdf7268fbb4c15b1bf617873bd
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Solution2 { private final static FastReader scan = new FastReader(); static List<Integer> l1 = new ArrayList<>(); public static void main(String[] args) { int tc = 1; tc = scan.nextInt(); while (tc-- != 0) { solve(); } } private static void solve() { int n = scan.nextInt(); List<Long> a = new ArrayList<>(); long tc = 0; for(int i = 0;i<n;i++) { a.add(scan.nextLong()); } Collections.sort(a); long low = a.get(0)+a.get(1); if(low<a.get(n-1)) { pln("YES"); return ; } long high = a.get(n-1); for(int i = 2;i<=(n/2);i++) { low+=a.get(i); high+=a.get(n-i); if(low<high) { pln("YES"); return ; } } pln("NO"); } @SuppressWarnings("unused") private static void ptArr(final int[] a) { for (int i : a) System.out.print(i + " "); System.out.println(); } @SuppressWarnings("unused") private static void pt2DArr(final int[][] a) { for(int[] j:a) { for (int i : j) System.out.print(i + " "); System.out.println(); } System.out.println(); } @SuppressWarnings("unused") private static void pt(String val) { System.out.print(val); } @SuppressWarnings("unused") private static void pln(String val) { System.out.println(val); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class ArrayHash { int[] a; ArrayHash(int[] a) { this.a = Arrays.copyOf(a, a.length); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(a); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ArrayHash other = (ArrayHash) obj; return Arrays.equals(a, other.a); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
fbdd6f895a9165e3c3f562adb83ab540
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc = new FastReader(); long t = sc.nextLong(); while(t-->0){ int n = sc.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); for(int i=0;i<n;i++){ a.add(sc.nextInt()); } Collections.sort(a); int end = 1; int start = n-1; long blue = a.get(0)+a.get(1); long red = a.get(n-1); int flag=0; while(end <start){ if(red>blue){ System.out.println("YES"); // System.out.println(red+" "+blue); flag++; break; } else{ end++; start--; blue+=a.get(end); red+=a.get(start); } } if(flag == 0) System.out.println("NO"); } } catch(Exception e){ return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
39f116a9c97677692436e98601cde2ca
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc = new FastReader(); long t = sc.nextLong(); while(t-->0){ int n = sc.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); for(int i=0;i<n;i++){ a.add(sc.nextInt()); } Collections.sort(a); long blue = a.get(0); long red = 0; boolean flag=false; for(int i=1;i<=n/2;i++ ){ blue+=a.get(i); red+=a.get(n-i); if(red>blue){ flag = true; break; } } if(flag) System.out.println("YES"); else System.out.println("NO"); } } catch(Exception e){ return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
dad4bd07a916814b891390dba8e31f37
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc = new FastReader(); long t = sc.nextLong(); while(t-->0){ int n = sc.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); for(int i=0;i<n;i++){ a.add(sc.nextInt()); } Collections.sort(a); long blue = a.get(0); long red = 0; boolean flag=false; for(int i=1;i<=n/2;i++ ){ blue+=a.get(i); red+=a.get(n-i); if(red>blue){ flag = true; break; } } if(flag) System.out.println("YES"); else System.out.println("NO"); } System.out.close(); } catch(Exception e){ return; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
c563bbf902f7d90601298e1124cdee93
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ 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()); } } public static void main(String[] args){ FastReader in = new FastReader(); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); Integer[] arr = new Integer[n]; Arrays.setAll(arr,i->in.nextInt()); Arrays.sort(arr, Collections.reverseOrder()); System.out.println(isPossible(arr,n)? "YES" : "NO"); } } public static boolean isPossible(Integer[] arr,int n){ long sum = arr[n-1]; int ri =0; int bi = n-2; while(ri<bi){ if(sum<0) return true; sum-=arr[ri]; sum+=arr[bi]; ri++; bi--; } return sum<0; } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
ff58a4709f1c021e4ca597aef992c362
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ 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()); } } public static void main(String[] args){ FastReader in = new FastReader(); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); Integer[] arr = new Integer[n]; Arrays.setAll(arr,i->in.nextInt()); Arrays.sort(arr, Collections.reverseOrder()); System.out.println(isPossible(arr,n)? "YES" : "NO"); } } public static boolean isPossible(Integer[] arr,int n){ long reds = 0; long blues = arr[n-1]; int ri =0; int bi = n-2; while(ri<bi){ if(reds>blues) return true; reds+=arr[ri]; blues+=arr[bi]; ri++; bi--; } return reds>blues; } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
e19fe3d6676b6aa689fdec4ac2f6aa92
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; public class coding { static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static int gcd(int a,int b) { int i=a%b; while(i!=0) { a=b; b=i; i=a%b; } return b; } public static void main(String args[] ) throws Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); ArrayList<Long> l= new ArrayList<>(); for(int i=0;i<n;i++) l.add(in.nextLong()); Collections.sort(l); long suma=l.get(0)+l.get(1); long sumb=l.get(n-1); int i=2,j=n-2; int z=0; if(sumb>suma) System.out.println("YES"); else { while(i<j) { suma+=l.get(i); sumb+=l.get(j); if(sumb>suma) { System.out.println("YES"); z=1;break; } i++;j--; } if(z==0) System.out.println("NO"); } } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
6340449628f9c9bfc0f85cadb1df9c1b
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class B { void solve() throws IOException { int t = nextInt(); for (int tt = 0; tt < t; ++tt) { int n = nextInt(); Integer[] arr = new Integer[n]; for (int i = 0; i < arr.length; ++i) { arr[i] = nextInt(); } Arrays.sort(arr); int right = arr.length; long rightSum = 0; int left = 0; long leftSum = arr[0]; boolean good = false; while (left < right) { if (rightSum > leftSum) { good = true; break; } ++left; leftSum += arr[left]; --right; rightSum += arr[right]; } // for (int i = 1, j = n-1; ; i++, j--) { // if (rightSum > leftSum) { // good = true; // break; // } // if (i >= j) break; // rightSum += arr[j]; // leftSum += arr[i]; // } if (good) writer.println("YES"); else writer.println("NO"); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } StringTokenizer tokenizer; PrintWriter writer; BufferedReader reader; public void run() { try { writer = new PrintWriter(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new B().run(); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
19123fee610625788bc0a66288841af0
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class b { public static void main(String[] args) { PrintWriter pw = new PrintWriter(System.out); FastReader s = new FastReader(); int tc = s.nextInt(); loop: while (tc-- > 0) { int n = s.nextInt(); ArrayList<Integer> l = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { l.add(s.nextInt()); } Collections.sort(l); long sumr = l.get(n - 1); long sumb = l.get(0) + l.get(1); if (sumr > sumb) { pw.println("YES"); continue loop; } for (int i = 2; i <= (n / 2); i++) { sumr += l.get(n - i); sumb += l.get(i); if (sumr > sumb) { pw.println("YES"); continue loop; } } pw.println("NO"); } pw.close(); } public static int[] sorting(int[] arr) { Arrays.sort(arr); return arr; } public static boolean solve(int[] arr, long sumr, long sumb, int n) { for (int i = 1; i <= (n / 2); i++) { sumr += arr[n - i]; sumb += arr[i]; if (sumr > sumb) { return true; } } return false; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
1646b71d7f0df4c6d2f760d6cdb8bd98
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class go { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int x= sc.nextInt(); while(x-- !=0){ int n=sc.nextInt(); int[] arr= new int[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextInt(); } arr=Arrays.stream(arr).boxed().sorted().mapToInt(p -> p).toArray(); long l=arr[0]; long m=0; int j=arr.length-1; for(int i=0; i<(n-1)/2; i++){ m+=arr[j-i]; l+=arr[i+1]; } System.out.println(m>l? "YES":"NO"); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
55c7ad091aee7bc078de55b88ad4a71b
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Main { // Graph // prefix sums //inputs public static void main(String args[])throws Exception{ Input sc=new Input(); precalculates p=new precalculates(); StringBuilder sb=new StringBuilder(); int t=sc.readInt(); for(int f=0;f<t;f++){ int n=sc.readInt(); long a[]=sc.readArrayLong(); boolean b=false; long min1=Integer.MAX_VALUE; long min2=Integer.MAX_VALUE; long max=0; long cmin1=0; long cmin2=0; long cmax=0; for(int i=0;i<n;i++){ min1=Math.min(a[i],min1); max=Math.max(max,a[i]); } for(int i=0;i<n;i++){ if(a[i]==min1) cmin1++; if(a[i]==max) cmax++; if(a[i]>min1) min2=Math.min(a[i],min2); } boolean eq=false; if(cmin1>1){ min2=min1; } if(min2!=min1){ for(int i=0;i<n;i++){ if(a[i]==min2) cmin2++; } }else{ cmin2=cmin1-1; } if(min1+min2<max){ b=true; }else{ Arrays.sort(a); long sum1=a[0]; long sum2=0; int count=0; for(int i=n-1;i>=1;i--){ count++; sum1+=a[count]; sum2+=a[i]; if((count<i) && sum2>sum1){ b=true; break; } if(count>=i){ break; } } } if(b) sb.append("YES\n"); else sb.append("NO\n"); //System.out.println(Math.log(200000)/Math.log(2)); } System.out.print(sb); } // 4 4 5 5 5 8 } class Input{ BufferedReader br; StringTokenizer st; Input(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public int[] readArray() throws Exception{ st=new StringTokenizer(br.readLine()); int a[]=new int[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Integer.parseInt(st.nextToken()); } return a; } public long[] readArrayLong() throws Exception{ st=new StringTokenizer(br.readLine()); long a[]=new long[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Long.parseLong(st.nextToken()); } return a; } public int readInt() throws Exception{ st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public long readLong() throws Exception{ st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public String readString() throws Exception{ return br.readLine(); } public int[][] read2dArray(int n,int m)throws Exception{ int a[][]=new int[n][m]; for(int i=0;i<n;i++){ st=new StringTokenizer(br.readLine()); for(int j=0;j<m;j++){ a[i][j]=Integer.parseInt(st.nextToken()); } } return a; } } class precalculates{ public long gcd(long p, long q) { if (q == 0) return p; else return gcd(q, p % q); } public int[] prefixSumOneDimentional(int a[]){ int n=a.length; int dp[]=new int[n]; for(int i=0;i<n;i++){ if(i==0) dp[i]=a[i]; else dp[i]=dp[i-1]+a[i]; } return dp; } public int[] postSumOneDimentional(int a[]) { int n = a.length; int dp[] = new int[n]; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) dp[i] = a[i]; else dp[i] = dp[i + 1] + a[i]; } return dp; } public int[][] prefixSum2d(int a[][]){ int n=a.length;int m=a[0].length; int dp[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; } } return dp; } public long pow(long a,long b){ long mod=998244353; long ans=1; if(b<=0) return 1; if(b%2==0){ ans=pow(a,b/2)%mod; return ((ans%mod)*(ans%mod))%mod; }else{ ans=pow(a,b-1)%mod; return ((a%mod)*(ans%mod))%mod; } } } class GraphInteger{ HashMap<Integer,vertex> vtces; class vertex{ HashMap<Integer,Integer> children; public vertex(){ children=new HashMap<>(); } } public GraphInteger(){ vtces=new HashMap<>(); } public void addVertex(int a){ vtces.put(a,new vertex()); } public void addEdge(int a,int b,int cost){ if(!vtces.containsKey(a)){ vtces.put(a,new vertex()); } if(!vtces.containsKey(b)){ vtces.put(b,new vertex()); } vtces.get(a).children.put(b,cost); // vtces.get(b).children.put(a,cost); } public boolean isCyclicDirected(){ boolean isdone[]=new boolean[vtces.size()+1]; boolean check[]=new boolean[vtces.size()+1]; for(int i=1;i<=vtces.size();i++) { if (!isdone[i] && isCyclicDirected(i,isdone, check)) { return true; } } return false; } private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){ if(check[i]) return true; if(isdone[i]) return false; check[i]=true; isdone[i]=true; Set<Integer> set=vtces.get(i).children.keySet(); for(Integer ii:set){ if(isCyclicDirected(ii,isdone,check)) return true; } check[i]=false; return false; } } class union_find { int n; int[] sz; int[] par; union_find(int nval) { n = nval; sz = new int[n + 1]; par = new int[n + 1]; for (int i = 0; i <= n; i++) { par[i] = i; sz[i] = 1; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } boolean find(int a, int b) { return root(a) == root(b); } int union(int a, int b) { int ra = root(a); int rb = root(b); if (ra == rb) return 0; if(a==b) return 0; if (sz[a] > sz[b]) { int temp = ra; ra = rb; rb = temp; } par[ra] = rb; sz[rb] += sz[ra]; return 1; } } /* static int mod=998244353; private static int add(int x, int y) { x += y; return x % MOD; } private static int mul(int x, int y) { int res = (int) (((long) x * y) % MOD); return res; } private static int binpow(int x, int y) { int z = 1; while (y > 0) { if (y % 2 != 0) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } private static int inv(int x) { return binpow(x, MOD - 2); } private static int devide(int x, int y) { return mul(x, inv(y)); } private static int C(int n, int k, int[] fact) { return devide(fact[n], mul(fact[k], fact[n-k])); } */
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
059ec7f2608933b793376af05bfbc267
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.io.FileNotFoundException; import java.io.FileReader; import java.math.BigInteger; import java.util.Arrays; public class Main { static long M = 998244353; public static void main(String[] args) { FastInput sc = new FastInput(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); int a[] = sc.readArr(n); sc.sort(a); int j = n-1; int t = 0; long s = a[0]; long f = 0; String ans = "NO"; for(int i = 1;i<n;i++) { s += a[i]; f += a[j--]; if(t<i+1 && f>s) { ans = "YES"; break; } t++; } System.out.println(ans); } } public static boolean isSorted(int a[]) { for(int i =1;i<a.length;i++) { if(a[i-1] > a[i]) return false; } return true; } public static long pow(long a,long b, long M) { if(b == 0) return 1; long res = (pow(a,b/2,M)%M); if(b%2 != 0) return (a%M * res%M * res%M)%M; return (res%M * res%M)%M; } public static long pow(long a,long b) { if(b == 0) return 1; long res = (pow(a,b/2)); if(b%2 != 0) return (a * res* res); return (res * res); } } class Pair implements Comparable<Pair>{ long i; long j; public Pair(long i,long j) { this.i = i; this.j = j; } public Pair(){ } public int compareTo(Pair t) { if(t.i>i) return -1; else if(t.i<i) return 1; //else return 0; } } class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { System.out.println(e.toString()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { System.out.println(e.toString()); } return str; } public static void sort(int[] arr) { 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 void sort(long[] arr) { ArrayList<Long> ls = new ArrayList<Long>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public int [] readArr(int n) { int [] a = new int[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } public long [] readArrL(int n) { long [] a = new long[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } public void swap(int a,int b) { int temp = a; a = b; b = temp; } public static long gcd(long a,long b) { if(b == 0) return a; return gcd(b,a%b); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
17639caed3dffb215754f1664fe97801
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
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.StringTokenizer; public class C774B { static StringBuffer ans1 = new StringBuffer(""); static FastScanner sc = new FastScanner(); static PrintWriter printWriter = new PrintWriter(System.out); public static boolean solve(int n, Integer[] nums) { Arrays.sort(nums); int i = 1, j = n - 1; long left = nums[0] + nums[1], right = nums[j]; while(i < j) { if(left < right) return true; left += nums[++i]; right += nums[--j]; } return false; } public static void main(String[] args) { int t = sc.nextInt(); for (int tt = 0; tt < t; tt++) { int n = sc.nextInt(); Integer[] nums = new Integer[n]; for (int i = 0; i < n; i++) { nums[i] = sc.nextInt(); } printWriter.println(solve(n, nums) ? "YES" : "NO"); } printWriter.flush(); } /***************************************************************** ******************** DO NOT READ AFTER THIS LINE ***************** *****************************************************************/ 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[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] read2dArray(int n, int m) { int arr[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextInt(); } } return arr; } ArrayList<Integer> readArrayList(int n) { ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int a = nextInt(); arr.add(a); } return arr; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
10d6a99936976d09537e0b0ce8933706
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static final int mod=100000007; static final int maxn=2000+10; static IO io; static int n,m; public static void main(String[] args)throws IOException { io=new IO(); int t; t=io.nextInt(); //t=1; while(t>0) { t--; solve(); } io.close(); } private static void solve()throws IOException { n=io.nextInt();//m=io.nextInt(); Integer[] a=new Integer[n]; for(int i=0;i<n;i++) { a[i]=io.nextInt(); } Arrays.sort(a); int l=0,r=n; long x=a[0],y=0; boolean f=false; while(l<r) { if(y>x) { f=true;break; } ++l;--r;x+=a[l];y+=a[r]; } if(f) { io.println("YES"); }else { io.println("NO"); } } static class IO extends PrintWriter { BufferedReader br; StringTokenizer st; // standard input public IO() { this(System.in, System.out); } public IO(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public IO(String problemName) throws IOException { super(problemName + ".out"); br = new BufferedReader(new FileReader(problemName + ".in")); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
3455577368efe79943449b00e7204ba3
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class QualityVSQuantity { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for(int i=0; i<t; i++){ int n = Integer.parseInt(br.readLine()); Integer[] nums = new Integer[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int j=0; j<n; j++){ nums[j] = Integer.parseInt(st.nextToken()); } check(n, nums, pw); } pw.close(); } public static void check(int n, Integer[] nums, PrintWriter pw) { Arrays.sort(nums); long blueSum = nums[0]; long redSum = 0; for(int j = 1; j < n-j; j++) { blueSum += nums[j]; redSum += nums[n - j]; if (blueSum < redSum) { pw.println("YES"); return; } } pw.println("NO"); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
d7a284f832f9b5152067437079216d95
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class QualityQuantity { static long power(long pow, long pow2, long mod) { long res = 1; // Initialize result pow = pow % mod; // Update x if it is more than or // equal to p if (pow == 0) return 0; // In case x is divisible by p; while (pow2 > 0) { // If y is odd, multiply x with result if ((pow2 & 1) != 0) res = (res * pow) % mod; // y must be even now pow2 = pow2 >> 1; // y = y/2 pow = (pow * pow) % mod; } return res; } public static void main(String[] args) { // TODO Auto-generated method stub StringBuilder st = new StringBuilder(); FastReader sc = new FastReader(); 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(); } Arrays.sort(arr); if(n<=2) { st.append("NO"+"\n"); } else{ // long left_sum=arr[0]+arr[1]; // long right_sum=arr[n-1]; // int i=2; // int j=n-2; // if(left_sum<right_sum) // { // st.append("YES"+"\n"); // } // else{ // boolean flag=false; // while(i<j) // { // if(left_sum<right_sum) // { // // st.append("YES"+"\n"); // flag=true; // break; // } // left_sum+=arr[i]; // right_sum+=arr[j]; // i++; // j--; // } // if(flag||left_sum<right_sum) // { // st.append("YES"+"\n"); // } // else // { // st.append("NO"+"\n"); // } // } // boolean flag=false; // while(i<j) // { // if(left_sum<right_sum) // { // flag=true; // break; // } // left_sum+=arr[i]; // right_sum+=arr[j]; // i++; // j--; // } // if(flag||right_sum>left_sum) // { // st.append("YES"+"\n"); // } // else{ // st.append("NO"+"\n"); // } long prefix[]=new long[n+1]; Arrays.fill(prefix,0); for(int i=1;i<=n;i++) { prefix[i]=prefix[i-1]+arr[i-1]; } boolean flag=false; for(int i=2;i<=n;i++) { long left=prefix[i]; long right=prefix[n]-prefix[n-i+1]; if(right>left) { flag=true; break; } } if(flag) { st.append("YES"+"\n"); } else{ st.append("NO"+"\n"); } } } System.out.println(st); } static long pow(long n) { long ans=n*n; return ans; } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
53258a3bd085d994624a9947dbe8c0c0
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
// Working program with FastReader import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.lang.*; public class B_Quality_vs_Quantity { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); ArrayList<Integer> arr=new ArrayList<>(); // int arr[]=new int[n]; for(int i=0;i<n;i++){ arr.add(sc.nextInt()); } if(n<3) { System.out.println("NO"); continue; } Collections.sort(arr); boolean flag=false; long sR=0,sB=0; sB=arr.get(0); int i=1,j=n-1; while(i<j){ sR+=arr.get(j); sB+=arr.get(i); if(sR>sB){ flag=true; break; } ++i; --j; } if(flag) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
0cc76cbdfda508e0d3260fb9740d8965
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; import java.util.stream.Collectors; public class Main { static Scanner s = new Scanner(System.in); public static void main(String[] args) { int test_cases = s.nextInt(); for (int i = 0; i < test_cases; i++) { test_output(); } } static void test_output() { int n = s.nextInt(); // if there's a set of sum bigger than another set of sum and has lesser elements Integer[] array = readArray(n); Arrays.sort(array); long sumMax, sumMin; sumMax = array[array.length - 1]; sumMin = array[0] + array[1]; if (sumMax > sumMin) { YES(); return; } int startIndex = 2; int endIndex = array.length - 2; while (endIndex - startIndex > 0) { sumMax += array[endIndex]; sumMin += array[startIndex]; if (sumMax > sumMin) { YES(); return; } endIndex--; startIndex++; } NO(); } static Integer[] readArray(int n) { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = s.nextInt(); } return array; } static void NO() { System.out.println("NO"); } static void YES() { System.out.println("YES"); } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
4b876c59392c2def996cb460633b09e0
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int n=sc.nextInt(), j=0; int[] a=new int[n+5]; for(j=1;j<=n;j++) a[j]=sc.nextInt(); shuffle(a,n); Arrays.sort(a,1,n+1); long ans1=a[1]+a[2], ans2=a[n]; int now1=3, now2=n-1, suc=0; while(now1<now2) { if(suc==1) break; if(ans1<ans2) { System.out.println("YES"); suc=1; break; } ans1+=a[now1]; ans2+=a[now2]; now1++; now2--; } if(ans1<ans2 && suc==0) { System.out.println("YES"); suc=1; } if(suc==0) System.out.println("NO"); } } static void shuffle(int[] a,int n) { Random r=new Random(); for(int i=1;i<=n;i++) { int j=r.nextInt(i)+1; int temp=a[i]; a[i]=a[j]; a[j]=temp; } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
042e6d2c870e86b75b1ce31c51342dd4
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codeforces{ static int[] par; static int[] ranked; public static int find(int x) { if(par[x]==x) return x; return par[x]=find(par[x]); } public static void union(int x,int y) { int lx=find(x); int ly=find(y); if(lx!=ly) { if(ranked[lx]>ranked[ly]) { par[ly]=lx; }else if(ranked[ly]>ranked[lx]) { par[lx]=ly; }else{ par[lx]=ly; ranked[ly]++; } } } public static void dfs(int src,boolean[] vis,ArrayList<Integer>[] graph) { vis[src]=true; for(int nbr: graph[src]) { if(vis[nbr]==false) { dfs(nbr,vis,graph); } } } public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); Long[] arr = new Long[n]; String[] in = br.readLine().split(" "); for(int i = 0; i < n; i++) { arr[i] = Long.parseLong(in[i]); } Arrays.sort(arr); boolean flag = false; long sum = 0; for(int i = 0; i < n && i+1<n-i-1 && flag == false; i++) { sum += arr[n-i-1] - arr[i]; if(sum > arr[i+1]) { flag = true; } } if(flag == false) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output
PASSED
718f8cdce7cce3410bc82dea0083122a
train_110.jsonl
1646408100
$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) &gt; \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) &lt; \text{Count}(\BLUE)$$$.
256 megabytes
import java.util.Scanner; import java.util.Arrays; import java.util.Collections; // import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.ArrayList; import java.util.Set; import java.util.Stack; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; // import java.util.Set; // import java.util.TreeSet; // import java.util.stream.Collectors; // import java.util.stream.Stream; // import java.util.Stack; // import java.util.Optional; // import java.util.HashSet; import java.io.*; public class sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int ar[] = new int[n]; long sum=0; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); sum+=ar[i]; } // Arrays.sort(ar,(i1,i2)->i1<i2?1:-1); int[] sorted = Arrays.stream(ar).boxed().sorted(Collections.reverseOrder()).mapToInt(x -> x).toArray(); boolean flag=true; long tempSum=0; sum=0; int i=0; int j=n-2; tempSum+=sorted[n-1]; int in=((n/2)+n%2)-1; // for(int a : sorted) System.out.print(a+" "); // System.out.println("in="+in); while(i<in||j>=in){ // System.out.println("i="+i+" j="+j); if(i<in){ sum+=sorted[i]; } if(j>=in){ tempSum+=sorted[j]; } i+=1; j-=1; if(sum>tempSum){ break; } // 0 1 2 3 4 5 // // if(i==((n/2)+n%2)-1) break; } // System.out.println("sum of more lements="+tempSum+" sum of less="+sum); if(sum>tempSum) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"]
2 seconds
["NO\nYES\nNO\nNO"]
NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 &gt; \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 &lt; \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 &gt; \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
Java 11
standard input
[ "brute force", "constructive algorithms", "greedy", "sortings", "two pointers" ]
4af59df1bc56ca8eb5913c2e57905922
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
800
For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
standard output