exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
acf9b50b29a6484a802fc4ffdd42ad62
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.net.ssl.SSLContext; public class Main { static long mod=(long)998244353; static long inf=1000000007; static int[] sieve; static ArrayList<Integer> primes; public static void main(String[] args) throws java.lang.Exception { fast s = new fast(); PrintWriter out=new PrintWriter(System.out); Scanner sc=new Scanner(System.in); StringBuilder fans = new StringBuilder(); int n=s.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) {a[i]=s.nextLong();if(a[i]%2==0) a[i]=0; else a[i]=1;} LinkedList<Long> st=new LinkedList<Long>(); st.push(a[0]); for(int i=1;i<n;i++) { if(!st.isEmpty() && st.peek()==a[i]) st.poll(); else st.push(a[i]); } if(st.size()<=1) System.out.println("YES"); else System.out.println("NO"); } static class fast { private InputStream i; private byte[] buf = new byte[1024]; private int curChar; private int numChars; //Return floor log2n public static int log2(int bits) // returns 0 for bits=0 { int log = 0; if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; } if( bits >= 256 ) { bits >>>= 8; log += 8; } if( bits >= 16 ) { bits >>>= 4; log += 4; } if( bits >= 4 ) { bits >>>= 2; log += 2; } return log + ( bits >>> 1 ); } public static boolean next_permutation(int a[]) { int i=0,j=0;int index=-1; int n=a.length; for(i=0;i<n-1;i++) if(a[i]<a[i+1]) index=i; if(index==-1) return false; i=index; for(j=i+1;j<n && a[i]<a[j];j++); int temp=a[i]; a[i]=a[j-1]; a[j-1]=temp; for(int p=i+1,q=n-1;p<q;p++,q--) { temp=a[p]; a[p]=a[q]; a[q]=temp; } return true; } public static void division(char ch[],int divisor) { int div=Character.getNumericValue(ch[0]); int mul=10;int remainder=0; StringBuilder quotient=new StringBuilder(""); for(int i=1;i<ch.length;i++) { div=div*mul+Character.getNumericValue(ch[i]); if(div<divisor) {quotient.append("0");continue;} quotient.append(div/divisor); div=div%divisor;mul=10; } remainder=div; while(quotient.charAt(0)=='0')quotient.deleteCharAt(0); System.out.println(quotient+" "+remainder); } public static void sieve(int size) { sieve=new int[size+1]; primes=new ArrayList<Integer>(); sieve[1]=1; for(int i=2;i<=Math.sqrt(size);i++) { if(sieve[i]==0) { for(int j=i*i;j<size;j+=i) sieve[j]=1; } } for(int i=2;i<=size;i++) { if(sieve[i]==0) primes.add(i); } } public static long pow(long n, long b, long MOD) { long x=1;long y=n; while(b > 0) { if(b%2 == 1) { x=x*y; if(x>MOD) x=x%(MOD); } y = y*y; if(y>MOD) y=y%(MOD); b >>= 1; } return x; } public static long mod_inv(long n,long mod) { return pow(n,mod-2,mod); } public static int upper(Integer[] a,int start,int end,int key) { int mid=(start+end)>>1; if(start==end && a[mid]<key) {return -1;} if(start>end) return -1; if(a[mid]>=key && (((mid-1)>=0 && a[mid-1]<key) || (mid-1)==0)) return mid; else if(a[mid]== key && (mid-1)>=0 && a[mid-1]==key) return lower(a,start,mid-1,key); else if(key>a[mid]) return lower(a,mid+1,end,key); else return lower(a,start,mid-1,key); } public static int lower(Integer a[],int start,int end,int key) { int mid=(start+end)>>1; if(start==end && a[mid]>key) {return -1;} if(start>end) return -1; if(a[mid]<=key && (((mid+1)<a.length && a[mid+1]>key) || (mid+1)==a.length)) return mid; else if(a[mid]== key && (mid+1)<a.length && a[mid+1]==key) return upper(a,mid+1,end,key); else if(key>=a[mid]) return upper(a,mid+1,end,key); else return upper(a,start,mid-1,key); } public int gcd(int a,int b) { if(a==0) return b; return gcd(b%a,a); } public fast() { this(System.in); } public fast(InputStream is) { i = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = i.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
74e17503b6c46d6ecaa8174edd9f692f
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter pw = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); int[] a = sc.nextIntArray(n); Stack<Integer> stk = new Stack<Integer>(); for(int i = 0; i < n; i++){ int next = a[i]%2; if(stk.size() != 0){ int now = stk.peek(); if(now != next){ stk.push(next); }else{ stk.pop(); } }else{ stk.push(next); } } pw.println(stk.size() <= 1 ? "YES": "NO"); pw.flush(); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class Str implements Comparable<Str>{ int i,len; char[] s; public Str(int i, int l, char[] s){ this.i = i; this.len = l; this.s = s; } public int compareTo(Str s){ if(this.len > s.len){ return -1; }else if(this.len < s.len){ return 1; }else{ return 0; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
ea3cd6d0dbdd9d0e9625f287958bf580
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package ; import java.io.*; import java.util.*; public class D1 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n=sc.nextInt(); int[]a=new int[n]; Stack<Integer>st=new Stack<>(); a[0]=sc.nextInt(); st.push(a[0]%2); for(int i=1;i<n;i++) { a[i]=sc.nextInt(); if(!st.isEmpty() && a[i]%2==st.peek()) st.pop(); else st.add(a[i]%2); } pw.print(st.size()<=1?"YES":"NO"); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasnext() throws IOException{ return br.ready(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
11ce8f8ddeb3dbf0cb377a24b06d2237
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package ; import java.io.*; import java.util.*; public class D1 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n=sc.nextInt(); Stack<Integer>st=new Stack<>(); int x=sc.nextInt(); st.push(x%2); for(int i=1;i<n;i++) { x=sc.nextInt(); if(!st.isEmpty() && x%2==st.peek()) st.pop(); else st.add(x%2); } pw.print(st.size()<=1?"YES":"NO"); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasnext() throws IOException{ return br.ready(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
2e9f99c774bc61fe5e1d313dc7f12f44
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.util.Vector; import java.io.BufferedInputStream; import java.util.Stack; 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; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); D1GreatVovaWallVersion1 solver = new D1GreatVovaWallVersion1(); solver.solve(1, in, out); out.close(); } static class D1GreatVovaWallVersion1 { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.scanInt(); } if (n == 1) { out.println("YES"); return; } else if (n == 2) { if (Math.abs(arr[0] - arr[1]) % 2 == 0) { out.println("YES"); return; } else { out.println("NO"); return; } } Stack<Integer> st = new Stack<>(); for (int i = 0; i < n; i++) { if (st.isEmpty()) st.push(arr[i] % 2); else { if (st.peek() == (arr[i] % 2)) st.pop(); else st.push(arr[i] % 2); } } if (st.size() > 1) out.println("NO"); else out.println("YES"); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 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') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
ee35f2024369f856174bc1908307715b
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class Exam { public static long mod = (long) Math.pow(10, 9)+7 ; public static InputReader sc = new InputReader(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { // Code starts.. int n=sc.nextInt(); //int a[]=scanArray(n); int s[]=new int [n]; int t=0; s[0]=sc.nextInt(); for(int i=1;i<n;i++){ s[++t]=sc.nextInt(); if(t>0&&(s[t]-s[t-1])%2==0) t-=2; } if(t==-1||t==0) pw.println("YES"); else pw.println("NO"); // Code ends... pw.flush(); pw.close(); } public static String reverseString(String s){ StringBuilder input1 = new StringBuilder(); input1.append(s); input1 = input1.reverse(); return input1.toString(); } public static int[] scanArray(int n){ int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); return a; } public static long[] scanLongArray(int n){ long a[]=new long [n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); return a; } public static String [] scanStrings(int n){ String a[]=new String [n]; for(int i=0;i<n;i++) a[i]=sc.nextLine(); return a; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { 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 = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
4780232995dd4cf6b2435dde1b01588d
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.util.*; import org.omg.Messaging.SyncScopeHelper; public class tr { static int n,k; static boolean []vis; static ArrayList<Integer> []ad; public static void main(String[] args) throws IOException, InterruptedException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); int []a=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); boolean f=false; Stack<Integer> s=new Stack<Integer>(); for(int i=0;i<n;i++) { if(!s.isEmpty() && s.peek()%2==a[i]%2) { s.pop(); } else s.push(a[i]%2); } if(s.size()>1) { System.out.println("NO"); return; } System.out.println("YES"); out.flush(); } static long fast_pow(long a, long b) { if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int c; static void dfs(int s) { c++; vis[s]=true; for(int u:ad[s]) if(!vis[u]) dfs(u); } static long cal(int r) { long ans=r; int u=k; while(--u>0) { ans=(ans*r)%mod; } return ans; } static long mod=(long)1e9+7; static class pair2 implements Comparable<pair2> { long a; long b; pair2(long a, long b) { this.a = a; this.b = b; } public String toString() { return a + " " + b; } public int compareTo(pair2 o) { if(o.b<b) return 1; else if(o.b>b) return -1; return 0; } } static class pair implements Comparable<pair> { long a; int b; pair(long a, int b) { this.a = a; this.b = b; } public String toString() { return a + " " + b; } @Override public int compareTo(pair o) { //if(o.a!=a) //return a-o.a ; return b-o.b; } } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
4ed14d627244ae22e2a58c81869bd03b
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.Stack; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Random; import java.io.PrintWriter; /* Solution Created: 06:01:35 21/07/2019 Custom Competitive programming helper. */ public class Main { public static void solve(Reader in, Writer out) { int n = in.nextInt(); int[] a = in.na(n); Stack<Integer> s = new Stack<>(); for(int i = 0; i<n; i++) { int nxt = a[i]; if(!s.isEmpty()&&Math.abs(s.peek()-nxt)%2==0) { s.pop(); }else s.push(nxt); } boolean can = false; if(s.size()<=1) can = true; out.println(can?"YES":"NO"); } public static void main(String[] args) { Reader in = new Reader(); Writer out = new Writer(); solve(in, out); out.exit(); } static class Graph { private ArrayList<Integer> con[]; private boolean[] visited; public Graph(int n) { con = new ArrayList[n]; for (int i = 0; i < n; ++i) con[i] = new ArrayList(); visited = new boolean[n]; } public void reset() { Arrays.fill(visited, false); } public void addEdge(int v, int w) { con[v].add(w); } public void dfs(int cur) { visited[cur] = true; for (Integer v : con[cur]) { if (!visited[v]) { dfs(v); } } } public void bfs(int cur) { Queue<Integer> q = new LinkedList<>(); q.add(cur); visited[cur] = true; while (!q.isEmpty()) { cur = q.poll(); for (Integer v : con[cur]) { if (!visited[v]) { visited[v] = true; q.add(v); } } } } } static class Reader { static BufferedReader br; static StringTokenizer st; private int charIdx = 0; private String s; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public char nextChar() { if (s == null || charIdx >= s.length()) { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } s = st.nextToken(); charIdx = 0; } return s.charAt(charIdx++); } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] nS(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } return Integer.parseInt(st.nextToken()); } public double nextDouble() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } return Double.parseDouble(st.nextToken()); } public Long nextLong() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } return Long.parseLong(st.nextToken()); } public String next() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } return st.nextToken(); } public static void readLine() { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } } } static class Sort { static Random random = new Random(); public static void sortArray(int[] s) { shuffle(s); Arrays.sort(s); } public static void sortArray(long[] s) { shuffle(s); Arrays.sort(s); } public static void sortArray(String[] s) { shuffle(s); Arrays.sort(s); } public static void sortArray(char[] s) { shuffle(s); Arrays.sort(s); } private static void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } private static void shuffle(long[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); long t = s[i]; s[i] = s[j]; s[j] = t; } } private static void shuffle(String[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); String t = s[i]; s[i] = s[j]; s[j] = t; } } private static void shuffle(char[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); char t = s[i]; s[i] = s[j]; s[j] = t; } } } static class Util{ static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static int upperBound(long[] a, long v) { int l=0, h=a.length-1, ans = -1; while(l<h) { int mid = (l+h)/2; if(a[mid]<=v) { ans = mid; l = mid+1; }else h = mid-1; } return ans; } public static int lowerBound(long[] a, long v) { int l=0, h=a.length-1, ans = -1; while(l<h) { int mid = (l+h)/2; if(v<=a[mid]) { ans = mid; h = mid-1; }else l = mid-1; } return ans; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } public static long modAdd(long a, long b, long mod) { return (a%mod+b%mod)%mod; } public static long modMul(long a, long b, long mod) { return ((a%mod)*(b%mod))%mod; } public static void dbg(Object... o) { System.out.println(Arrays.deepToString(o)); } } static class Writer { private PrintWriter pw; public Writer(){ pw = new PrintWriter(System.out); } public void printArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void printArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void print(Object o) { pw.print(o.toString()); } public void println(Object o) { pw.println(o.toString()); } public void println() { pw.println(); } public void flush() { pw.flush(); } public void exit() { pw.close(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
7ba73341a4972f44129ed190be0bedb8
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.InputMismatchException; import java.io.IOException; import java.util.function.Function; import java.util.Stack; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author MaxHeap */ 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); D1GreatVovaWallVersion1 solver = new D1GreatVovaWallVersion1(); solver.solve(1, in, out); out.close(); } static class D1GreatVovaWallVersion1 { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); ArrayUtils.transform(arr, value -> value % 2); Stack<Integer> st = new Stack<>(); for (int i = 0; i < n; ++i) { if (st.isEmpty() || st.peek() != arr[i]) { st.add(arr[i]); } else { st.pop(); } } out.println(st.size() <= 1 ? "YES" : "NO"); } } static class InputReader implements FastIO { private InputStream stream; private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final int EOF = -1; private byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == EOF) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return EOF; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } static class ArrayUtils { public static void transform(int[] arr, Function<Integer, Integer> transformer) { for (int i = 0; i < arr.length; ++i) { arr[i] = transformer.apply(arr[i]); } } } static interface FastIO { } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
b589ff2ea2b5994096fcf713c200b8ec
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Stack; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Washoum */ public class main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; inputClass in = new inputClass(inputStream); PrintWriter out = new PrintWriter(outputStream); D2GreatVovaWallVersion2 solver = new D2GreatVovaWallVersion2(); solver.solve(1, in, out); out.close(); } static class D2GreatVovaWallVersion2 { public void solve(int testNumber, inputClass sc, PrintWriter out) { int n = sc.nextInt(); int[] tab = new int[n]; for (int i = 0; i < n; i++) { tab[i] = sc.nextInt(); } int max = 0; int[] buil = new int[n]; for (int i = 0; i < n; i++) { if (tab[i] > tab[max]) { max = i; } } int nbmax = 0; for (int i = 0; i < n; i++) { if (tab[i] == tab[max]) nbmax++; } boolean cbon = true; for (int i = 0; i < n; i++) { if (buil[i] == 0) { cbon = false; break; } } if (cbon) { out.println("YES"); return; } Stack<Integer> stack = new Stack<>(); cbon = true; for (int i = 0; i < n; i++) { if (stack.size() == 0) { stack.push(tab[i]); } else { if (stack.peek() != tab[i]) { if (Math.abs(tab[i]-stack.peek())%2==0) stack.pop(); else stack.push(tab[i]); } else if (stack.peek() == tab[i]) { stack.pop(); } } } if (stack.size()>1) { out.println("NO"); } else out.println("YES"); } } static class inputClass { BufferedReader br; StringTokenizer st; public inputClass(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
980064e184ce022e917f1c788d06bd78
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayDeque; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ 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); D1GreatVovaWallVersion1 solver = new D1GreatVovaWallVersion1(); solver.solve(1, in, out); out.close(); } static class D1GreatVovaWallVersion1 { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); ArrayDeque<pair> pairs = new ArrayDeque<>(); for (int i = 0; i < n - 1; i++) { if (a[i] % 2 == a[i + 1] % 2) { pairs.add(new pair(i, i + 1)); } } TreeSet<Integer> ts = new TreeSet<>(); for (int i = 0; i < n; i++) { ts.add(i); } while (!pairs.isEmpty()) { pair cp = pairs.removeFirst(); if (a[cp.l] % 2 == a[cp.r] % 2 && ts.contains(cp.l) && ts.contains(cp.r)) { ts.remove(cp.l); ts.remove(cp.r); Integer li = ts.floor(cp.l - 1); Integer ri = ts.ceiling(cp.r + 1); if (li != null && ri != null) { if (a[li] % 2 == a[ri] % 2) { pairs.addLast(new pair(li, ri)); } } } } if (ts.size() <= 1) { out.println("YES"); } else { out.println("NO"); } } class pair { int l; int r; public pair(int l, int r) { this.l = l; this.r = r; } } } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
5d8ae1191cf3d843a93a39cdd4c6be5e
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayDeque; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ 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); D1GreatVovaWallVersion1 solver = new D1GreatVovaWallVersion1(); solver.solve(1, in, out); out.close(); } static class D1GreatVovaWallVersion1 { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); ArrayDeque<pair> pairs = new ArrayDeque<>(); for (int i = 0; i < n - 1; i++) { if (a[i] % 2 == a[i + 1] % 2) { pairs.add(new pair(i, i + 1)); } } TreeSet<Integer> ts = new TreeSet<>(); for (int i = 0; i < n; i++) { ts.add(i); } while (!pairs.isEmpty()) { pair cp = pairs.removeFirst(); if (a[cp.l] % 2 == a[cp.r] % 2 && ts.contains(cp.l) && ts.contains(cp.r)) { ts.remove(cp.l); ts.remove(cp.r); Integer li = ts.floor(cp.l - 1); Integer ri = ts.ceiling(cp.r + 1); if (li != null && ri != null) { if (a[li] % 2 == a[ri] % 2) { pairs.addLast(new pair(li, ri)); } } } } if (ts.size() <= 1) { out.println("YES"); } else { out.println("NO"); } } class pair { int l; int r; public pair(int l, int r) { this.l = l; this.r = r; } } } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
14a8d2e58856cd627f81e0b5d68ebb2a
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) throws Exception { Scanner s=new Scanner(System.in); int n=s.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } Stack<Integer> st=new Stack<>(); if(arr[0]%2==0) st.add(2); else st.add(1); for(int i=1;i<n;i++) { if(arr[i]%2==0) { if(st.size()>=1&&st.get(st.size()-1)==2) { st.pop(); }else { st.add(2); } }else { if(st.size()>=1&&st.get(st.size()-1)==1) { st.pop(); }else { st.add(1); } } } if(st.size()<=1) { System.out.println("YES"); }else { System.out.println("NO"); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
b7f4da94bf0152c5fac67e544ff67aa6
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; /** * @author: Muhammadjon Hakimov * created: 21.04.2019 19:06:35 */ public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } Stack<Integer> stack = new Stack<>(); int i = 1; stack.push(a[0] % 2); while (i < n) { if (!stack.empty() && a[i] % 2 == stack.peek()) { stack.pop(); } else { stack.push(a[i] % 2); } i++; } if (stack.size() > 1) { System.out.println("NO"); } else { System.out.println("YES"); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
44b0ca480a197f9cbb898b90ed3d8635
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] str = (br.readLine()).trim().split(" "); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = Integer.parseInt(str[i])%2; Stack<Integer> st = new Stack<>(); for(int i=0;i<n;i++) { if(st.isEmpty()) st.push(arr[i]); else if(arr[i] == st.peek()) st.pop(); else st.push(arr[i]); } if(st.size() <= 1) System.out.println("YES"); else System.out.println("NO"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
ae7ba25e049ffde91ae4b55fd2910c78
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by vaibhav on 20/12/18. */ public class CF { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int n = Integer.parseInt(br.readLine()); String[] as = br.readLine().split(" "); int[] a = new int[n]; Stack st = new Stack(n); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(as[i]); Wall wall = new Wall(1, a[i]); if(!st.isEmpty()) { while(!st.isEmpty()) { Wall mergedWall = WallMerger.mergeWalls(wall, st.peak()); if(mergedWall!=null) { st.pop(); wall = mergedWall; } else { break; } } st.push(wall); } else { st.push(wall); } } Wall finalWallCandidate = st.pop(); if(st.isEmpty()) { System.out.println("YES"); } else { System.out.println("NO"); } } } class Stack { private Wall[] s; private int size; public Stack(int n) { s = new Wall[n]; size = 0; } public boolean isEmpty() { return size==0; } public void push(Wall k) { s[size] = k; size++; } public Wall pop() { Wall k = s[size-1]; size--; return k; } public Wall peak() { return s[size-1]; } } class Wall{ private int w; private int h; public Wall(int w, int h) { this.h = h; this.w = w; } public int getH() { return h; } public int getW() { return w; } } class WallMerger { public static Wall mergeWalls(Wall wall1, Wall wall2) { if(wall1.getW()%2==0 && wall2.getW()%2==0) { return new Wall(wall1.getW()+wall2.getW(), Math.max(wall1.getH(), wall2.getH())); } else if(wall1.getW()%2==1 && wall2.getW()%2==1) { if(wall1.getH()==wall2.getH()) { return new Wall(wall1.getW()+wall2.getW(), wall1.getH()); } else { if((wall1.getH()-wall2.getH())%2==0) { return new Wall(wall1.getW() + wall2.getW() , Math.max(wall1.getH(), wall2.getH())); } else { return null; } } } else { Wall evenWall, oddWall; if(wall1.getW()%2==1) { oddWall = wall1; evenWall = wall2; } else { oddWall = wall2; evenWall = wall1; } if(evenWall.getH() <= oddWall.getH()) { return new Wall(evenWall.getW()+oddWall.getW(), oddWall.getH()); } else { return new Wall(evenWall.getW()+oddWall.getW(), evenWall.getH()+Math.abs((evenWall.getH()-oddWall.getH())%2)); } } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
86f3d5dcf9e7e2f19b9e307dbb521aa4
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by vaibhav on 20/12/18. */ public class CF { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int n = Integer.parseInt(br.readLine()); String[] as = br.readLine().split(" "); int[] a = new int[n]; Stack st = new Stack(n); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(as[i]); Wall wall = new Wall(1, a[i]); if(!st.isEmpty()) { while(!st.isEmpty()) { Wall mergedWall = WallMerger.mergeWalls(wall, st.peak()); if(mergedWall!=null) { st.pop(); wall = mergedWall; } else { break; } } } st.push(wall); } Wall finalWallCandidate = st.pop(); System.out.println(st.isEmpty() ? "YES" : "NO"); } } class Stack { private Wall[] s; private int size; public Stack(int n) { s = new Wall[n]; size = 0; } public boolean isEmpty() { return size==0; } public void push(Wall k) { s[size] = k; size++; } public Wall pop() { Wall k = s[size-1]; size--; return k; } public Wall peak() { return s[size-1]; } } class Wall{ private int w; private int h; public Wall(int w, int h) { this.h = h; this.w = w; } public int getH() { return h; } public int getW() { return w; } } class WallMerger { public static Wall mergeWalls(Wall wall1, Wall wall2) { if(wall1.getW()%2==0 && wall2.getW()%2==0) { return new Wall(wall1.getW()+wall2.getW(), Math.max(wall1.getH(), wall2.getH())); } else if(wall1.getW()%2==1 && wall2.getW()%2==1) { if(wall1.getH()==wall2.getH()) { return new Wall(wall1.getW()+wall2.getW(), wall1.getH()); } else { if((wall1.getH()-wall2.getH())%2==0) { return new Wall(wall1.getW() + wall2.getW() , Math.max(wall1.getH(), wall2.getH())); } else { return null; } } } else { Wall evenWall, oddWall; if(wall1.getW()%2==1) { oddWall = wall1; evenWall = wall2; } else { oddWall = wall2; evenWall = wall1; } if(evenWall.getH() <= oddWall.getH()) { return new Wall(evenWall.getW()+oddWall.getW(), oddWall.getH()); } else { return new Wall(evenWall.getW()+oddWall.getW(), evenWall.getH()+Math.abs((evenWall.getH()-oddWall.getH())%2)); } } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
52e0a2ff6f003673fab9092b2f5d8344
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // InputStream uinputStream = new FileInputStream("lightsout.in"); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("lightsout.out"))); Task t = new Task(); t.solve(in, out); out.close(); } static class Task { class BIT{ int arr[]; int n; public BIT(int a) { n=a; arr = new int[n]; } int sum(int p) { int s=0; while(p>0) { s+=arr[p]; p-=p&(-p); } return s; } void add(int p, int v) { while(p<n) { arr[p]+=v; p+=p&(-p); } } } class DSU{ int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for(int i=0;i<n;i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if(arr[a]!=a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if(x==y) return; arr[x] = y; sz[y] += sz[x]; } public int size(int x) { return sz[find(x)]; } } ArrayList<Integer> x = new ArrayList<Integer>(); public void solve(InputReader in, PrintWriter out) throws IOException { // Dumper.print_int_arr(movesToStamp("mda","mdadddaaaa")); // Dumper.print_int_arr(movesToStamp("aye","eyeye")); // Dumper.print_int_arr(movesToStamp("df","dfdff")); // Dumper.print_int_arr(movesToStamp("aye","eyeye")); // Dumper.print_int_arr(movesToStamp("oz","ooozz")); // Dumper.print_int_arr(movesToStamp("abca","aabcaca")); // Dumper.print_int_arr(movesToStamp("cab","cabbb")); // Dumper.print_int_arr(movesToStamp("aq","aqaaqaqqqaqqaaq")); // int arr[] = {2,3,6,7,4,12,21,39}; //int[] arr = {1,2,4,8,16,32,64,128,256,512,50,50,50,150,150,150,100,100,100,123}; int n = in.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = in.nextInt()%2; int c[] = new int[n]; int p=-1; for(int i=0;i<n;i++) { if(p==-1||c[p]!=arr[i]) c[++p] = arr[i]; else p--; } if(p<=0) { Dumper.print("YES"); }else { Dumper.print("NO"); } } public class subs{ String val; int id; public subs(String a, int b) { val=a;id=b; } } public int[] movesToStamp(String stamp, String target) { int n = stamp.length(); int m = target.length(); char[] s = stamp.toCharArray(); char[] t = target.toCharArray(); ArrayList<Integer> part1 = new ArrayList<Integer>(); ArrayList<Integer> part2 = new ArrayList<Integer>(); ArrayList<Integer> part3 = new ArrayList<Integer>(); int p = target.indexOf(stamp); if(p==-1) return new int[] {}; part1.add(p); update(t,p,p+stamp.length()); while(true) { int x = target.indexOf(stamp,p+stamp.length()); if(x==-1) break; update(t,x,x+stamp.length()); part1.add(0,x); p=x; } TreeSet<Integer> todo = new TreeSet<Integer>(Collections.reverseOrder()); for(int i=0;i<m;i++) if(t[i]!='*') todo.add(i); while(true) { int temp = -1; for(int i:todo) { if(i+1<m&&t[i+1]=='*') { temp = i; int tmp = stamp.indexOf(t[i]); if(i-tmp<0) return new int[] {}; part2.add(i-tmp); t[i]='*'; break; } } if(temp==-1) break; else todo.remove(temp); } while(!todo.isEmpty()) { int l = 0; int end = 0; for(int i:todo) { end=i; l = backmatch(s,t,i); part3.add(i-n+1); update(t,i-l+1,i); break; } if(l==0) return new int[] {}; else { for(int j=end-l+1;j<=end;j++) todo.remove(j); } } int[] ret = new int[part1.size()+part2.size()+part3.size()]; int c=0; for(int i:part3) { ret[c++] = i; } for(int i:part2) { ret[c++] = i; } for(int i:part1) { ret[c++] = i; } return ret; } public int backmatch(char s[], char t[], int idx) { int q = s.length-1; int c = 0; while(q>0&&idx>0&&s[q]==t[idx]) { q--;idx--;c++; } return c; } public void update(char[] arr, int s, int l) { for(int i=s;i<l;i++) arr[i] = '*'; } } static class MinHeap<Key> implements Iterable<Key> { private int maxN; private int n; private int[] pq; private int[] qp; private Key[] keys; private Comparator<Key> comparator; public MinHeap(int capacity){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); } public MinHeap(int capacity, Comparator<Key> c){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); comparator = c; } public boolean isEmpty() { return n==0; } public int size() { return n; } public boolean contains(int i) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); return qp[i] != -1; } public int peekIdx() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } public Key peek(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } public int poll(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1,n--); down(1); assert min==pq[n+1]; qp[min] = -1; keys[min] = null; pq[n+1] = -1; return min; } public void update(int i, Key key) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (!contains(i)) { this.add(i, key); }else { keys[i] = key; up(qp[i]); down(qp[i]); } } private void add(int i, Key x){ if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = x; up(n); } private void up(int k){ while(k>1&&less(k,k/2)){ exch(k,k/2); k/=2; } } private void down(int k){ while(2*k<=n){ int j=2*k; if(j<n&&less(j+1,j)) j++; if(less(k,j)) break; exch(k,j); k=j; } } public boolean less(int i, int j){ if (comparator == null) { return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0; } else { return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0; } } public void exch(int i, int j){ int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } @Override public Iterator<Key> iterator() { // TODO Auto-generated method stub return null; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } 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 { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
8d64c19cd6c908dba5c26e412200dca9
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; public class CodeForces1092D1{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = input.nextInt(); } Stack<Integer> st=new Stack<>(); for(int i=0;i<n;i++){ if(!st.isEmpty() && st.peek()==arr[i]%2){ while(!st.isEmpty() && st.peek()==arr[i]%2){ st.pop(); } } else{ st.push(arr[i]%2); } } System.out.println(st.size()<=1?"YES":"NO"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
7c65471ad3fd2fd7f58c571bfc921d48
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package All_in_all; import java.io.*; import java.util.Arrays; import java.util.HashSet; import java.util.Stack; /** * Created by nikitos on 25.08.17. */ public class simvoli { public StreamTokenizer t; public int nextInt() throws IOException { t.nextToken(); return (int) t.nval; } public long nextLong() throws IOException { t.nextToken(); return (long) t.nval; } public String nextString() throws IOException { t.nextToken(); return t.sval; } public void start() throws IOException { t = new StreamTokenizer( new BufferedReader(new InputStreamReader(System.in))); int n = nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt() % 2; } Stack<Integer> st = new Stack<>(); for (int i = 0; i < n; i++) { if (!st.empty() && st.peek() == arr[i]) { st.pop(); } else { st.push(arr[i]); } } System.out.println(st.size() > 1? "NO" : "YES"); } public static void main(String[] args) throws IOException { new simvoli().start(); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
aeb27ce6d556534f7421486262281599
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.awt.Point; public class Main { //static final long MOD = 998244353L; //static final long INF = 100000000000007L; static final long MOD = 1000000007L; //static final int INF = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int N = sc.ni(); int odds = 0; int evens = 0; int[] nums = new int[N]; for (int i = 0; i < N; i++) { nums[i] = sc.ni()%2; if (nums[i] == 0) evens++; else odds++; } StringBuilder parities = new StringBuilder(); for (int par: nums) { if (parities.length() == 0) { parities.append(par); } else if (par == ((parities.charAt(parities.length()-1)-'0') )){ parities = parities.deleteCharAt(parities.length()-1); } else { parities.append(par); } } if (parities.length() > 1) { pw.println("NO"); } else { pw.println("YES"); } pw.close(); } static class DisjointSetUnion { public int[] parent; public int[] weight; public int count; public DisjointSetUnion(int nodes) { count = nodes; parent = new int[nodes]; weight = new int[nodes]; for (int i = 0; i < nodes; i++) { parent[i] = i; weight[i] = 1; } } //"find" public int root(int p) { while (p != parent[p]) { p = parent[p]; } return p; } //"union" public void connect(int p, int q) { int rootP = root(p); int rootQ = root(q); if (rootP == rootQ) return; if (weight[rootP] < weight[rootQ]) { parent[rootP] = rootQ; weight[rootQ] += weight[rootP]; } else { parent[rootQ] = rootP; weight[rootP] += weight[rootQ]; } count--; } public boolean connected(int p, int q) { return root(p) == root(q); } } public static long power(long x, long y, long p) { // Initialize result long 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 & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static long dist(int[] point, int[] point2) { return (long)(Math.pow((point2[1]-point[1]),2)+Math.pow((point2[0]-point[0]),2)); } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b,a%b); } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return arr2[2]-arr1[2]; //descending order } }); return array; } public static long[][] sort(long[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<long[]>() { @Override //Descending order public int compare(long[] arr1, long[] arr2) { if (arr2[0] < arr1[0]) return -1; if (arr2[0] > arr1[0]) return 1; return 0; } }); return array; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
42942b861328cef3f65b1bf88dcb0190
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package sept; import java.io.*; import java.util.*; public class TimePass implements Runnable { InputStream is; PrintWriter out; String INPUT = "1 2"; //boolean debug=false; boolean debug=true; static int mod=998244353; static int mod2=1000000007; void solve() throws IOException { int n=ni(); int[] a=new int[n]; int max=0; for(int i=0;i<n;i++) { a[i]=ni(); max=Math.max(max, a[i]); } for(int i=max;i<=max+2;i++) { int[] b=new int[2]; for(int j=0;j<n;j++) { if((i-a[j])%2==1)b[j%2]++; } if(b[0]==b[1]) { out.println("YES"); return; } } out.println("NO"); } static int[][] packD(int n,int[] from,int[] to) { int[][] g=new int[n][]; int[] p=new int[n]; for(int f:from) { p[f]++; } int m=from.length; for(int i=0;i<n;i++) { g[i]=new int[p[i]]; } for(int i=0;i<m;i++) { g[from[i]][--p[from[i]]]=to[i]; } return g; } static class Pair { int a,b; public Pair(int a,int b) { this.a=a; this.b=b; //this.ind=ind; } } static long lcm(int a,int b) { long val=a; val*=b; return (val/gcd(a,b)); } static long gcd(long a,long b) { if(a==0)return b; return gcd(b%a,a); } static int pow(int a, int b, int p) { long ans = 1, base = a; while (b!=0) { if ((b & 1)!=0) { ans *= base; ans%= p; } base *= base; base%= p; b >>= 1; } return (int)ans; } static int inv(int x, int p) { return pow(x, p - 2, p); } public void run() { if(debug)oj=true; is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); try { solve(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception {new Thread(null,new TimePass(),"Main",1<<26).start();} private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
f302486219cb39a9dececaa0943ec180
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package sept; import java.io.*; import java.util.*; public class TimePass implements Runnable { InputStream is; PrintWriter out; String INPUT = "1 2"; //boolean debug=false; boolean debug=true; static int mod=998244353; static int mod2=1000000007; void solve() throws IOException { int n=ni(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=ni()%2; } Stack<Integer> st=new Stack<>(); for(int i=0;i<n;i++) { if(!st.isEmpty() && st.peek()==a[i])st.pop(); else st.push(a[i]); } if(st.size()<=1) out.println("YES"); else out.println("NO"); } static int[][] packD(int n,int[] from,int[] to) { int[][] g=new int[n][]; int[] p=new int[n]; for(int f:from) { p[f]++; } int m=from.length; for(int i=0;i<n;i++) { g[i]=new int[p[i]]; } for(int i=0;i<m;i++) { g[from[i]][--p[from[i]]]=to[i]; } return g; } static class Pair { int a,b; public Pair(int a,int b) { this.a=a; this.b=b; //this.ind=ind; } } static long lcm(int a,int b) { long val=a; val*=b; return (val/gcd(a,b)); } static long gcd(long a,long b) { if(a==0)return b; return gcd(b%a,a); } static int pow(int a, int b, int p) { long ans = 1, base = a; while (b!=0) { if ((b & 1)!=0) { ans *= base; ans%= p; } base *= base; base%= p; b >>= 1; } return (int)ans; } static int inv(int x, int p) { return pow(x, p - 2, p); } public void run() { if(debug)oj=true; is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); try { solve(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception {new Thread(null,new TimePass(),"Main",1<<26).start();} private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
1b6719b617ba50d474e058daabe1e360
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** What do you think? What do you think? 1st on Billboard, what do you think of it Next is a Grammy, what do you think of it However you think, I’m sorry, but shit, I have no fcking interest ******************************* I'm standing on top of my Monopoly board That means I'm on top of my game and it don't stop til my hip don't hop anymore https://www.a2oj.com/Ladder16.html ******************************* 300IQ as writer = Sad! */ import java.util.*; import java.io.*; import java.math.*; public class x1092D1 { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken())%2; ArrayList<Integer> ls = new ArrayList<Integer>(); int curr = arr[0]; int size = 1; for(int i=1; i < N; i++) { if(curr == arr[i]) size++; else { ls.add(size); size = 1; curr = arr[i]; } } ls.add(size); if(ls.size() >= 2) { ArrayDeque<Integer> stack = new ArrayDeque<Integer>(); int tag = 1; for(int x: ls) { tag ^= 1; if(x%2 == 0) continue; if(stack.size() == 0) stack.push(tag); else if(stack.peek() == tag) stack.pop(); else stack.push(tag); } if(stack.size() >= 2) System.out.println("NO"); else System.out.println("YES"); } else System.out.println("YES"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
7cc12e7e2658593ef9842c9dd225a095
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** What do you think? What do you think? 1st on Billboard, what do you think of it Next is a Grammy, what do you think of it However you think, I’m sorry, but shit, I have no fcking interest ******************************* I'm standing on top of my Monopoly board That means I'm on top of my game and it don't stop til my hip don't hop anymore https://www.a2oj.com/Ladder16.html ******************************* 300IQ as writer = Sad! */ import java.util.*; import java.io.*; import java.math.*; public class x1092D1 { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken())%2; ArrayList<Integer> ls = new ArrayList<Integer>(); int curr = arr[0]; int size = 1; for(int i=1; i < N; i++) { if(curr == arr[i]) size++; else { ls.add(size); size = 1; curr = arr[i]; } } ls.add(size); if(ls.size() >= 2) { Stack<Integer> stack = new Stack<Integer>(); int tag = 1; for(int x: ls) { tag ^= 1; if(x%2 == 0) continue; if(stack.size() == 0) stack.push(tag); else if(stack.peek() == tag) stack.pop(); else stack.push(tag); } if(stack.size() >= 2) System.out.println("NO"); else System.out.println("YES"); } else System.out.println("YES"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
abc1cb87b67ba0d0cfd672e90e60edaa
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Deque; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rustam Musin (t.me/musin_acm) */ 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); D1VelikayaVovinaStenaVersiya1 solver = new D1VelikayaVovinaStenaVersiya1(); solver.solve(1, in, out); out.close(); } static class D1VelikayaVovinaStenaVersiya1 { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); if (n == 1) { out.print("YES"); return; } Block[] a = new Block[n]; for (int i = 0; i < n; i++) { a[i] = new Block(1, in.readInt()); } Deque<Block> q = new ArrayDeque<>(); for (Block b : a) { q.addLast(b); while (q.size() >= 2) { Block b2 = q.pollLast(); Block b1 = q.pollLast(); Block both = b1.merge(b2); if (both != null) { q.addLast(both); } else { q.addLast(b1); q.addLast(b2); break; } } } out.print(q.size() == 1 ? "YES" : "NO"); } class Block { int len; int height; Block(int len, int height) { this.len = len % 2; this.height = height % 2; } Block merge(Block other) { if (len == 0 && other.len == 0) { return new Block(0, 0); } if (len == 0) { return new Block(1, other.height); } if (other.len == 0) { return new Block(1, height); } if (height == other.height) { return new Block(len + other.len, height); } return null; } public String toString() { return "Block{" + "len=" + len + ", height=" + height + '}'; } } } 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 readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void close() { writer.close(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
e5886f2c4809ff1bcc706fdbd1fb198e
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class fast implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new fast(),"fast",1<<26).start(); } public void sortbyColumn(int arr[][], int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } public void sortbyColumn(long arr[][], int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<long[]>() { @Override // Compare values according to columns public int compare(final long[] entry1, final long[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } long power(long x, long y, long p){ long res = 1; x = x % p; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long ncr(int n, int r, long p){ long C[]=new long[r+1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j-1])%p; } return C[r]; } public void run(){ InputReader s = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=s.nextInt(); Stack<Long> st=new Stack<Long>(); while(n-->0){ long x=s.nextLong()&1; if(st.isEmpty() || st.peek()!=x){ st.push(x); } else st.pop(); } if(st.size()<2){ w.println("YES"); } else w.println("NO"); w.close(); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
cd49feb0748b4e61ebd8358502077ebb
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.Scanner; import java.util.Stack; public class VovaWall1Edit { private static String checkWall (int[] arr, int size) { Stack<Integer> st = new Stack<>(); //We convert all numbers to parities. odd for 1 and even elements to 0. Then, If the parities are same, the wall can be built and we remove the head, else, we insert. for (int elem : arr) { if (!st.empty() && elem ==st.peek()) { st.pop(); } else { st.push(elem); } } if (st.size() <= 1) { return "YES"; } else { return "NO"; } } public static void main (String[] args) { try (Scanner sc = new Scanner(System.in)) { int size = sc.nextInt(); int[] arr = new int[size]; //& with 1 to obtain all odd numbers for (int i = 0; i < size; i++) { arr[i] = sc.nextInt(); arr[i] &= 1; } System.out.println(checkWall(arr, size)); /* for (int elem : arr) { System.out.println(elem); } */ } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
88e97383c9444808567a25a4a8f8e5ae
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.*; import java.math.*; import java.io.*; import java.text.*; public class hacker { /*Fatt Gyi Bhai*/ //Dekh le mera code public static boolean[] sieve(long n) { boolean[] prime = new boolean[(int)n+1]; Arrays.fill(prime,true); prime[0] = false; prime[1] = false; long m = (long)Math.sqrt(n); for(int i=2;i<=m;i++) { if(prime[i]) { for(int k=i*i;k<=n;k+=i) { prime[k] = false; } } } return prime; } static long GCD(long a,long b) { if(a==0 || b==0) { return 0; } if(a==b) { return a; } if(a>b) { return GCD(a-b,b); } return GCD(a,b-a); } static long CountCoPrimes(long n) { long res = n; for(int i=2;i*i<=n;i++) { if(n%i==0) { while(n%i==0) { n/=i; } res-=res/i; } } if(n>1) { res-=res/n; } return res; } //fastest way to find x**n static long modularExponentiation(long x,long n,long m) { long res = 1; while(n>0) { if(n%2==1) { res = (res*x)%m; } x =(x*x)%m; n/=2; } return res; } static long lcm(long a,long b) { return (a*b)/GCD(a,b); } static long pow(long a,long b) { long res = 1; while(b>0) { if((b&1)==1) { res *= a; } b >>= 1; a *=a; } return res; } static long modInverse(long A,long M) { return modularExponentiation(A,M-2,M); } static boolean prime(int n) { for(int i=2;i*i<=n;i++) { if(i%2==0 ||i%3==0) { return false; } } return true; } public static void main(String[] args) throws IOException { // = new PrintWriter("explicit.out"); new hacker().run(); } static int rev(int n) { int x=0; int num = 0; while(n>0) { x = n%10; num = num*10+x; n/=10; } return num; } // Firse author ka solution static Scanner in = new Scanner(System.in); static void run() throws IOException { int t = ni(); long[] a = new long[t]; for(int i=0;i<t;++i) { a[i] = nl()%2; } Stack<Long> s1 = new Stack<>(); for(int i=0;i<t;i++) { if(!s1.isEmpty() && s1.peek()==a[i]) { s1.pop(); } else { s1.push(a[i]); } } if(s1.size()<=1) { printS("YES"); } else { printS("NO"); } } static void printL(long a) { System.out.println(a); } static void printS(String s) { System.out.println(s); } static void swap(char c,char p) { char t = c; c = p; p = t; } static long max(long n,long m) { return Math.max(n,m); } static long min(long n,long m) { return Math.min(n,m); } static double nd() throws IOException { return Double.parseDouble(in.next()); } static int ni() throws IOException { return Integer.parseInt(in.next()); } static long nl() throws IOException { return Long.parseLong(in.next()); } static String si() throws IOException { return in.next(); } static int abs(int n) { return Math.abs(n); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } } class Pair implements Comparable<Pair> { int value; int index; public Pair(int v,int i) { value = v; index = i; } public int compareTo(Pair a) { return this.value-a.value; } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
52a652bae53face529cee2c1191de3f8
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
// No sorceries shall previal. // import java.io.*; import java.util.*; public class _InVoker_ { //Variables static long mod = 1000000007; static long mod2 = 998244353; static FastReader inp= new FastReader(); static PrintWriter out= new PrintWriter(System.out); public static void main(String args[]) { _InVoker_ g=new _InVoker_(); g.main(); out.close(); } //Main void main() { int n=inp.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=inp.nextInt()%2; } Stack<Integer> stack=new Stack<>(); for(int i=0;i<n;i++) { if(!stack.isEmpty() && stack.peek()==a[i]) { stack.pop(); }else { stack.push(a[i]); } } out.println(stack.size()<=1?"YES":"NO"); } /********************************************************************************************************************************************************************************************************* * ti;. .:,:i: :,;;itt;. fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE :,:. f::::. .,ijLGDDDDDDDEEEEEEE* *ti;. .:,:i: .:,;itt;: GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG. .::. f:,...,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: :,;;iti, :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG .::. .f,::,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: .,,;iti;. LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj .::. .:L;;ijfGDDDDDDDDDEEEEE * *ti;. .:,:i: .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWKWWWWWWWWWWWWWWKKKKKKKEL .::. .:;LijLGGDDDDDDDDEEEEE * *ti;. .:,:;: :,;;ittfDEEEEEEEEEEEEEEEEKEEEGEEEEEEEEEEEKWWWEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWKKKKKKKELj .::. :,;jffGGDDDDDDDDDEEEE * *ti;. .:,:i: .,;;tGGDEEEEEEEEEEEKEEEKEEEDEEEEEEEEEEEEWWWEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWKWWWWWWKKKKKKKEEL .::. .:;itDGGDDDDDDDDDEEEE * *ti;. .:::;: :;ifDEEEEEEEEEEEEKEEEKEEEEEEEEEEEEEEEWWWEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WWWKKKKKKKKEEf .::. :,itfGEDDDDDDDDDDDEE * *ti;. .:::;: :GGEEEEEEEEEEEKEKEEKEEEEEEEEEEEEEEEEWWEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKKEEDG .::. .,;jfLGKDLDDDDDDEEDD * *ti;. .:::;: fDEEEEEEKKKKKKKKKEKEEEEEEEEEEEEEEE#WEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#KKKKKKKKKKEEf .:::. .,;tfLGDEDDDDDDDDEEE * *ti;. :::;: fDEEEEEEKKKKKKKKKKWKEEEEEEEEEEEEEEEWKEEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKKKEEft :::. .,;tfLGDDDKDDDDDDDDD * *ti;. .::;: fDEEEEEEKKKKKKKWKKKKKEEEEEEEEEEEEE#WEEWEEEEEDEEDEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKEEGG :,:. .,;tfLGGDDDKDDDDDDDD * *ti;. .:.;: tGDEEEEKKKKKKKKKKKKKKKKKEEEEEEEEEEEWEEKWEEEEEEEDEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKKKKKEEDf :::. .,;tfLGGDDDDEDDDDDDD * *ti;. .::;: fDEEEEEKKKKKKKKKKKWKKKKKKKKEEEEEEEWWEEWEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKEEEft.::. .,;tfLGGDDDDDDEDDDDD * *ti;. .:.;: tGDEEEKKKKKKKKKKKKKKKKKKKKKKEKEEEEE#EEWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKEEEGD:::. .,;tfLGGDDDDDDDEDDDD * *ti;. .:.,. LDEEEEKKKKKKKKKKWKWKKKKKKKKKKKKEEEKWEKW#EEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKW##KKKKKKEEEEf,,:. .,;tfLGGDDDDDDDDEDDD * *ti;. ..:.,. LGDEEEEKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEEW#WEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKK##KKKKKEEEEEfi;,. .,;tfLGGDDDDDDDDDKDD * *tt;. .:.,: jDEEEEKKKKKKKKKKWWKKKKKKKKKKKKKKKKKWKE#WWEEEEEEEEEEEEEEWEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKWWKKKKEEEEDfG;,: .,;tfLGGDDDDDDDDDDKD * *tii,. .:.,. tGDEEEEKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKKWWWKEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKW#KKKKEEEEDGGi;,. .,;tfLGGDDDDDDDDDDDE * *ti;;,:. .:.,: fDEEEEKKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEK#WWKEEEEEEEEEEEEDEEEEEEEEEEEEEEGEEEEEEEEEEEEEEEEEEEKKKKKKKWWKKEEEEEEDDf;;;,. .,;tfLGGDDDDDDDDDDDD * *tii;,,:.. ...,. ;LEEEEEKKKKKKWKKKKWKKKKKKKKKKKKKKKKKEKKW#WEEEEEEEEEEEEEjEEEEEEEEEKEEEEGEEEEEEEEEKEEEEEEEEEEEEEEEEE#WKEEEEEEDDf;;;;,: .,itfLGGDDDDDDDDDDDD * *ti;,,,,,:. ...,. LDEEEEKKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKK#W#WEEEEEEEEEEEDDLEEEEEEEEEWEEEEDEEEEEEEEEKEEEEEEEEEEEEEEEEEWWEEEEEEEDDfj,,,,,:. .,itfGGGDDDDDDDDDDDD * *tii,,,,::::. ...,: .fDEEEEKKKKKKWKKKKWWWKKKKKKKKKKKKKKKEKKW#WWEEEEEEEEEEEKiKEEKEEEEEEWEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEEWWEEEEEEEDDLD:::,,,:. .,ijfGGGDDDDDDDDDDDD * *ti;:::::::::.. .:.,: LDEEEEKKKKKKKWKKKKWWKKKKKKKKKKKKKKKKtKKWWWWKEEEEEEEEEDiiDEEEEEEEEWWEEEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEWKEEEEEDDDGL:. .:,,,: .,ijLGGGDDDDDDDDDDDD * *tt;. .::::::::.. ...,: :fDEEEKKKKKKKKKKKKWW#KKKKKKKKKKKKKKKKfKKWWWWKEEEEEEEEDti,DEKEEEEEEWWEEEDEEEEEEEEEKEEEEEEEEEEEEEDEEEEE#WEEEEEGGDGf:. .:,;,:. .,ijLGGDDDDDDDDDDDDD * *tt;. .:::::::.. ...,: GDEEEKKKKKKKKWKKKKWWWKKKWKKKKKKKWWWKDEKLWWWWKKEEEEEEDEi,LDEEEEEEEEWWEEEEEEEEEEEEEEEEEEEEEEEEEDEDEEEEEW#EEEEDDDDGf,. :,,,:...,ijLGGGDDDDDDDDDDDD * *tt;. .....::::.. ...,: fDEEEKKKKKKKKWKKKKWWWWKKKWKKKKKKKKKKfWKiWWW#KKEEEEEEEi;.EDfEEEDEEiWWEEEEEEEEEEEEDGKEEEEEEEEEEDEEEEEEEWWEEEEDDDGGLi. .,;,:::,ijLGGGDDDDDDDDDDDD * *tt;. ....:::::. ...,. iDEEEEKKKKKKKKWKKWKWWWWWKKWWWKKKKKKKKtWKt#WWWKKEEEEEDji..DDKDDEDEGiWKEEEEEEEEEEDDEjEEEEEEEEEEEDEEEEEEEKWKEEDDDDGGff. .:,;,,;ijLGGGDDDDDDDDDDDD * *tt;. ....::::.. .:.,: .LDEEEKKKKKKKKKKKKWWWWKWWWWWWWWWWKKKKWtKKiDWWWKKKEEEEKi:..DEDDDDDDiiWKEEEEEEEEEEDDEijDEEEEEKEEEEEEEEEEEEWWEEGDDDGGLG. .:,;;iijLGGGDDDDDDDDDDDD * *tt;. .....:::.. ...,. .fEEEEKKKKKKKKWKKKKWWWWWWWWWWWWWWKWKKKiKDiLWWWWKEEEEEi,..fD:DDDDDti;WEEEEEEEEEEKDDi:iDDEEEEWEEEEEEEEEEEE#WEEGDDDDGGG. :,iitjLGGGDDDDDDDDDDDD * *tti. .....:::.. ...,. GDEEEKKKKKKKKKWKKKWWW#WWWWWWWWWWWKWKKjiEjitWWWKKWEEEDi...DDLDDDDji;;WEEEEEEEEEEEDEj.iDDEEEEWEEEEEEEEEEEEWWEEDDDDDDGf. .,;tjfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. fEEEKKKKKKKKKKKKKKKW#WWWWWWWWWWWWWWWWtiEiiiWWWKKEWKEi....D.EDDDEi;.fWEEEEEEEEEEDDfL.;EDDEEEWEEEEEEEEEEEEWWEEEDDDDDGf. :;ijfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. LDEEEKKKKKKKKKKKKKKWWWWWWWWWWWWWWWW####WKiiiWWWKKKEEK,...:E:DDDEii..GWEEEEEEEEDWDDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf: .,itfLGGDDDDDDDDDDDD * *tti. .....:::.. ...,. fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi .,;tfLGGGDDDDDDDDDDD * *tti. .....:::.. ...,. iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::......:. LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG .,;tjLLGGDDDDDDDDDDD * *tti. ...::::::..,. :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::::,. GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf .,;tjLLGGDDDDDDDDDDD * *tti. ....:::,,. .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf .:;tjLLGGGDDDDDDDDDD * *tti. ...::,,,:. GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf .:;tjLLGGDDDDDDDDDDD * *jti. ...::,,,,:. .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf: .,;tfLLGGDDDDDDDDDDD * *tti. ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi .,;tfLLGGGDDDDDDDDDD * *tti. ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL .,;tfLGGGDDDDDDDDDDD * *jti. ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG .,;tfLGGGGDDDDDDDDDD * *jti. ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL .,;tfLGGGGDDDDDDDDDD * *jti. ...:. .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf .,;tfGGGGDDDDDDDDDDD * *jti. ...:. ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:. .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,: .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK, ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG .:,,,:::,;tfGGGDDDDDDDDDDDD * *jti. ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL .:,;,,,;tfGGGDDDDDDDDDDDD * *jti. ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf .:,;;;itfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf .,;iitfGGGDDDDDDDDDDDD * *jti. ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL .:;itfGGGDDDDDDDDDDDD * *jji. ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j. KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf .,;tfGGGDDDDDDDDDDDD * *jji. ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;.................................. . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL, .:;tfGGGDDDDDDDDDDDD * *jji. .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................:: KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj .:;tfGGDDDDDDDDDDDDD * *jji. .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG .:;tfGGGDDDDDDDDDDDD * *jji. . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG .;tjfLLGDDDDDDDDDDDDDDD * *jji. i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL ,fGGGDDDDDDDDEEEDDDDDDDDDD * *jji. .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD * *jji. i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD * *jji. .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD * *jji. .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL; .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD * *jji. :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED * *jji. i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE * *jji. i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD * *jji. .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG * *jjt. :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG * *jji. ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG * *jjt. f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG * *jjt. ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG * *fji. ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL * *fji. t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL * *fjt: .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL * *fjji;:. .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG * *fjjtii;,:. :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG * *fjtiiiii;;:. j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG * *fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG * *fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD * *jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD * *fjt. .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD * *fjt. .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD * *fjt. .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD * *fjt: :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD * *fjt: t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD * *jjt. f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD * *fjt:. ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG * *fjt:. ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; * *fjt:. f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWWGi;i;,..;jDDDKEGi;;;;;;:................EED,,,,,,D,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .. ;::,,,,,;;EDDEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKW#WW####KWKKKiiiiiiiiiiiiijKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWt;i;;;;i;DDDDDDGi;;;;;;;;:.............EDf;,,,;,G;;;;;;;;;;;;;;;,,,,,,,,,, * *fjt:......:,,,,,,;LDDDEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWWKWWWW####KKKKKiiiiiiiiiiijKEKKWKKKKKKKWWWWWWWWWWWWWWWWWWWWWWiLiii;i;DEEEEDDE;i;;;;;;;;;:..........EDi,;;;;;L;;;;;;;;;;;;;;;;;;,,,,,,, * *fjt:......:,,,,,;EDDDEEKEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKWWWWKKWWW##W#KWKKWEiiiiiijGKKKKKWWKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKi;iiiiDDEEEEEEDEi;;;;;;;;;;;;;,:.....ED;;;;;;;j;;;;;;;;;;;;;;;;;;;;;;;,, * *fjt:.....t,,,,,;DDDDEEEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKWWWW##WKWKKWKiiiKKKKKKKKKWWKKKKKKKKWWWWWWWWWWWWWWW#WWWWWWWWWiiiiifLEEEEEEEEDi;i;;;;;;;;;;;;.....DD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....G,,,,,GDDDEEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKKWWWKKKWWW###WKWKKWKitKKKKKKKKKWKKKKKKKKKKWWWWWWWWWWWWWW###WWWWWWWWEiiiiiiiEEEEEEEEDGiiii;;;;;;;;;.....GD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....L,,,,;GDDDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; * ***********************************************************************************************************************************************************************************************************/ // Classes static class Pair implements Comparable<Pair> { long l, r; int initIndex; Pair () {} Pair (long l_, long r_, int i) { this.l = l_; this.r = r_; this.initIndex=i; } @Override public int compareTo(Pair other) { if(this.l==other.l) { return Long.compare(other.initIndex,this.initIndex); } return Long.compare(this.l, other.l); } } static class Segment implements Comparable<Segment> { int l, r, initialIndex; Segment () {} Segment (int l_, int r_, int d_) { this.l = l_; this.r = r_; this.initialIndex = d_; } @Override public int compareTo(Segment o) { return l - o.l; } } 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 s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } // Functions static long gcd(long a, long b) { return b==0?a:gcd(b,a%b); } static int gcd(int a, int b) { return b==0?a:gcd(b,a%b); } //merge-sort public static void sort(long arr[], int start, int end) { if(start>=end) return; int mid=(start+end)/2; sort(arr,start,mid); sort(arr,mid+1,end); merge(arr,start,mid,end); } private static void merge(long arr[], int start, int mid, int end) { int i, j=mid+1,c=0; long temp[]= new long[end-start+1]; for(i=start;i<=mid && j<=end;c++) { if(arr[i]<=arr[j]) { temp[c]=arr[i]; i++; } else { temp[c]=arr[j]; j++; } } while(i<=mid) { temp[c]=arr[i]; i++;c++; } while(j<=end) { temp[c]=arr[j]; j++;c++; } c=0; for(i=start;i<=end;i++,c++) arr[i]=temp[c]; } void reverse(long[] A,int l,int r) { int i=l,j=r-1; while(i<j) { long t=A[i]; A[i]=A[j]; A[j]=t; i++;j--; } } void reverse(int[] A,int l,int r) { int i=l,j=r-1; while(i<j) { int t=A[i]; A[i]=A[j]; A[j]=t; i++;j--; } } // Input arrays static void input(long a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextLong(); } } static void input(int a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextInt(); } } static void input(String s[],int n) { for(int i=0;i<n;i++) { s[i]=inp.next(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
56e90e701c9e46506b8cd5f202ee6be5
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { MyScanner in = new MyScanner(System.in); OutputWriter out = new OutputWriter(System.out); int n = in.nextInt(); int[]a = new int[n]; for (int i =0;i<n;i++) { a[i]=in.nextInt()%2; } Stack<Integer>s = new Stack<>(); for (int i =0;i<n;i++) { if (!s.isEmpty()&&s.peek()==a[i]) { s.pop(); } else { s.push(a[i]); } } if (s.size()>1) out.printLine("NO"); else { out.printLine("YES"); } out.flush(); } } class MyScanner { StringTokenizer st; BufferedReader br; public MyScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public MyScanner (FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } public boolean nextEmpty() throws IOException { String s = nextLine(); st = new StringTokenizer(s); return s.isEmpty(); } } class pair implements Comparable { int key; int value; public pair(Object key, Object value) { this.key = (int)key; this.value=(int)value; } @Override public int compareTo(Object o) { pair temp =(pair)o; return key-temp.key; } } class Graph { int n; ArrayList<Integer>[] adjList; public Graph(int n) { this.n = n; adjList = new ArrayList[n]; for (int i = 0; i < n; i++) adjList[i] = new ArrayList<>(); } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
4466ef72266cda9a9aaaba71c116a996
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class ProblemD1 { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { MyScanner scanner = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = scanner.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(scanner.nextInt()); } int x = 0; int y = 0; LinkedList<Integer> linkedList = new LinkedList<>(); for (int i = 0; i < n; i++) { if (list.get(i) % 2 == 0) { if (y != 0) { if (y % 2 == 1) { linkedList.add(y); } else { if (!linkedList.isEmpty()) { x = linkedList.removeLast(); } } } x++; y = 0; } else { if (x != 0) { if (x % 2 == 1) { linkedList.add(x); } else { if (!linkedList.isEmpty()) { y = linkedList.removeLast(); } } } y++; x = 0; } } if (x != 0) { if (x % 2 == 1) { linkedList.add(x); } } else if (y != 0) { if (y % 2 == 1) { linkedList.add(y); } } if (linkedList.size() <= 1) { out.println("YES"); } else { out.println("NO"); } out.flush(); } private static class MyScanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; private MyScanner(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } private String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } private String nextLine() { String str = ""; try { str = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static class Pair<F, S> { private F first; private S second; public Pair() {} public Pair(F first, S second) { this.first = first; this.second = second; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
dbebf4eeb0c930d774c6873fa5d024d8
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { //consider in binary //we can add 2's forever to any part of the wall, so just consider entire thing mod 2. //only operation is adding 1 to two adjacent parts Scanner reader = new Scanner(System.in); int numParts = reader.nextInt(); int[] modifiedInputs = new int[numParts]; int a = 0; int[] mod2Stuff = new int[numParts]; for(int i = 0; i < numParts; i++) { mod2Stuff[i] = reader.nextInt() & 1; } for(int i = 0; i < numParts; i++) { modifiedInputs[a] = mod2Stuff[i]; a++; while(a>1) { if(modifiedInputs[a-2] == modifiedInputs[a-1]) //level a -= 2; //mod2 else break; } } if(a > 1) System.out.println("NO"); else System.out.println("YES"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
75b78a9cba6e09639ae3c56f9dad487a
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
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 b==0?a:gcd(b,a%b); } void work() { int n=in.nextInt(); int[] A=new int[n]; for(int i=0;i<n;i++)A[i]=in.nextInt(); Stack<Integer> stack=new Stack<>(); for(int i=0;i<n;i++) { if(stack.size()==0||stack.peek()!=A[i]%2) { stack.push(A[i]%2); }else { stack.pop(); } } if(stack.size()>1) { out.println("NO"); }else { out.println("YES"); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { if(st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
1087e70ae3ee4f40c738e9a546b46669
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.math.*; import java.security.KeyStore.Entry; import java.util.*; /* yash jobanputra DA-IICT */ public class yash { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long mod = 1000000000 + 7; private static long mod1 = 1000000000 + 9; private static int MAX=1000001; private static int block; private static int[] cost; private static int times; //private static int times2; private static boolean[] vis; private static int[] mark; private static int count; private static boolean ff; private static void soln(){ int n=ni(); int[] arr=nia(n); Stack<Integer> st=new Stack<>(); for(int i=0;i<n;i++) { if(st.isEmpty()) st.push(arr[i]); else if((st.peek()%2)==(arr[i]%2)) st.pop(); else st.push(arr[i]); } if(st.size()==1 || st.size()==0) pw.println("YES"); else pw.println("NO"); } private static class graph{ public static void dfs(ArrayList<Integer>[] gr,int v) { vis[v]=true; ArrayList<Integer> arr=gr[v]; for(int i=0;i<arr.size();i++) { if(!vis[arr.get(i)]) { count++; dfs(gr,arr.get(i)); } } } public static void arti(boolean[] ap,int[] parent,int[] disc,int[] low,ArrayList<Integer>[] gr,boolean[] vis,int ver) { int child=0; vis[ver]=true; ArrayList<Integer> arr=gr[ver]; times++; disc[ver]=low[ver]=times; for(int i=0;i<arr.size();i++) { if(!vis[arr.get(i)]) { child++; parent[arr.get(i)]=ver; arti(ap,parent,disc,low,gr,vis,arr.get(i)); low[ver]=Math.min(low[ver], low[arr.get(i)]); if(parent[ver]!=0 && low[arr.get(i)]>=disc[ver]) ap[ver]=true; if(parent[ver]==0 && child>1) ap[ver]=true; } else if(parent[ver]!=arr.get(i)) low[ver]=Math.min(low[ver], disc[arr.get(i)]); } } public static void floyd(Pair[] arr,int n,int m,int[][] dist) { for(int i=0;i<=n;i++) { Arrays.fill(dist[i], Integer.MAX_VALUE); } for(int c=0;c<m;c++) { int w=arr[c].v; int a=arr[c].j.v; int b=arr[c].j.i; dist[a][b]=w; } for(int i=0;i<=n;i++) dist[i][i]=0; for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(dist[i][k]!=Integer.MAX_VALUE && dist[k][j]!=Integer.MAX_VALUE) dist[i][j]=Math.min(dist[i][j], dist[i][k]+dist[k][j]); } } } } public static boolean bfs(ArrayList<Integer>[] gr,int v,int n) { boolean[] vis=new boolean[n+1]; int c2=0; Queue<Integer> q=new LinkedList<>(); q.add(v); while(!q.isEmpty()) { int x=q.poll(); Iterator<Integer> it=gr[x].iterator(); while(it.hasNext()) { int p=it.next(); if(!vis[p]) { q.add(p); c2++; vis[p]=true; } } } return c2==n; } public static ArrayList<Pair> MSTP(int n,int m,ArrayList<Pair>[] arr,int[][] arr2) { ArrayList<Pair> pp=new ArrayList<>(); int[] par=new int[n+1]; int[] key=new int[n+1]; vis=new boolean[n+1]; Arrays.fill(key, Integer.MAX_VALUE); key[1]=0; PriorityQueue<Integer> dis=new PriorityQueue<>(n+1, new Comparator<Integer>() { public int compare(Integer p,Integer q) { return key[p]-key[q]; } } ); dis.add(1); vis[1]=true; while(!dis.isEmpty()) { //pw.println(dis); int u=dis.poll(); ArrayList<Pair> p=arr[u]; for(int i=0;i<p.size();i++) { if(key[u]+p.get(i).i<key[p.get(i).v]) { key[p.get(i).v]=key[u]+p.get(i).i; par[p.get(i).v]=u; if(!vis[p.get(i).v]) { dis.add(p.get(i).v); vis[p.get(i).v]=true; } } } } for(int i=1;i<=n;i++) pp.add(new Pair(i,par[i])); return pp; } public static ArrayList<Pair> MSTK(int n,int m,Pair[] arr) { ArrayList<Pair> pp=new ArrayList<>(); long sum=0; Arrays.sort(arr); DSU x=new DSU(n+1); for(int c=0;(c<m);c++) { Pair p=arr[c]; //int a=p.v; //int b=p.i; int a=p.j.v; int b=p.j.i; int w=p.v; if(x.find(a)!=x.find(b)) { pp.add(new Pair(w,new Pair(a,b))); x.Union(a, b); } } return pp; } public static int[] dijkstras(ArrayList<Pair>[] gr) { int n=gr.length-1; int[] dist=new int[n+1]; Arrays.fill(dist, Integer.MAX_VALUE); PriorityQueue<Integer> dis=new PriorityQueue<>(n+1, new Comparator<Integer>() { public int compare(Integer p,Integer q) { return dist[p]-dist[q]; } } ); boolean[] vis=new boolean[n+1]; Arrays.fill(vis, false); int s=1; dist[s]=0; dis.add(s); vis[s]=true; while(!dis.isEmpty()) { int p=dis.poll(); for(int i=0;i<gr[p].size();i++) { int y=gr[p].get(i).v; int w=gr[p].get(i).i; if(!vis[y]) { dist[y]=dist[p]+w; dis.add(y); vis[y]=true; } else { if(dist[p]+w<dist[y]) { dist[y]=dist[p]+w; } } } } return dist; } } public static class sieve{ public static ArrayList<Integer> sieves(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean[] bol=new boolean[n+1]; Arrays.fill(bol, true); //arr.add(1); for(int i=2;i<=n;i++) { if(bol[i]) { arr.add(i); for(int j=2;j*i<=n;j++) { bol[i*j]=false; } } } return arr; } } public static class isprime{ public static boolean check(int n) { if(n==2) return true; else if(n==3) return true; else if(n%2==0) return false; else if(n%3==0) return false; else { for(int i=6;;i+=6) { if(i>Math.sqrt(n)) break; if(n%(i-1)==0) { return false; } else if(n%(i+1)==0) return false; } return true; } } } private static class DSU{ int[] parent; int[] rank; int cnt; public DSU(int n){ parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++){ parent[i]=i; rank[i]=1; } cnt=n; } int find(int i){ while(parent[i] !=i){ parent[i]=parent[parent[i]]; i=parent[i]; } return i; } int Union(int x, int y){ int xset = find(x); int yset = find(y); if(xset!=yset) cnt--; if(rank[xset]<rank[yset]){ parent[xset] = yset; rank[yset]+=rank[xset]; rank[xset]=0; return yset; }else{ parent[yset]=xset; rank[xset]+=rank[yset]; rank[yset]=0; return xset; } } } private static class Pair implements Comparable<Pair>{ int v,i;Pair j; public Pair(int a,int b){ v=a; i=b; } public Pair(int a,Pair b) { v=a; j=b; } @Override public int compareTo(Pair arg0) { { return this.v-arg0.v; } } } private static class Pairl implements Comparable<Pairl>{ long v,i;Pairl j; public Pairl(long a,long b){ v=a; i=b; } public Pairl(long a,Pairl b) { v=a; j=b; } @Override public int compareTo(Pairl arg0) { { if(this.v>arg0.v) return 1; else return -1; } } } public static long f(long number,long m) { if (number <= 1) return 1; else return (number * f(number - 1,m))%m; } public static long mmi(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process same as // Euclid's algo m = a % m; a = t; t = y; // Update y and x y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } public static class segtree{ static int[] tree=new int[10000000]; static int[] lazy=new int[10000000]; public static void initial() { Arrays.fill(lazy, 0); } public static void build(int[] arr,int node,int l,int r) { if(l==r) { tree[node]=arr[l]; } else { int m=(l+r)/2; build(arr,(2*node)+1,l,m); build(arr,(2*node)+2,m+1,r); tree[node]=tree[2*node+1]+tree[(2*node)+2]; } } public static void updater(int[] arr,int node,int l,int r,int s,int e,int val) { if(lazy[node]!=0) { tree[node]+=(r-l+1)*lazy[node]; if(l!=r) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(s<=l && e>=r) { tree[node]+=(r-l+1)*val; if(l!=r) { lazy[2*node+1]+=val; lazy[2*node+2]+=val; } } else if(!(e<l || r<s)) { int m=(l+r)/2; updater(arr,2*node+1,l,m,s,e,val); updater(arr,2*node+2,m+1,r,s,e,val); tree[node]=tree[2*node+1]+tree[2*node+2]; } } public static void update(int[] arr,int node,int l,int r,int ind,int val) { if(l==r) { arr[ind]+=val; tree[node]+=val; } else { int m=(l+r)/2; if(l<=ind && ind<=m) { update(arr,(2*node+1),l,m,ind,val); } else { update(arr,(2*node+2),m+1,r,ind,val); } tree[node]=tree[2*node+1]+tree[2*node+2]; } } public static int query(int node,int l,int r,int s,int e) { if(lazy[node]!=0) { tree[node]+=(r-l+1)*lazy[node]; if(l!=r) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(e<l || r<s) return 0; if(s<=l && e>=r) return tree[node]; int m=(l+r)/2; int p1=query((2*node+1),l,m,s,e); int p2=query((2*node+2),m+1,r,s,e); return (p1+p2); } } private static long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } private static long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } private static int gcd(int n, int l) { if (l == 0) return n; return gcd(l, n % l); } private static long max(long a, long b) { if (a > b) return a; return b; } private static long min(long a, long b) { if (a < b) return a; return b; } public static void main(String[] args) throws Exception { new Thread(null,new Runnable(){ @Override public void run() { /*try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\C-small-1-attempt1.in")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ InputReader(System.in); pw = new PrintWriter(System.out); /*try { pw=new PrintWriter(new FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ soln(); pw.close(); } },"1",1<<26).start(); } public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static 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++]; } private static int ni() { 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; } private static long nl() { 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; } private static double nd() { double ret = 0, div = 1; int 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 static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nli() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nia(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } private static long[] nla(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } private static void pa(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pa(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private static char nc() { int c = read(); while (isSpaceChar(c)) c = read(); char c1=(char)c; while(!isSpaceChar(c)) c=read(); return c1; } public String ns() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
33f8472fd7271576cda0f434f84c8cd8
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { int n = Integer.parseInt(reader.readLine()); String[] line = reader.readLine().split(" "); long[] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = Long.parseLong(line[i]); } int[] ost = new int[n - 1]; TreeSet<Integer> ones = new TreeSet<>(); ones.add(-1); ones.add(n - 1); for(int i = 0; i < n - 1; i++) { ost[i] = (int)Math.abs(a[i] % 2 - a[i + 1] % 2); if(ost[i] == 1) { ones.add(i); } //print(ost[i] + " "); } //println("\n"); boolean ans = true; for(int i = 0; i < n - 1; i++) { if(ost[i] == 1) continue; int r = ones.ceiling(i); //println(r + " " + i); if((r - i) % 2 == 1) { if(r != n - 1) ost[r] = 0; if(i > 0) ost[i - 1] = 0; int l = ones.floor(i); if(l != -1) ones.remove(l); int rr = ones.ceiling(i); if(rr != n - 1) ones.remove(rr); i = ones.floor(i); } else { i = r - 1; } if(r == n - 1 && i == -1) break; } for(int i = 0; i < n - 1; i++) { //print(ost[i] + " "); if(ost[i] == 1) { println("NO"); return; } } println("YES"); } catch (IOException e) { } } static void change() { } static int log(int num, int d) { int ans = 0; while(num > 1) { num /= d; ans++; } return ans; } static class Pair implements Comparable<Pair> { char a; int b; Pair(char a, int b) { this.a = a; this.b = b; } @Override public int compareTo(Pair o) { if(this.a > o.a) { return 1; } else if(this.a < o.a) { return -1; } return 0; } } static String reverse(String s) { String ans = ""; char[] c = s.toCharArray(); for(int i = s.length() - 1; i >= 0; i--) { ans += c[i]; } return ans; } static long gcd(long a, long b) { while(b != 0) { long c = a % b; a = b; b = c; } return a; } static long fac(int deg) { long ans = 1; for(int i = 2; i <= deg; i++) { ans *= i; } return ans; } static int r(int a, int b) { int ans = a / b; if(a % b != 0) ans++; return ans; } static void print(Object o) { System.out.print(o.toString()); } static void println(Object o) { System.out.println(o.toString()); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
5d1db1458d9f17c74d79a8d04a6712b2
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import javafx.scene.layout.Priority; import java.io.*; import java.lang.reflect.Array; import java.net.Inet4Address; import java.util.*; import java.lang.*; import java.util.HashMap; import java.util.PriorityQueue; public class templ implements Runnable { public class triplet { int f,s,t; triplet(int f,int s,int t) { this.f=f; this.s=s; this.t=t; } public boolean equals(Object o) { triplet ob=(triplet)o; int ff,ss,tt; if(o!=null) { ff=ob.f; ss=ob.s; tt=ob.t; if((ff==this.f)&&(ss==this.s)&&(tt==this.t)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s+" "+this.t).hashCode(); } } public class pair implements Comparable { int f; int s; pair(int f,int s) { this.f=f; this.s=s; } public boolean equals(Object o) { pair ob=(pair)o; int ff; int ss; if(o!=null) { ff=ob.f; ss=ob.s; if((ff==this.f)&&(ss==this.s)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s).hashCode(); } public int compareTo(Object o) { pair pr=(pair)o; if(s>pr.s) return 1; else if(s==pr.s) { if(f<pr.f) return -1; else return 1; } else return -1; } } public static void main(String args[])throws Exception { new Thread(null,new templ(),"templ",1<<27).start(); } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.ni(); int m[]=new int[n+2]; int max=0; for(int i=1;i<=n;i++) { m[i]=in.ni()%2; max=Math.max(max,m[i]); } m[0]=max; m[n+1]=max; ArrayList<Integer>al=new ArrayList<>(); for(int i=0;i<=n+1;i++) { if(m[i]==max) al.add(i); } int k=0; Stack<Integer>st=new Stack<>(); for(int i=1;i<=n;i++) { if(st.isEmpty()) st.push(m[i]); else { if(st.peek()==m[i]) st.pop(); else st.push(m[i]); } } if(st.size()>1) out.println("NO"); else out.println("YES"); out.close(); } catch(Exception e){ return; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
a97ea789a053eaa3ea26381aba870a9e
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Objects; public class Main { static StdIn in = new StdIn(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ int n = in.nextInt(); int a[] = new int[n + 1]; int top = 0; for(int i = 0; i < n; ++i) { int x = in.nextInt() & 1; if(top > 0 && a[top - 1] == x) --top; else a[top++] = x; } out.println(top > 1 ? "NO" : "YES"); out.close(); } static long mul_mod(long a, long b, long m) { if (a >= m) a %= m; if (b >= m) b %= m; double x = a; long c = (long) (x * b / m); long r = (long) (a * b - c * m) % m; return r < 0 ? r + m : r; } static long pow_mod(long a, long b, long m) { long res = 1; a %= m; while (b > 0) { if (b % 2 != 0) res = mul_mod(res, a, m); b >>= 1; a = mul_mod(a, a, m); } return res; } static class Pair<T extends Comparable<T>, K extends Comparable<K>> implements Comparable<Pair<T, K>>{ public T x; public K y; public Pair() { } public Pair(T first, K second) { this.x = first; this.y = second; } @Override public int compareTo(Pair<T, K> o) { int cmp = x.compareTo(o.x); return cmp != 0 ? cmp : y.compareTo(y); } @Override public boolean equals(Object obj) { if (!(obj instanceof Pair)) { return false; } Pair<?, ?> p = (Pair<?, ?>) obj; return Objects.equals(p.x, x) && Objects.equals(p.y, y); } @Override public int hashCode() { return x.hashCode() * 31 + y.hashCode() * 10000007; } @Override public String toString() { return "( " + x.toString() + " , " + y.toString() + " )"; } } static class PairInt extends Pair<Integer, Integer> { public PairInt() { super(0, 0); } public PairInt(int a, int b) { super(a, b); } } static class PairLong extends Pair<Long, Long> { public PairLong() { super(0L, 0L); } public PairLong(long a, long b) { super(a, b); } } static class StdIn { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public StdIn() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public StdIn(InputStream in) { try { din = new DataInputStream(in); } catch (Exception e) { throw new RuntimeException(); } buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public StdIn(String fileName) { InputStream in; try { in = new FileInputStream(new File(fileName)); din = new DataInputStream(in); } catch (Exception e) { throw new RuntimeException(); } buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String next() { int c; while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ; StringBuilder s = new StringBuilder(); while (c != -1) { if (c == ' ' || c == '\n' || c == '\r') break; s.append((char) c); c = read(); } return s.toString(); } public String nextLine() { int c; while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ; StringBuilder s = new StringBuilder(); while (c != -1) { if (c == '\n' || c == '\r') break; s.append((char) c); c = read(); } return s.toString(); } public int nextInt() { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public int[] readIntArray(int n) { int[] ar = new int[n]; for (int i = 0; i < n; ++i) ar[i] = nextInt(); return ar; } public long nextLong() { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long[] readLongArray(int n) { long[] ar = new long[n]; for (int i = 0; i < n; ++i) ar[i] = nextLong(); return ar; } public double nextDouble() { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() { try { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } catch (IOException e) { throw new RuntimeException(); } } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
476ca91b22fe47249dce132ba8e6dab6
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); //Scanner sc = new Scanner(); Reader in = new Reader(); Main solver = new Main(); solver.solve(out, in); out.flush(); out.close(); } static int maxn = (int)1e5*2; static int mod = 998244353; static int arr[],cnt[],freq[],mat[][][]; static double[] p; static int n,k,m,c,x,t,ans; void solve(PrintWriter out, Reader in) throws IOException{ n = in.nextInt(); arr = new int[n]; for(int i=0;i<n;i++) arr[i] = (in.nextInt()%2==0 ? 0:1); if(n%2==0){ if(check(0,n-1)==0) out.println("YES"); else out.println("NO"); }else{ int temp = check(0,n-1); if(temp==1 || temp==-1) out.println("YES"); else out.println("NO"); } } static int check(int s,int e){ int sum=0; for(int i=s;i<=e;i++) sum+= (i%2==arr[i] ? 1:-1); return sum; } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
47a7350fe7bbf82a7df5416b155f83ce
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class cdf527d1 { static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } public static int lowerBound(int[] array, int length, int value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(int[] array, int length, int value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value >= array[mid]) { low = mid + 1; } else { high = mid; } } return low; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long power(long n,long m) { if(m==0) return 1; long ans=1; while(m>0) { ans=ans*n; m--; } return ans; } static int BinarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); ArrayList<Integer> arrli=new ArrayList<Integer>(); String s=br.readLine(); String str[]=s.split(" "); for(int i=0;i<t;i++) { int temp=Integer.parseInt(str[i]); temp%=2; if(arrli.size()==0) { arrli.add(temp); continue; } if(arrli.get(arrli.size()-1)%2==temp%2) arrli.remove(arrli.size()-1); else arrli.add(temp); } if(arrli.size()<=1) System.out.println("YES"); else System.out.println("NO"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
32c3ac0c8587f89063f035c4ab2f1d45
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; public class Solution { public static void main(String args[]) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); List<Integer> li = new ArrayList<>(); for (int i = 0; i < n; i++) { int m = li.size(); int num = sc.nextInt() % 2; if(m > 0 && li.get(m - 1) == num) { li.remove(m-1); } else { li.add(num); } } if(li.size() < 2) System.out.println("YES"); else System.out.println("NO"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
e044f5784262cd50e2102f4bd5c844a0
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.util.*; public class Solution { public static void main(String args[]) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); List<Integer> li = new ArrayList<>(); for (int i = 0; i < n; i++) { int m = li.size(); int num = sc.nextInt() % 2; if(m > 0 && li.get(m - 1) == num) { li.set(m - 1, num); li.remove(m-1); } else { li.add(num); } } if(li.size() < 2) System.out.println("YES"); else System.out.println("NO"); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
3600257943e87b3e2be33ce30bb471e5
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
import java.io.*; import java.math.*; import java.security.KeyStore.Entry; import java.util.*; /* yash jobanputra DA-IICT */ public class yash { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long mod = 1000000000 + 7; private static long mod1 = 1000000000 + 9; private static int MAX=1000001; private static int block; private static int[] cost; private static int times; //private static int times2; private static boolean[] vis; private static int[] mark; private static int count; private static boolean ff; private static void soln(){ int n=ni(); int[] arr=nia(n); Stack<Integer> st=new Stack<>(); for(int i=0;i<n;i++) { if(st.isEmpty()) st.push(arr[i]); else if((st.peek()%2)==(arr[i]%2)) st.pop(); else st.push(arr[i]); } if(st.size()==1 || st.size()==0) pw.println("YES"); else pw.println("NO"); } private static class graph{ public static void dfs(ArrayList<Integer>[] gr,int v) { vis[v]=true; ArrayList<Integer> arr=gr[v]; for(int i=0;i<arr.size();i++) { if(!vis[arr.get(i)]) { count++; dfs(gr,arr.get(i)); } } } public static void arti(boolean[] ap,int[] parent,int[] disc,int[] low,ArrayList<Integer>[] gr,boolean[] vis,int ver) { int child=0; vis[ver]=true; ArrayList<Integer> arr=gr[ver]; times++; disc[ver]=low[ver]=times; for(int i=0;i<arr.size();i++) { if(!vis[arr.get(i)]) { child++; parent[arr.get(i)]=ver; arti(ap,parent,disc,low,gr,vis,arr.get(i)); low[ver]=Math.min(low[ver], low[arr.get(i)]); if(parent[ver]!=0 && low[arr.get(i)]>=disc[ver]) ap[ver]=true; if(parent[ver]==0 && child>1) ap[ver]=true; } else if(parent[ver]!=arr.get(i)) low[ver]=Math.min(low[ver], disc[arr.get(i)]); } } public static void floyd(Pair[] arr,int n,int m,int[][] dist) { for(int i=0;i<=n;i++) { Arrays.fill(dist[i], Integer.MAX_VALUE); } for(int c=0;c<m;c++) { int w=arr[c].v; int a=arr[c].j.v; int b=arr[c].j.i; dist[a][b]=w; } for(int i=0;i<=n;i++) dist[i][i]=0; for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(dist[i][k]!=Integer.MAX_VALUE && dist[k][j]!=Integer.MAX_VALUE) dist[i][j]=Math.min(dist[i][j], dist[i][k]+dist[k][j]); } } } } public static boolean bfs(ArrayList<Integer>[] gr,int v,int n) { boolean[] vis=new boolean[n+1]; int c2=0; Queue<Integer> q=new LinkedList<>(); q.add(v); while(!q.isEmpty()) { int x=q.poll(); Iterator<Integer> it=gr[x].iterator(); while(it.hasNext()) { int p=it.next(); if(!vis[p]) { q.add(p); c2++; vis[p]=true; } } } return c2==n; } public static ArrayList<Pair> MSTP(int n,int m,ArrayList<Pair>[] arr,int[][] arr2) { ArrayList<Pair> pp=new ArrayList<>(); int[] par=new int[n+1]; int[] key=new int[n+1]; vis=new boolean[n+1]; Arrays.fill(key, Integer.MAX_VALUE); key[1]=0; PriorityQueue<Integer> dis=new PriorityQueue<>(n+1, new Comparator<Integer>() { public int compare(Integer p,Integer q) { return key[p]-key[q]; } } ); dis.add(1); vis[1]=true; while(!dis.isEmpty()) { //pw.println(dis); int u=dis.poll(); ArrayList<Pair> p=arr[u]; for(int i=0;i<p.size();i++) { if(key[u]+p.get(i).i<key[p.get(i).v]) { key[p.get(i).v]=key[u]+p.get(i).i; par[p.get(i).v]=u; if(!vis[p.get(i).v]) { dis.add(p.get(i).v); vis[p.get(i).v]=true; } } } } for(int i=1;i<=n;i++) pp.add(new Pair(i,par[i])); return pp; } public static ArrayList<Pair> MSTK(int n,int m,Pair[] arr) { ArrayList<Pair> pp=new ArrayList<>(); long sum=0; Arrays.sort(arr); DSU x=new DSU(n+1); for(int c=0;(c<m);c++) { Pair p=arr[c]; //int a=p.v; //int b=p.i; int a=p.j.v; int b=p.j.i; int w=p.v; if(x.find(a)!=x.find(b)) { pp.add(new Pair(w,new Pair(a,b))); x.Union(a, b); } } return pp; } public static int[] dijkstras(ArrayList<Pair>[] gr) { int n=gr.length-1; int[] dist=new int[n+1]; Arrays.fill(dist, Integer.MAX_VALUE); PriorityQueue<Integer> dis=new PriorityQueue<>(n+1, new Comparator<Integer>() { public int compare(Integer p,Integer q) { return dist[p]-dist[q]; } } ); boolean[] vis=new boolean[n+1]; Arrays.fill(vis, false); int s=1; dist[s]=0; dis.add(s); vis[s]=true; while(!dis.isEmpty()) { int p=dis.poll(); for(int i=0;i<gr[p].size();i++) { int y=gr[p].get(i).v; int w=gr[p].get(i).i; if(!vis[y]) { dist[y]=dist[p]+w; dis.add(y); vis[y]=true; } else { if(dist[p]+w<dist[y]) { dist[y]=dist[p]+w; } } } } return dist; } } public static class sieve{ public static ArrayList<Integer> sieves(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean[] bol=new boolean[n+1]; Arrays.fill(bol, true); //arr.add(1); for(int i=2;i<=n;i++) { if(bol[i]) { arr.add(i); for(int j=2;j*i<=n;j++) { bol[i*j]=false; } } } return arr; } } public static class isprime{ public static boolean check(int n) { if(n==2) return true; else if(n==3) return true; else if(n%2==0) return false; else if(n%3==0) return false; else { for(int i=6;;i+=6) { if(i>Math.sqrt(n)) break; if(n%(i-1)==0) { return false; } else if(n%(i+1)==0) return false; } return true; } } } private static class DSU{ int[] parent; int[] rank; int cnt; public DSU(int n){ parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++){ parent[i]=i; rank[i]=1; } cnt=n; } int find(int i){ while(parent[i] !=i){ parent[i]=parent[parent[i]]; i=parent[i]; } return i; } int Union(int x, int y){ int xset = find(x); int yset = find(y); if(xset!=yset) cnt--; if(rank[xset]<rank[yset]){ parent[xset] = yset; rank[yset]+=rank[xset]; rank[xset]=0; return yset; }else{ parent[yset]=xset; rank[xset]+=rank[yset]; rank[yset]=0; return xset; } } } private static class Pair implements Comparable<Pair>{ int v,i;Pair j; public Pair(int a,int b){ v=a; i=b; } public Pair(int a,Pair b) { v=a; j=b; } @Override public int compareTo(Pair arg0) { { return this.v-arg0.v; } } } private static class Pairl implements Comparable<Pairl>{ long v,i;Pairl j; public Pairl(long a,long b){ v=a; i=b; } public Pairl(long a,Pairl b) { v=a; j=b; } @Override public int compareTo(Pairl arg0) { { if(this.v>arg0.v) return 1; else return -1; } } } public static long f(long number,long m) { if (number <= 1) return 1; else return (number * f(number - 1,m))%m; } public static long mmi(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process same as // Euclid's algo m = a % m; a = t; t = y; // Update y and x y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } public static class segtree{ static int[] tree=new int[10000000]; static int[] lazy=new int[10000000]; public static void initial() { Arrays.fill(lazy, 0); } public static void build(int[] arr,int node,int l,int r) { if(l==r) { tree[node]=arr[l]; } else { int m=(l+r)/2; build(arr,(2*node)+1,l,m); build(arr,(2*node)+2,m+1,r); tree[node]=tree[2*node+1]+tree[(2*node)+2]; } } public static void updater(int[] arr,int node,int l,int r,int s,int e,int val) { if(lazy[node]!=0) { tree[node]+=(r-l+1)*lazy[node]; if(l!=r) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(s<=l && e>=r) { tree[node]+=(r-l+1)*val; if(l!=r) { lazy[2*node+1]+=val; lazy[2*node+2]+=val; } } else if(!(e<l || r<s)) { int m=(l+r)/2; updater(arr,2*node+1,l,m,s,e,val); updater(arr,2*node+2,m+1,r,s,e,val); tree[node]=tree[2*node+1]+tree[2*node+2]; } } public static void update(int[] arr,int node,int l,int r,int ind,int val) { if(l==r) { arr[ind]+=val; tree[node]+=val; } else { int m=(l+r)/2; if(l<=ind && ind<=m) { update(arr,(2*node+1),l,m,ind,val); } else { update(arr,(2*node+2),m+1,r,ind,val); } tree[node]=tree[2*node+1]+tree[2*node+2]; } } public static int query(int node,int l,int r,int s,int e) { if(lazy[node]!=0) { tree[node]+=(r-l+1)*lazy[node]; if(l!=r) { lazy[2*node+1]+=lazy[node]; lazy[2*node+2]+=lazy[node]; } lazy[node]=0; } if(e<l || r<s) return 0; if(s<=l && e>=r) return tree[node]; int m=(l+r)/2; int p1=query((2*node+1),l,m,s,e); int p2=query((2*node+2),m+1,r,s,e); return (p1+p2); } } private static long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } private static long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } private static int gcd(int n, int l) { if (l == 0) return n; return gcd(l, n % l); } private static long max(long a, long b) { if (a > b) return a; return b; } private static long min(long a, long b) { if (a < b) return a; return b; } public static void main(String[] args) throws Exception { new Thread(null,new Runnable(){ @Override public void run() { /*try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\C-small-1-attempt1.in")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ InputReader(System.in); pw = new PrintWriter(System.out); /*try { pw=new PrintWriter(new FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ soln(); pw.close(); } },"1",1<<26).start(); } public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static 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++]; } private static int ni() { 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; } private static long nl() { 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; } private static double nd() { double ret = 0, div = 1; int 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 static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nli() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nia(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } private static long[] nla(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } private static void pa(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pa(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private static char nc() { int c = read(); while (isSpaceChar(c)) c = read(); char c1=(char)c; while(!isSpaceChar(c)) c=read(); return c1; } public String ns() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
b3d03ab1276e12a409eef15d2542a0cd
train_003.jsonl
1545143700
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall.Vova can only use $$$2 \times 1$$$ bricks to put in the wall (he has infinite supply of them, however).Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it).The next paragraph is specific to the version 1 of the problem.Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)?
256 megabytes
//package Codef; import java.util.Scanner; import java.util.Stack; public class D1_1092 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } //System.out.println(-2%2); Stack<Integer> st = new Stack<>(); boolean fg = false; for(int i=0;i<n;i++) { //int diff = Math.abs(st.peek()-a[i]); if(st.isEmpty()) { st.push(a[i]); }else if((st.peek()-a[i] )%2 ==0) { st.pop(); }else { st.push(a[i]); } } if(st.size()<=1) { System.out.println("YES"); }else { System.out.println("NO"); } } }
Java
["5\n2 1 1 2 5", "3\n4 5 3", "2\n10 10", "3\n1 2 3"]
2 seconds
["YES", "YES", "YES", "NO"]
NoteIn the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$.In the second example Vova can put a brick vertically on part 3 to make the wall $$$[4, 5, 5]$$$, then horizontally on parts 2 and 3 to make it $$$[4, 6, 6]$$$ and then vertically on part 1 to make it $$$[6, 6, 6]$$$.In the third example the wall is already complete.
Java 8
standard input
[ "implementation", "greedy", "math" ]
bb4ecfaaccd538e23f883a18f9672af8
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial heights of the parts of the wall.
2,200
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise.
standard output
PASSED
179117b1e659e7f18a125d2066372847
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.io.BufferedReader; import java.util.List; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; import java.util.Collection; /** * Built using CHelper plug-in * Actual solution is at the top * * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC_CF_XXX solver = new TaskC_CF_XXX(); solver.solve(1, in, out); out.close(); } static class TaskC_CF_XXX { Graph g; int[][][] onPath; public int HEIGHT; int[][] parent; DFSOrder order; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int countPeople = in.nextInt(); int countQueries = in.nextInt(); g = new Graph(); g.initGraph(n, n - 1); for (int i = 0; i < n - 1; ++i) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; g.addEdge(a, b); } parent = Parent.getLevelParents(g, 0); int maxLevel = 1; while ((1 << maxLevel) <= g.nVertex) { ++maxLevel; } HEIGHT = maxLevel; onPath = new int[HEIGHT][n][0]; order = new DFSOrder(g, 0, parent[0]); for (int i = 0; i < countPeople; ++i) { int city = in.nextInt() - 1; int[] to = new int[]{i}; onPath[0][city] = merge(onPath[0][city], to, 10); } dfs(0, -1); for (int i = 0; i < countQueries; ++i) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; int am = in.nextInt(); int l = lca(a, b); int[] a1 = get(a, l, am); int[] a2 = get(b, l, am); int[] res = merge(a1, a2, am); res = merge(res, onPath[0][l], am); out.print(res.length); for (int x : res) { out.print(" " + (x + 1)); } out.println(); } } private int lca(int a, int b) { if (order.isChild(a, b)) return b; if (order.isChild(b, a)) return a; for (int h = HEIGHT - 1; h >= 0; --h) { if (parent[h][a] == -1) continue; if (order.isChild(b, parent[h][a])) continue; a = parent[h][a]; } return parent[0][a]; } private void dfs(int v, int p) { for (int j = 1; j < HEIGHT; ++j) { if (parent[j - 1][v] != -1) { onPath[j][v] = merge(onPath[j - 1][v], onPath[j - 1][parent[j - 1][v]], 10); } } for (int ei = g.first[v]; ei != -1; ei = g.next[ei]) { int to = g.to[ei]; if (to == p) continue; dfs(to, v); } } int[] merge(int[] a, int[] b, int am) { List<Integer> res = new ArrayList<Integer>(am); int ptrA = 0, ptrB = 0; while (res.size() < am) { if (ptrA >= a.length && ptrB >= b.length) break; int ax = ptrA >= a.length ? Integer.MAX_VALUE : a[ptrA]; int bx = ptrB >= b.length ? Integer.MAX_VALUE : b[ptrB]; if (ax < bx) { res.add(ax); ++ptrA; } else { res.add(bx); ++ptrB; } } return ArrayUtils.toArray(res); } int[] get(int v, int p, int am) { int[] res = new int[0]; for (int h = HEIGHT - 1; h >= 0; --h) { if (parent[h][v] == -1) continue; int vp = parent[h][v]; if (order.isChild(p, vp) && p != vp) continue; res = merge(res, onPath[h][v], am); v = vp; } return res; } } static class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } } static class ArrayUtils { public static int[] toArray(Collection<Integer> collection) { int[] array = new int[collection.size()]; int index = 0; for (int element : collection) { array[index++] = element; } return array; } public static void fill(int[][] f, int value) { for (int i = 0; i < f.length; ++i) { Arrays.fill(f[i], value); } } } static class Parent { public static int[] run(Graph tree, int root) { int[] parent = new int[tree.nVertex]; int[] queue = new int[tree.nVertex]; int front = 0, back = 0; queue[back++] = root; parent[root] = -1; while (front < back) { int v = queue[front++]; for (int i = tree.first[v]; i != -1; i = tree.next[i]) { if (parent[v] != tree.to[i]) { queue[back++] = tree.to[i]; parent[tree.to[i]] = v; } } } return parent; } public static int[][] getLevelParents(Graph tree, int root) { int maxLevel = 1; while ((1 << maxLevel) <= tree.nVertex) { ++maxLevel; } int[][] parent = new int[maxLevel][tree.nVertex]; ArrayUtils.fill(parent, -1); parent[0] = Parent.run(tree, root); for (int level = 1; level < maxLevel; ++level) { for (int i = 0; i < tree.nVertex; ++i) { if (parent[level - 1][i] != -1) { int u = parent[level - 1][i]; if (parent[level - 1][u] != -1) { int v = parent[level - 1][u]; parent[level][i] = v; } } } } return parent; } } static class DFSOrder { int[] start; int[] end; public DFSOrder(Graph tree, int root, int[] p) { start = new int[tree.nVertex]; end = new int[tree.nVertex]; buildDfsOrder(tree, root, p); } public boolean isChild(int candidate, int v) { return start[v] <= start[candidate] && end[v] >= end[candidate]; } private void buildDfsOrder(Graph tree, int root, int[] p) { int[] stack = new int[2 * tree.nVertex]; int top = 0; stack[top++] = (root + 1); int time = 0; while (top > 0) { --top; int v = stack[top]; if (v < 0) { v = (-v) - 1; end[v] = time; ++time; } else { --v; start[v] = time++; stack[top++] = -(v + 1); for (int i = tree.first[v]; i != -1; i = tree.next[i]) { if (p[v] != tree.to[i]) { stack[top++] = (tree.to[i] + 1); } } } } } } static class Graph { public int[] from; public int[] to; public int[] first; public int[] next; public int nVertex; public int nEdges; public int curEdge; public Graph() { } public void initGraph(int n, int m) { curEdge = 0; nVertex = n; nEdges = m; from = new int[m * 2]; to = new int[m * 2]; first = new int[n]; next = new int[m * 2]; Arrays.fill(first, -1); } public void addEdge(int a, int b) { next[curEdge] = first[a]; first[a] = curEdge; to[curEdge] = b; from[curEdge] = a; ++curEdge; next[curEdge] = first[b]; first[b] = curEdge; to[curEdge] = a; from[curEdge] = b; ++curEdge; } } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
4ed796bc8558e17b29f9ba87f289541d
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 megabytes
import java.io.*; import java.math.*; import java.util.*; import static java.util.Arrays.fill; import static java.lang.Math.*; import static java.util.Arrays.sort; import static java.util.Collections.sort; public class E588 { public static int mod = 1000000007; public static long INF = (1L << 60); static FastScanner2 in = new FastScanner2(); static OutputWriter out = new OutputWriter(System.out); static ArrayList<Integer>[] adjacencylist=new ArrayList[100001]; static int[] tin=new int[100001]; static int[] tout=new int[100001]; static int[][] up=new int[100001][20]; static int[][] lives=new int[100001][10]; static int[][][] mins=new int[100001][20][10]; static int[] count=new int[100001]; static int time=0; static int log=1; static int n,m; public static void dfs(int v,int parent) { tin[v]=++time; up[v][0]=parent; mins[v][0]=lives[v]; for(int i=1;i<=log;i++) { up[v][i]=up[up[v][i-1]][i-1]; mins[v][i]=merge(mins[up[v][i-1]][i-1], mins[v][i-1]); } for(int u : adjacencylist[v]) { if(u!=parent) { dfs(u, v); } } tout[v]=++time; } public static boolean isancestor(int u,int v) { return tin[u]<=tin[v] && tout[u]>=tout[v]; } public static int lca(int u,int v) { if(isancestor(u, v)) return u; if(isancestor(v, u)) return v; for(int i=log;i>=0;i--) { if(!isancestor(up[u][i], v)) u=up[u][i]; } return up[u][0]; } public static int[] merge(int[] a,int[] b) { int[] c=new int[10]; Arrays.fill(c, mod); int i=0,j=0; int count=0; while(count<10) { if(a[i]==mod && b[j]==mod) break; if(a[i]<b[j]) { c[count++]=a[i++]; } else { c[count++]=b[j++]; } } return c; } public static int[] getpath(int u,int lca) { int[] ret=new int[10]; Arrays.fill(ret, mod); if(u==lca) return ret; for(int i=log;i>=0;i--) { if(!isancestor(up[u][i], lca)) { ret=merge(ret, mins[u][i]); u=up[u][i]; } } ret=merge(ret, mins[u][0]); return ret; } public static void main(String[] args) { for(int i=0;i<100001;i++) { Arrays.fill(lives[i], mod); Arrays.fill(up[i], 1); for(int j=0;j<10;j++) { Arrays.fill(mins[i][j], mod); } } n=in.nextInt(); int power=1; while(power<=n) { log++; power=(power<<1); } for(int i=1;i<=n;i++) { adjacencylist[i]=new ArrayList<>(); } m=in.nextInt(); int queries=in.nextInt(); for(int i=1;i<n;i++) { int u=in.nextInt(); int v=in.nextInt(); adjacencylist[u].add(v); adjacencylist[v].add(u); } for(int i=1;i<=m;i++) { int c=in.nextInt(); if(count[c]<10) { lives[c][count[c]]=i; count[c]++; } } dfs(1, 1); while(queries-->0) { int u=in.nextInt(); int v=in.nextInt(); int a=in.nextInt(); int lca=lca(u, v); int[] answer1=getpath(u, lca); int[] answer2=getpath(v, lca); int[] ans=merge(answer1, answer2); ans=merge(ans, lives[lca]); int count=0; for(int i=0;i<10;i++) { if(ans[i]==mod) break; count++; } out.print(min(count, a)+" "); for(int i=0;i<min(count, a);i++) out.print(ans[i]+" "); out.println(); } out.close(); } public static long pow(long x, long n) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p)) { if ((n & 1) != 0) { res = (res * p); } } return res; } public static long pow(long x, long n, long mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = (res * p % mod); } } return res; } public static long gcd(long n1, long n2) { long r; while (n2 != 0) { r = n1 % n2; n1 = n2; n2 = r; } return n1; } public static long lcm(long n1, long n2) { long answer = (n1 * n2) / (gcd(n1, n2)); return answer; } static class FastScanner2 { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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 long nextLong() { return Long.parseLong(next()); } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
5e83b25410ff9964e1bb0fb6833cd6ec
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 megabytes
import java.io.*; import java.math.*; import java.util.*; import static java.util.Arrays.fill; import static java.lang.Math.*; import static java.util.Arrays.sort; import static java.util.Collections.sort; public class E588 { public static int mod = 1000000007; public static long INF = (1L << 60); static FastScanner2 in = new FastScanner2(); static OutputWriter out = new OutputWriter(System.out); static ArrayList<Integer>[] adjacencylist=new ArrayList[100001]; static int[] tin=new int[100001]; static int[] tout=new int[100001]; static int[][] up=new int[100001][20]; static int[][] lives=new int[100001][10]; static int[][][] mins=new int[100001][20][10]; static int[] count=new int[100001]; static int time=0; static int log=0; static int n,m; public static void dfs(int v,int parent) { tin[v]=++time; up[v][0]=parent; mins[v][0]=lives[v]; for(int i=1;i<=log;i++) { up[v][i]=up[up[v][i-1]][i-1]; mins[v][i]=merge(mins[up[v][i-1]][i-1], mins[v][i-1]); } for(int u : adjacencylist[v]) { if(u!=parent) { dfs(u, v); } } tout[v]=++time; } public static boolean isancestor(int u,int v) { return tin[u]<=tin[v] && tout[u]>=tout[v]; } public static int lca(int u,int v) { if(isancestor(u, v)) return u; if(isancestor(v, u)) return v; for(int i=log;i>=0;i--) { if(!isancestor(up[u][i], v)) u=up[u][i]; } return up[u][0]; } public static int[] merge(int[] a,int[] b) { int[] c=new int[10]; Arrays.fill(c, mod); int i=0,j=0; int count=0; while(count<10) { if(a[i]==mod && b[j]==mod) break; if(a[i]<b[j]) { c[count++]=a[i++]; } else { c[count++]=b[j++]; } } return c; } public static int[] getpath(int u,int lca) { int[] ret=new int[10]; Arrays.fill(ret, mod); if(u==lca) return ret; for(int i=log;i>=0;i--) { if(!isancestor(up[u][i], lca)) { ret=merge(ret, mins[u][i]); u=up[u][i]; } } ret=merge(ret, mins[u][0]); return ret; } public static void main(String[] args) { for(int i=0;i<100001;i++) { Arrays.fill(lives[i], mod); Arrays.fill(up[i], 1); for(int j=0;j<10;j++) { Arrays.fill(mins[i][j], mod); } } n=in.nextInt(); int power=1; while(power<=n) { log++; power=(power<<1); } for(int i=1;i<=n;i++) { adjacencylist[i]=new ArrayList<>(); } m=in.nextInt(); int queries=in.nextInt(); for(int i=1;i<n;i++) { int u=in.nextInt(); int v=in.nextInt(); adjacencylist[u].add(v); adjacencylist[v].add(u); } for(int i=1;i<=m;i++) { int c=in.nextInt(); if(count[c]<10) { lives[c][count[c]]=i; count[c]++; } } dfs(1, 1); while(queries-->0) { int u=in.nextInt(); int v=in.nextInt(); int a=in.nextInt(); int lca=lca(u, v); int[] answer1=getpath(u, lca); int[] answer2=getpath(v, lca); int[] ans=merge(answer1, answer2); ans=merge(ans, lives[lca]); int count=0; for(int i=0;i<10;i++) { if(ans[i]==mod) break; count++; } out.print(min(count, a)+" "); for(int i=0;i<min(count, a);i++) out.print(ans[i]+" "); out.println(); } out.close(); } public static long pow(long x, long n) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p)) { if ((n & 1) != 0) { res = (res * p); } } return res; } public static long pow(long x, long n, long mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = (res * p % mod); } } return res; } public static long gcd(long n1, long n2) { long r; while (n2 != 0) { r = n1 % n2; n1 = n2; n2 = r; } return n1; } public static long lcm(long n1, long n2) { long answer = (n1 * n2) / (gcd(n1, n2)); return answer; } static class FastScanner2 { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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 long nextLong() { return Long.parseLong(next()); } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
fa03489ad5b8296f1ee9b578204de634
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 megabytes
//package codeforces.cfr326div2; import java.io.*; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** * Created by raggzy on 31-Oct-15. */ public class F { static final int MAX_LOG = 18; static final int MAX_SIZE = 10; static int n; static int m; static int q; static City[] cities; static class City { int height; KMins[] mins = new KMins[MAX_LOG]; int[] parents = new int[MAX_LOG]; List<Integer> neighbors = new LinkedList<>(); List<Integer> people = new LinkedList<>(); } static class KMins { int[] arr = new int[MAX_SIZE]; int[] tmp = new int[MAX_SIZE]; { clear(); } private static void merge(int[] arr1, int[] arr2, int[] dest) { int arr1C = 0; int arr2C = 0; for (int i = 0; i < MAX_SIZE; i++) { int tmpO = arr1C < arr1.length ? arr1[arr1C] : Integer.MAX_VALUE; int otherO = arr2C < arr2.length ? arr2[arr2C] : Integer.MAX_VALUE; if (tmpO == otherO) { dest[i] = tmpO; arr1C++; arr2C++; } else if (tmpO < otherO) { dest[i] = tmpO; arr1C++; } else { dest[i] = otherO; arr2C++; } if (dest[i] == Integer.MAX_VALUE) break; } } KMins mergeWith(int[] other) { System.arraycopy(arr, 0, tmp, 0, MAX_SIZE); merge(other, tmp, arr); return this; } KMins initFrom(KMins one, KMins two) { merge(one.arr, two.arr, arr); return this; } KMins mergeWith(List<Integer> list) { int i = 0; for (int v : list) { arr[i++] = v; if (i >= MAX_SIZE) break; } return this; } KMins mergeWith(KMins other) { return mergeWith(other.arr); } void sout(int a, StringBuilder sb) { int count = 0; for (int i = 0; i < MAX_SIZE; i++) { if (arr[i] < Integer.MAX_VALUE) { count++; sb.append(" ").append(arr[i]); } if (count >= a) { break; } } sb.insert(0, count); } void clear() { Arrays.fill(arr, Integer.MAX_VALUE); } } private static void calcStuff(int curr, int prev, int h, int[] parents) { City city = cities[curr]; city.height = h; city.parents[0] = curr; city.mins[0] = new KMins().mergeWith(city.people); parents[h] = curr; for (int i = 1; i < MAX_LOG; i++) { int jump = 1 << (i - 1); int parentIdx = Math.max(0, h - jump); city.parents[i] = parents[parentIdx]; City midParent = cities[city.parents[i - 1]]; city.mins[i] = new KMins().initFrom(city.mins[i - 1], midParent.mins[i - 1]); } for (int neigh : city.neighbors) { if (neigh != prev) { calcStuff(neigh, curr, h + 1, parents); } } } private static int lcaHeight(City one, City two) { for (int jmp = MAX_LOG - 1; jmp >= 0; jmp--) { if (one.parents[jmp] != two.parents[jmp]) { one = cities[cities[one.parents[jmp]].parents[1]]; two = cities[cities[two.parents[jmp]].parents[1]]; } } return one.height; } private static City parent(City city, int parentHeight) { for (int jmp = MAX_LOG - 1; jmp >= 0; jmp--) { City parent = cities[city.parents[jmp]]; if (parent.height >= parentHeight) { city = parent; } } return city; } private static void mergeKmins(City city, int parentHeight, KMins dest) { for (int jmp = MAX_LOG - 1; jmp >= 0; jmp--) { City parent = cities[city.parents[jmp]]; if (parent.height >= parentHeight) { dest.mergeWith(city.mins[jmp]); city = parent; } } } private static void query(City one, City two, KMins result) { if (one.height < two.height) { City tmp = one; one = two; two = tmp; } // eq height mergeKmins(one, two.height, result); one = parent(one, two.height); int lcaHeight = lcaHeight(one, two); mergeKmins(one, lcaHeight, result); mergeKmins(two, lcaHeight, result); } private static int nextInt(StreamTokenizer in) throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintStream out = new PrintStream(System.out); // StreamTokenizer in = new StreamTokenizer(new BufferedReader(new FileReader("C:/Temp/test.in"))); // PrintStream out = new PrintStream(new FileOutputStream("C:/Temp/test.out")); n = nextInt(in); m = nextInt(in); q = nextInt(in); cities = new City[1 + n]; for (int i = 1; i <= n; i++) { cities[i] = new City(); } for (int i = 0; i < n - 1; i++) { int v1 = nextInt(in); int v2 = nextInt(in); cities[v1].neighbors.add(v2); cities[v2].neighbors.add(v1); } for (int i = 0; i < m; i++) { int c = nextInt(in); cities[c].people.add(1 + i); } int[] tmp = new int[n]; calcStuff(1, 0, 0, tmp); KMins tmpKmins = new KMins(); StringBuilder tmpBuilder = new StringBuilder(100); for (int i = 0; i < q; i++) { int u = nextInt(in); int v = nextInt(in); int a = nextInt(in); tmpKmins.clear(); tmpBuilder.setLength(0); query(cities[u], cities[v], tmpKmins); tmpKmins.sout(a, tmpBuilder); out.println(tmpBuilder.toString()); } System.out.println(); } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
ee8e4efd881a312d6ccd1a08fe1b1740
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { // int n = in.nextInt(); // long a[] = new long[n]; // for (int i = 0; i < n; i++) a[i] = in.nextInt(); // Arrays.sort(a); // int n = in.nextInt(); int m = in.nextInt(); // int a[][] = new int[n][m]; // for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = in.nextInt(); // string s = in.next(); public static final int N = 100000+5; public static int log = 1; public static int p[] = new int[N]; public static long MOD = 1000000000 + 7; public static int tin[] = new int[N]; public static int tout[] = new int[N]; public static int lca[][] = new int[N][20]; public static int mins[][][] = new int[N][20][11]; public static int lives[][] = new int[N][11]; public static int ll[] = new int[N]; public static ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(); public static int timer = 0; public static int[] merge(int a[], int b[]) { int c[] = new int[15]; int count = 0; int i = 0; int j = 0; while (count < 10 && (a[i] != 0 || b[j] != 0)) { count++; if (a[i] == 0) { c[count - 1] = b[j]; j++; } else if (b[j] == 0) { c[count - 1] = a[i]; i++; } else if (a[i] != 0 && b[j] != 0) { if (a[i] < b[j]) { c[count - 1] = a[i]; i++; } else { c[count - 1] = b[j]; j++; } } } return c; } public static void setTimers(int t, int p) { timer++; tin[t] = timer; lca[t][0] = p; mins[t][0] = lives[t]; for (int i = 1; i <= log; ++i) { lca[t][i] = lca[lca[t][i - 1]][i - 1]; mins[t][i] = merge(mins[t][i - 1], mins[lca[t][i - 1]][i - 1]); } for (int to : adj.get(t)) { if (to != p) { setTimers(to, t); } } timer++; tout[t] = timer; } public static boolean isP(int a, int b) { if (a == 0) return true; return tin[a] <= tin[b] && tout[a] >= tout[b]; } public static int lca(int a, int b) { if (isP(a, b)) return a; if (isP(b, a)) return b; for (int i = log; i >= 0; i--) { if (!isP(lca[a][i], b)) { a = lca[a][i]; } } return lca[a][0]; } public static int[] getPath(int a, int b) { if (a == b) return new int[15]; int ans[] = new int[15]; for (int i = log; i >= 0; --i) { if (!isP(lca[a][i], b)) { ans = merge(ans, mins[a][i]); a = lca[a][i]; } } ans = merge(ans, mins[a][0]); return ans; } public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); int pow = 1; while (pow <= n) { log++; pow *= 2; } for (int i = 0; i <= n; i++) { adj.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { int u = in.nextInt(); int v = in.nextInt(); adj.get(u).add(v); adj.get(v).add(u); } for (int i = 1; i <= m; i++) { int c = in.nextInt(); if (ll[c] < 10) { ll[c]++; lives[c][ll[c] - 1] = i; } } setTimers(1, 0); for (int i = 0; i < q; i++) { int u = in.nextInt(); int v = in.nextInt(); int a = in.nextInt(); int lca = lca(u, v); int a1[] = getPath(u, lca); int a2[] = getPath(v, lca); int ans[] = merge(a1, a2); ans = merge(ans, lives[lca]); int ll = 0; while (ans[ll] != 0) { ll++; } int len = Math.min(ll, a); out.print(len); for (int ii = 0; ii < len; ii++) { out.print(" " + ans[ii]); } out.println(); } out.close(); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() throws IOException { return reader.readLine(); } 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 double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
b458642f2923901f40bc5e6a09e42c1f
train_003.jsonl
1444926600
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
512 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.util.Iterator; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author IgorKoval */ 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); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { final int maxP = 16; int n; int m; int q; Graph gr; int[][] pr; IntArrayList[][] people; int[] depth; public void solve(int testNumber, InputReader in, OutputWriter out) { // n = in.readInt(); m = in.readInt(); q = in.readInt(); gr = new BidirectionalGraph(n, n - 1); for (int iter = 0; iter < n - 1; iter++) { int a = in.readInt() - 1; int b = in.readInt() - 1; gr.addSimpleEdge(a, b); } people = new IntArrayList[maxP + 1][n]; for (int i = 0; i < n; i++) { people[0][i] = new IntArrayList(10); } for (int i = 1; i <= m; i++) { int v = in.readInt() - 1; if (people[0][v].size() < 10) people[0][v].add(i); } /**/ /*/ n = (int) 1e5; m = (int) 1e5; q = (int) 1e5; gr = new BidirectionalGraph(n, n - 1); for (int iter = 0; iter < n - 1; iter++) { int a = iter; int b = iter + 1; gr.addSimpleEdge(a, b); } people = new IntArrayList[maxP + 1][n]; for (int i = 0; i < n; i++) { people[0][i] = new IntArrayList(10); } for (int i = 1; i <= m; i++) { people[0][i % n].add(i); } /**/ depth = new int[n]; pr = new int[maxP + 1][n]; ArrayUtils.fill(pr, -1); dfs(0, -1); for (int p = 0; p <= maxP; p++) { for (int v = 0; v < n; v++) { if (p == 0) { } else { int up = pr[p - 1][v]; if (up == -1) { people[p][v] = people[p - 1][v]; } else { people[p][v] = merge(people[p - 1][v], people[p - 1][up]); pr[p][v] = pr[p - 1][up]; } } } } LCA lca = new LCA(gr); for (int iter = 0; iter < q; iter++) { int v = in.readInt() - 1; int to = in.readInt() - 1; int cnt = in.readInt(); // int v = 0; // int to = n - 1; // int cnt = 10; int vto = lca.getLCA(v, to); IntArrayList a = go(v, vto); IntArrayList b = go(to, vto); IntArrayList c = merge(a, b); c = merge(c, people[0][vto]); out.print(Math.min(cnt, c.size())); for (int i = 0; i < Math.min(cnt, c.size()); i++) { out.print(" "); out.print(c.get(i)); } // } out.printLine(); } } IntArrayList merge(IntArrayList a, IntArrayList b) { IntArrayList c = new IntArrayList(Math.min(10, a.size() + b.size())); int posA = 0; int posB = 0; for (; c.size() < 10 && posA != a.size() && posB != b.size(); ) { if (a.get(posA) < b.get(posB)) c.add(a.get(posA++)); else c.add(b.get(posB++)); } for (; c.size() < 10 && posA != a.size(); ) c.add(a.get(posA++)); for (; c.size() < 10 && posB != b.size(); ) c.add(b.get(posB++)); return c; } private IntArrayList go(int v, int u) { IntArrayList res = new IntArrayList(0); for (int p = maxP; 0 <= p; p--) { int to = pr[p][v]; if (to != -1 && depth[u] <= depth[to]) { res = merge(res, people[p][v]); v = to; } } if (!(v == u || pr[0][v] == u)) throw new RuntimeException(); return res; } private void dfs(int v, int p) { pr[0][v] = p; for (Edge edge : gr.outbound(v)) { if (edge.getDestination() == p) continue; depth[edge.getDestination()] = depth[v] + 1; dfs(edge.getDestination(), v); } } } static abstract class IntList extends IntCollection implements Comparable<IntList> { public abstract int get(int index); public IntIterator iterator() { return new IntIterator() { private int size = size(); private int index = 0; public int value() throws NoSuchElementException { if (!isValid()) throw new NoSuchElementException(); return get(index); } public void advance() throws NoSuchElementException { if (!isValid()) throw new NoSuchElementException(); index++; } public boolean isValid() { return index < size; } }; } public int hashCode() { int hashCode = 1; for (IntIterator i = iterator(); i.isValid(); i.advance()) hashCode = 31 * hashCode + i.value(); return hashCode; } public boolean equals(Object obj) { if (!(obj instanceof IntList)) return false; IntList list = (IntList) obj; if (list.size() != size()) return false; IntIterator i = iterator(); IntIterator j = list.iterator(); while (i.isValid()) { if (i.value() != j.value()) return false; i.advance(); j.advance(); } return true; } public int compareTo(IntList o) { IntIterator i = iterator(); IntIterator j = o.iterator(); while (true) { if (i.isValid()) { if (j.isValid()) { if (i.value() != j.value()) { if (i.value() < j.value()) return -1; else return 1; } } else return 1; } else { if (j.isValid()) return -1; else return 0; } i.advance(); j.advance(); } } } static class IntArrayList extends IntList { private int[] array; private int size; public IntArrayList() { this(10); } public IntArrayList(int capacity) { array = new int[capacity]; } public IntArrayList(IntList list) { this(list.size()); addAll(list); } public int get(int index) { if (index >= size) throw new IndexOutOfBoundsException(); return array[index]; } public int size() { return size; } public void add(int value) { ensureCapacity(size + 1); array[size++] = value; } public void ensureCapacity(int newCapacity) { if (newCapacity > array.length) { int[] newArray = new int[Math.max(newCapacity, array.length << 1)]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } } } static abstract class ReadOnlyIntervalTree extends IntervalTree { protected long[] value; protected long[] array; protected ReadOnlyIntervalTree(long[] array) { super(array.length, false); this.array = array; init(); } protected void initData(int size, int nodeCount) { value = new long[nodeCount]; } protected void initAfter(int root, int left, int right, int middle) { value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]); } protected void initBefore(int root, int left, int right, int middle) { } protected void initLeaf(int root, int index) { value[root] = array[index]; } protected long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult) { return joinValue(leftResult, rightResult); } protected void queryPreProcess(int root, int left, int right, int from, int to, int middle) { } protected long queryFull(int root, int left, int right, int from, int to) { return value[root]; } protected long emptySegmentResult() { return neutralValue(); } protected abstract long neutralValue(); protected abstract long joinValue(long left, long right); } 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 printLine() { writer.println(); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } static abstract class IntCollection { public abstract IntIterator iterator(); public abstract int size(); public abstract void add(int value); public void addAll(IntCollection values) { for (IntIterator it = values.iterator(); it.isValid(); it.advance()) { add(it.value()); } } } static abstract class IntervalTree { protected int size; protected IntervalTree(int size) { this(size, true); } public IntervalTree(int size, boolean shouldInit) { this.size = size; int nodeCount = Math.max(1, Integer.highestOneBit(size) << 2); initData(size, nodeCount); if (shouldInit) init(); } protected abstract void initData(int size, int nodeCount); protected abstract void initAfter(int root, int left, int right, int middle); protected abstract void initBefore(int root, int left, int right, int middle); protected abstract void initLeaf(int root, int index); protected abstract long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult); protected abstract void queryPreProcess(int root, int left, int right, int from, int to, int middle); protected abstract long queryFull(int root, int left, int right, int from, int to); protected abstract long emptySegmentResult(); public void init() { if (size == 0) return; init(0, 0, size - 1); } private void init(int root, int left, int right) { if (left == right) { initLeaf(root, left); } else { int middle = (left + right) >> 1; initBefore(root, left, right, middle); init(2 * root + 1, left, middle); init(2 * root + 2, middle + 1, right); initAfter(root, left, right, middle); } } public long query(int from, int to) { return query(0, 0, size - 1, from, to); } protected long query(int root, int left, int right, int from, int to) { if (left > to || right < from) return emptySegmentResult(); if (left >= from && right <= to) return queryFull(root, left, right, from, to); int middle = (left + right) >> 1; queryPreProcess(root, left, right, from, to, middle); long leftResult = query(2 * root + 1, left, middle, from, to); long rightResult = query(2 * root + 2, middle + 1, right, from, to); return queryPostProcess(root, left, right, from, to, middle, leftResult, rightResult); } } static class BidirectionalGraph extends Graph { public int[] transposedEdge; public BidirectionalGraph(int vertexCount) { this(vertexCount, vertexCount); } public BidirectionalGraph(int vertexCount, int edgeCapacity) { super(vertexCount, 2 * edgeCapacity); transposedEdge = new int[2 * edgeCapacity]; } public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { int lastEdgeCount = edgeCount; super.addEdge(fromID, toID, weight, capacity, reverseEdge); super.addEdge(toID, fromID, weight, capacity, reverseEdge == -1 ? -1 : reverseEdge + 1); this.transposedEdge[lastEdgeCount] = lastEdgeCount + 1; this.transposedEdge[lastEdgeCount + 1] = lastEdgeCount; return lastEdgeCount; } protected int entriesPerEdge() { return 2; } protected void ensureEdgeCapacity(int size) { if (size > edgeCapacity()) { super.ensureEdgeCapacity(size); transposedEdge = resize(transposedEdge, edgeCapacity()); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class LCA { private final long[] order; private final int[] position; private final Graph graph; private final IntervalTree lcaTree; private final int[] level; public LCA(Graph graph) { this(graph, 0); } public LCA(Graph graph, int root) { this.graph = graph; order = new long[2 * graph.vertexCount() - 1]; position = new int[graph.vertexCount()]; level = new int[graph.vertexCount()]; int[] index = new int[graph.vertexCount()]; for (int i = 0; i < index.length; i++) index[i] = graph.firstOutbound(i); int[] last = new int[graph.vertexCount()]; int[] stack = new int[graph.vertexCount()]; stack[0] = root; int size = 1; int j = 0; last[root] = -1; Arrays.fill(position, -1); while (size > 0) { int vertex = stack[--size]; if (position[vertex] == -1) position[vertex] = j; order[j++] = vertex; if (last[vertex] != -1) level[vertex] = level[last[vertex]] + 1; while (index[vertex] != -1 && last[vertex] == graph.destination(index[vertex])) index[vertex] = graph.nextOutbound(index[vertex]); if (index[vertex] != -1) { stack[size++] = vertex; stack[size++] = graph.destination(index[vertex]); last[graph.destination(index[vertex])] = vertex; index[vertex] = graph.nextOutbound(index[vertex]); } } lcaTree = new ReadOnlyIntervalTree(order) { protected long joinValue(long left, long right) { if (left == -1) return right; if (right == -1) return left; if (level[((int) left)] < level[((int) right)]) return left; return right; } protected long neutralValue() { return -1; } }; lcaTree.init(); } public int getLCA(int first, int second) { return (int) lcaTree.query(Math.min(position[first], position[second]), Math.max(position[first], position[second])); } } static class Graph { public static final int REMOVED_BIT = 0; protected int vertexCount; protected int edgeCount; private int[] firstOutbound; private int[] firstInbound; private Edge[] edges; private int[] nextInbound; private int[] nextOutbound; private int[] from; private int[] to; private long[] weight; public long[] capacity; private int[] reverseEdge; private int[] flags; public Graph(int vertexCount) { this(vertexCount, vertexCount); } public Graph(int vertexCount, int edgeCapacity) { this.vertexCount = vertexCount; firstOutbound = new int[vertexCount]; Arrays.fill(firstOutbound, -1); from = new int[edgeCapacity]; to = new int[edgeCapacity]; nextOutbound = new int[edgeCapacity]; flags = new int[edgeCapacity]; } public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { ensureEdgeCapacity(edgeCount + 1); if (firstOutbound[fromID] != -1) nextOutbound[edgeCount] = firstOutbound[fromID]; else nextOutbound[edgeCount] = -1; firstOutbound[fromID] = edgeCount; if (firstInbound != null) { if (firstInbound[toID] != -1) nextInbound[edgeCount] = firstInbound[toID]; else nextInbound[edgeCount] = -1; firstInbound[toID] = edgeCount; } this.from[edgeCount] = fromID; this.to[edgeCount] = toID; if (capacity != 0) { if (this.capacity == null) this.capacity = new long[from.length]; this.capacity[edgeCount] = capacity; } if (weight != 0) { if (this.weight == null) this.weight = new long[from.length]; this.weight[edgeCount] = weight; } if (reverseEdge != -1) { if (this.reverseEdge == null) { this.reverseEdge = new int[from.length]; Arrays.fill(this.reverseEdge, 0, edgeCount, -1); } this.reverseEdge[edgeCount] = reverseEdge; } if (edges != null) edges[edgeCount] = createEdge(edgeCount); return edgeCount++; } protected final GraphEdge createEdge(int id) { return new GraphEdge(id); } public final int addFlowWeightedEdge(int from, int to, long weight, long capacity) { if (capacity == 0) { return addEdge(from, to, weight, 0, -1); } else { int lastEdgeCount = edgeCount; addEdge(to, from, -weight, 0, lastEdgeCount + entriesPerEdge()); return addEdge(from, to, weight, capacity, lastEdgeCount); } } protected int entriesPerEdge() { return 1; } public final int addWeightedEdge(int from, int to, long weight) { return addFlowWeightedEdge(from, to, weight, 0); } public final int addSimpleEdge(int from, int to) { return addWeightedEdge(from, to, 0); } public final int vertexCount() { return vertexCount; } protected final int edgeCapacity() { return from.length; } public final int firstOutbound(int vertex) { int id = firstOutbound[vertex]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int nextOutbound(int id) { id = nextOutbound[id]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int destination(int id) { return to[id]; } protected final void initEdges() { if (edges == null) { edges = new Edge[from.length]; for (int i = 0; i < edgeCount; i++) edges[i] = createEdge(i); } } public final boolean flag(int id, int bit) { return (flags[id] >> bit & 1) != 0; } public final void setFlag(int id, int bit) { flags[id] |= 1 << bit; } public final void removeEdge(int id) { setFlag(id, REMOVED_BIT); } public final boolean isRemoved(int id) { return flag(id, REMOVED_BIT); } public final Iterable<Edge> outbound(final int id) { initEdges(); return new Iterable<Edge>() { public Iterator<Edge> iterator() { return new EdgeIterator(id, firstOutbound, nextOutbound); } }; } protected void ensureEdgeCapacity(int size) { if (from.length < size) { int newSize = Math.max(size, 2 * from.length); if (edges != null) edges = resize(edges, newSize); from = resize(from, newSize); to = resize(to, newSize); nextOutbound = resize(nextOutbound, newSize); if (nextInbound != null) nextInbound = resize(nextInbound, newSize); if (weight != null) weight = resize(weight, newSize); if (capacity != null) capacity = resize(capacity, newSize); if (reverseEdge != null) reverseEdge = resize(reverseEdge, newSize); flags = resize(flags, newSize); } } protected final int[] resize(int[] array, int size) { int[] newArray = new int[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private long[] resize(long[] array, int size) { long[] newArray = new long[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private Edge[] resize(Edge[] array, int size) { Edge[] newArray = new Edge[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } protected class GraphEdge implements Edge { protected int id; protected GraphEdge(int id) { this.id = id; } public int getDestination() { return destination(id); } } public class EdgeIterator implements Iterator<Edge> { private int edgeID; private final int[] next; private int lastID = -1; public EdgeIterator(int id, int[] first, int[] next) { this.next = next; edgeID = nextEdge(first[id]); } private int nextEdge(int id) { while (id != -1 && isRemoved(id)) id = next[id]; return id; } public boolean hasNext() { return edgeID != -1; } public Edge next() { if (edgeID == -1) throw new NoSuchElementException(); lastID = edgeID; edgeID = nextEdge(next[lastID]); return edges[lastID]; } public void remove() { if (lastID == -1) throw new IllegalStateException(); removeEdge(lastID); lastID = -1; } } } static interface IntIterator { public int value(); public void advance(); public boolean isValid(); } static class ArrayUtils { public static void fill(int[][] array, int value) { for (int[] row : array) Arrays.fill(row, value); } } static interface Edge { public int getDestination(); } }
Java
["5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1"]
4 seconds
["1 3\n2 2 3\n0\n3 1 2 4\n1 2"]
NoteGraph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
Java 8
standard input
[ "data structures", "dfs and similar", "trees" ]
ff84bc8e8e01c82e1ecaa2a39a7f8dc6
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105). The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u). Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m). Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
2,200
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
standard output
PASSED
c0b5caa3eca53b51713bd86cd4205097
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
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.HashMap; import java.util.Random; import java.util.StringTokenizer; public class Solution{ public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(), s = fs.nextInt(); ArrayList<Integer>[] adjList = new ArrayList[n]; for(int i=0;i<n;i++) adjList[i] = new ArrayList<Integer>(); for(int i=0;i<n-1;i++) { int u = fs.nextInt() - 1, v = fs.nextInt()-1; adjList[u].add(v); adjList[v].add(u); } int cnt = 0; for(int i=0;i<n;i++) { if(adjList[i].size()==1) cnt++; } out.println(((double)s*2)/cnt); } out.close(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class Pair<F,S>{ F first; S second; Pair(F first, S second){ this.first = first; this.second = second; } } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
Java
["4 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
92c0642a6b2193dea99ed1c229f6ed6b
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { long mod1 = (long) 1e9 + 7; int mod2 = 998244353; ArrayList<ArrayList<Integer>> a=new ArrayList<>(); public void solve() throws Exception { int n=sc.nextInt(); int sum=sc.nextInt(); for(int i=0;i<=n;i++) a.add(new ArrayList<>()); for(int i=0;i<n-1;i++) { int u=sc.nextInt(); int v=sc.nextInt(); a.get(u).add(v); a.get(v).add(u); } int cc=0; for(int i=0;i<=n;i++) { if(a.get(i).size()==1) cc++; } out.println(2.0d*(double)sum/(double)cc); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long ncr(int n, int r, long p) { if (r > n) return 0l; if (r > n - r) r = n - r; long C[] = new long[r + 1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r] % p; } public long power(long x, long y, long p) { long res = 1; // out.println(x+" "+y); x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public int[] readArray(int n) throws Exception { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["4 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
05926db6a808df22a102d1cdfe8181aa
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
256 megabytes
import java.util.*; import java.io.*; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Hello { static int []degre = new int[100005]; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int s = in.nextInt(); int cnt = 0; for (int i = 1;i <= n - 1;++i) { int i1 = in.nextInt(); int i2 = in.nextInt(); degre[i1] += 1; degre[i2] += 1; if (degre[i1] == 1) cnt += 1; if (degre[i2] == 1) cnt += 1; if (degre[i1] == 2) cnt -= 1; if (degre[i2] == 2) cnt -= 1; } double ans = (double)s / cnt; System.out.println(ans * 2); } }
Java
["4 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
57a492ff3a86513a5acf6462d0a5b75d
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); // br = new BufferedReader(new FileReader("input.txt")); // PrintWriter pw = new PrintWriter("output.txt"); int n = nextInt(); double s = nextInt(); // graph = new ArrayList[n]; int[] a = new int[n + 1]; for (int i = 0; i < n - 1; i++) { a[nextInt()]++; a[nextInt()]++; } int cntLeaves = 0; for (int i = 0; i < n + 1; i++) if (a[i] == 1) cntLeaves++; pw.println((s / cntLeaves) * 2); pw.close(); } static long mod = 1000000007; static ArrayList<Integer>[] graph; static boolean[] used; static void dfs(int v) { } static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
f7b7acc346e342e664b18a24cf7dc51d
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class B { static ArrayList<ArrayList<Integer>>adjmat = new ArrayList<>(); public static void main(String[] args)throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); double s = sc.nextDouble(); for(int i=0;i<n;i++){ adjmat.add(new ArrayList<Integer>()); } for(int i=0;i<n-1;i++){ int x = sc.nextInt()-1; int y = sc.nextInt()-1; adjmat.get(x).add(y); adjmat.get(y).add(x); } double res = 0.0; double c = 0; for(int i=0;i<adjmat.size();i++){ if(adjmat.get(i).size()==1){ c++; } } res = s/c * 2.0; System.out.println(res); pw.flush(); pw.close(); } static class Pair implements Comparable<Pair>{ long x; int index; public Pair(long x,int index){ this.x= x; this.index = index; } public int compareTo(Pair p){ if(this.x==p.x)return Integer.compare(this.index,p.index); return Long.compare(this.x,p.x); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
0bcaf89431720b6b3bf4781ebc939528
train_003.jsonl
1545572100
You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { static class Pair implements Comparable<Pair>{ // int a; // int b; // public Pair(){} // public Pair(int x,int y){a=x;b=y;} public int compareTo(Pair p){ return Double.compare(a, p.a); } // long a,b; // public Pair(long x,long y){a=x;b=y;} // public int compareTo(Pair p){ // return Long.compare(a, p.a); // } // double a, b; public Pair(double x, double y) {a=x;b=y;} // @Override // public int hashCode() { // final int prime = 31; // int result = 1; // result = prime * result + a; // result = prime * result + b; // // return result; // } @Override public boolean equals(Object obj) { Pair cur = (Pair)obj; if((a==cur.a && b==cur.b))return true; return false; } } static class TrieNode{ int cnt; TrieNode[]child; public TrieNode() { cnt = 0; child = new TrieNode[10]; } } public static long gcd(long a,long b) { if(a<b) return gcd(b,a); if(b==0) return a; return gcd(b,a%b); } static int lcm(int a,int b) { return a*b / (int)gcd(a,b); } static long mod = 998244353;//(long)(1e9+7); // public static void main(String[] args) throws Exception { new Thread(null, null, "Anshum Gupta", 99999999) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static long pow(long x,long y){ if(y == 0)return 1; if(y==1)return x; long a = pow(x,y/2); a = (a*a)%mod; if(y%2==0){ return a; } return (a*x)%mod; } static long mxx; static int mxN = (int)(2e6+5); static int mxV = (int)(2e5); static long[]fact,inv_fact; static long my_inv(long a) { return pow(a,mod-2); } static long bin(int a,int b) { if(a < b || a<0 || b<0)return 0; return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod; } static ArrayList<ArrayList<Integer>>adj; static boolean[]vis; static void make_facts() { fact=new long[mxN]; inv_fact = new long[mxN]; fact[0]=inv_fact[0]=1L; for(int i=1;i<mxN;i++) { fact[i] = (i*fact[i-1])%mod; inv_fact[i] = my_inv(fact[i]); } } static int lc; static void dfs(int sv) { vis[sv] = true; if(adj.get(sv).size() == 1) { lc++; } for(Integer x : adj.get(sv)) { if(!vis[x]) { dfs(x); } } } // static Pair dfs2(int sv) { // vis[sv] = true; // Pair ans = new Pair(0, 0); // double cur_val = 0; // if(adj.get(sv).size()==1) { // ans.a += val; // cur_val += val; // } // PriorityQueue<Double> pq = new PriorityQueue<Double>(); // for(Integer x : adj.get(sv)) { // if(!vis[x]) { // Pair cur = dfs2(x); // ans.a = Math.max(ans.a, cur.a + cur_val); // } // } // } static double val; public static void solve() throws Exception { // solve the problem here MyScanner s = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int tc = 1;//s.nextInt(); mxx = (long)(1e18+5); // make_facts(); // int mod = (int)1e9+7; while(tc-->0){ int n = s.nextInt(); double sum = s.nextDouble(); adj = new ArrayList<ArrayList<Integer>>(); for(int i=0;i<=n;i++)adj.add(new ArrayList<Integer>()); for(int i=0;i<n-1;i++) { int a = s.nextInt(); int b = s.nextInt(); adj.get(a).add(b); adj.get(b).add(a); } vis = new boolean[n+1]; lc = 0; dfs(1); val = sum / (lc*1D); out.println(val * 2); } out.flush(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } 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 3\n1 2\n1 3\n1 4", "6 1\n2 1\n2 3\n2 5\n5 4\n5 6", "5 5\n1 2\n2 3\n3 4\n3 5"]
1 second
["2.000000000000000000", "0.500000000000000000", "3.333333333333333333"]
NoteIn the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:
Java 11
standard input
[ "constructive algorithms", "implementation", "trees" ]
7bdd8f0cf42855bebda4ccc56d8fe788
The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.
1,700
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.
standard output
PASSED
47dc8121c556c9496fd56b1676d3e344
train_003.jsonl
1493391900
You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.
256 megabytes
import java.io.*; import java.util.*; public class Main { private void solve()throws Exception { int n=nextInt(); int a[]=new int[n+1]; int left[]=new int[n+1]; int right[]=new int[n+1]; for(int i=1,curr=-1<<30;i<=n;i++) { a[i]=nextInt(); if(a[i]==0) curr=i; left[i]=i-curr; } for(int i=n,curr=1<<30;i>=1;i--) { if(a[i]==0) curr=i; right[i]=curr-i; } for(int i=1;i<=n;i++) out.print(Math.min(left[i],right[i])+" "); } /////////////////////////////////////////////////////////// public void run()throws Exception { br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String args[])throws Exception{ new Main().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws Exception{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws Exception{ return br.readLine(); } int nextInt()throws Exception{ return Integer.parseInt(nextToken()); } long nextLong()throws Exception{ return Long.parseLong(nextToken()); } double nextDouble()throws Exception{ return Double.parseDouble(nextToken()); } }
Java
["9\n2 1 0 3 0 0 3 2 4", "5\n0 1 2 3 4", "7\n5 6 0 1 -2 3 4"]
2 seconds
["2 1 0 1 0 0 1 2 3", "0 1 2 3 4", "2 1 0 1 2 3 4"]
null
Java 11
standard input
[ "constructive algorithms" ]
2031f2ac5652609fda77830e93ffcc2c
The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109).
1,200
Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.
standard output
PASSED
5c0b6976f26c87a11edd31476374a16b
train_003.jsonl
1493391900
You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.
256 megabytes
//I AM THE CREED /* //I AM THE CREED /* package codechef; // don't place package name! */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.awt.Point; public class Main{ public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); while(input.hasNext()){ int n=input.nextInt(); int[] l=new int[n]; Queue<Integer> q=new LinkedList<>(); int[] dist=new int[n]; for(int i=0;i<n;i++){ l[i]=input.nextInt(); if(l[i]==0){ q.add(i); } } Arrays.fill(dist, Integer.MAX_VALUE); int level=0; while(!q.isEmpty()){ int size=q.size(); while(size!=0){ int ind=q.poll(); dist[ind]=Math.min(dist[ind], level); if(ind+1<n && dist[ind+1]==Integer.MAX_VALUE){ q.add(ind+1); } if(ind-1>=0 && dist[ind-1]==Integer.MAX_VALUE){ q.add(ind-1); } size--; } level++; } StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++){ sb.append(dist[i]+" "); } System.out.println(sb.toString()); } } }
Java
["9\n2 1 0 3 0 0 3 2 4", "5\n0 1 2 3 4", "7\n5 6 0 1 -2 3 4"]
2 seconds
["2 1 0 1 0 0 1 2 3", "0 1 2 3 4", "2 1 0 1 2 3 4"]
null
Java 11
standard input
[ "constructive algorithms" ]
2031f2ac5652609fda77830e93ffcc2c
The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109).
1,200
Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.
standard output
PASSED
33627db34d49cab486f86349095f9467
train_003.jsonl
1493391900
You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; 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) throws IOException { FastReader ip = new FastReader(); OutputStream output = System.out; PrintWriter out = new PrintWriter(output); int n=ip.nextInt(); int[] arr =new int[n]; for(int i=0;i<n;i++){ arr[i]=ip.nextInt(); } int ans[]=new int[n]; int v=Integer.MAX_VALUE; for(int i=0;i<n;i++){ if(arr[i]==0) v=0; ans[i]=v; if(v!=Integer.MAX_VALUE) v++; } v=Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ if(arr[i]==0) v=0; ans[i]=Math.min(ans[i],v); if(v!=Integer.MAX_VALUE) v++; } for(int i=0;i<n;i++){ out.print(ans[i]+" "); } out.close(); } }
Java
["9\n2 1 0 3 0 0 3 2 4", "5\n0 1 2 3 4", "7\n5 6 0 1 -2 3 4"]
2 seconds
["2 1 0 1 0 0 1 2 3", "0 1 2 3 4", "2 1 0 1 2 3 4"]
null
Java 11
standard input
[ "constructive algorithms" ]
2031f2ac5652609fda77830e93ffcc2c
The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109).
1,200
Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.
standard output
PASSED
12e50373515d17e8f9a820f776d213ee
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.*; public class sol { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i=0; i<t; i++) { int x = input.nextInt(); int y = input.nextInt(); int n = input.nextInt(); int a = n - n%x + y; int b = n - n%x + y - x; if(a>n) System.out.println(b); else System.out.println(a); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
b837c5b8717c3a984ad8bdc5a9840a1d
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
//package cfr653; 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){ int x = sc.nextInt(); int y = sc.nextInt(); int n = sc.nextInt(); int k=n%x-y; if(k<0) k=x+k; System.out.println(n-k); } }}
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
1e75d5a60fb8edd5552a6eec24aef55c
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class Main { public static Scanner in = new Scanner(System.in); public static void main(String[] args) { // write your code here int x, y, n, t; t = in.nextInt(); //Test cases while (t-- > 0) { x = in.nextInt(); y = in.nextInt(); n = in.nextInt(); long z = (n - y) / x * x + y; System.out.println(z); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
740a332c5d7caa4c34544e5ca4e3e41c
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class Modulo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while((t--)>0) { int x=sc.nextInt(); int y=sc.nextInt(); int n=sc.nextInt(); if((n%x)>y) System.out.println(n-(n%x-y)); else if((n%x)==y) System.out.println(n); else System.out.println((n-x)+(y-n%x)); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
538f5c3e4ae345923f8ea18f3d7793a6
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t>0) { int x = s.nextInt(); int y = s.nextInt(); int n = s.nextInt(); int l = 0, r = n / x; while(l < r) { int mid = (l + r + 1) / 2; if(x * mid + y <= n) l = mid; else r = mid - 1; } System.out.println(x * l + y); t--; } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
ed226a7d7a95e693f71c3b6b9b24b9c5
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class RequiredRemainder { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int x=sc.nextInt(); int y=sc.nextInt(); int n=sc.nextInt(); int a=n%x; if(a-y>=0){ n=n-(a-y); } else{ n=n-a-x+y; } System.out.println(n); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
572f12a2c59b2200fc0ff57d1e2e2d22
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class requiredRemainder{ public static void main(String [] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t!=0) { int x=sc.nextInt(); int y=sc.nextInt(); int n=sc.nextInt(); int q=n/x; int r=n%x; if(r>=y) { System.out.println(q*x+y); } else { System.out.println(x*(q-1)+y); } t--; } sc.close(); } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
594326e0f04de43d126403cdeb82c376
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner reader = new Scanner(System.in); // Scanner reader = new Scanner(new File("a.in")); // System.out.println("test"); int numCases = Integer.valueOf(reader.nextLine()); for(int i = 0; i < numCases; i++) { String line = reader.nextLine(); int x = Integer.valueOf(line.split(" ")[0]); int y = Integer.valueOf(line.split(" ")[1]); int n = Integer.valueOf(line.split(" ")[2]); int initVal = ( n - n % x + y ); if(initVal > n) { initVal -= x; } for(int k = initVal; k >= 0; k -= x) { if( k % x == y) { System.out.println(k); break; } } } // try { // FileWriter writer = new FileWriter("barn1.out"); // writer.write(String.valueOf(covered)); // writer.write("\n"); // writer.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
10235459ca09ce07fdfd4a970c7cf5f8
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
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 Main { static final long mod = (int)1e9+7; static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader sc=new Reader(); int t = sc.nextInt(); while (t-- > 0) { int x = sc.nextInt(); int y = sc.nextInt(); int n = sc.nextInt(); int ans = n%x; if(ans-y >= 0) { n = n-(ans-y); } else { n = n-ans-x+y; } System.out.println(n); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
d7880bd89ef32b47cf0ccdc5326f6d23
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
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){ int x =sc.nextInt(); int y =sc.nextInt(); int n =sc.nextInt(); int k=n%x; if(k==y) System.out.println(n); else if(k>y){ if(n-(k-y)<=n) System.out.println(n-(k-y)); else{ System.out.println(0); } }else{ n=n-k-(x-y); if(n>0){ System.out.println(n); } else{ System.out.println(0); } } } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
f63306a5888f43923e830b5fa643dbf9
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int test=in.nextInt(); for (int i = 0; i < test; i++) { int x=in.nextInt(); int y=in.nextInt(); int n=in.nextInt(); int ans=n-y; int ans1=ans/x; int bal=ans1*x+y; System.out.println(bal); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
ae4c1bb33c0afe3468a06638f2557032
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; /** * * @author Dell */ public class ARequired { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int x=sc.nextInt(); int y=sc.nextInt(); int n=sc.nextInt(); int rem=n%x; if(rem>=y) System.out.println(n-rem+y); else System.out.println(n-rem-x+y); // System.out.println(n); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
8ad5db50efb8c8ad353762bf8e2dee71
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); for(int i = 0; i < num; i++) { int x = input.nextInt(); int y = input.nextInt(); int k = input.nextInt(); int count = 0; int div = k / x; int result = 0; if(div * x + y <= k) result = div * x + y; else result = (div - 1) * x + y; System.out.println(result); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
fd5198e6472c39e47857868b85ea048f
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); for(int i = 0; i < num; i++) { int x = input.nextInt(); int y = input.nextInt(); int k = input.nextInt(); int count = 0; int div = k / x; int result = 0; if(div * x + y <= k) //원래값에 몫을 나누고 나머지를 더한경우가 원래값보다 작거나 작을때가 내가 원하는값 result = div * x + y; else // 더 클때는 몫에 1을 빼주면 됨. result = (div - 1) * x + y; System.out.println(result); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
897d051923374e1578d86a3a43c78671
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
/*input 1 2 0 2 */ import java.math.*; import java.io.*; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static Reader sc=new Reader(); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String args[])throws IOException { /* * For integer input: int n=inputInt(); * For long input: long n=inputLong(); * For double input: double n=inputDouble(); * For String input: String s=inputString(); * Logic goes here * For printing without space: print(a+""); where a is a variable of any datatype * For printing with space: printSp(a+""); where a is a variable of any datatype * For printing with new line: println(a+""); where a is a variable of any datatype Scanner in = new Scanner(System.in); //all four int[] dr = { 1, 0, -1, 0 }; int[] dc = { 0, 1, 0, -1 }; */ int t=inputInt(); while(t-->0) { int x=inputInt(),y=inputInt(),n=inputInt(); if(n/x==0) { if(y<=n) println(y+""); else println(0+""); } else if(n%x==0) { if(y==0) println(n+""); else println((n-x+y)+""); } else { int q=n/x; if(q*x+y<=n) { println((q*x+y)+""); } else { q--; println((q*x+y)+""); } } } bw.flush(); bw.close(); } public static int modulo(int x,int N) { return (x % N + N) %N; } public static long lcm(long a,long b) { return a / gcd(a, b) * b; } public static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } public static int inputInt()throws IOException { return sc.nextInt(); } public static long inputLong()throws IOException { return sc.nextLong(); } public static double inputDouble()throws IOException { return sc.nextDouble(); } public static String inputString()throws IOException { return sc.readLine(); } public static void print(String a)throws IOException { bw.write(a); } public static void printSp(String a)throws IOException { bw.write(a+" "); } public static void println(String a)throws IOException { bw.write(a+"\n"); } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
18fd01d5902e7cd48b53cf8a78d4f254
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
//package com.company; //import java.util.ArrayList; //import java.util.LinkedList; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) { // ArrayList<Integer> list = new ArrayList<>(); // LinkedList<Integer> linkedList = new LinkedList<>(); // StringBuilder stringBuilder = new StringBuilder(); Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i = 0 ; i<t;i++){ int x = scanner.nextInt(); int y = scanner.nextInt(); int n = scanner.nextInt(); // k1*(i++) + k2 = value if (n - n % x + y <= n) { System.out.println(n - n % x + y); } else { System.out.println(n - n % x - (x - y)); } } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
ae6abba954edacfab778b9ea6201c96a
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.*; public class requireRemainder{ public static void main(String [] args){ Scanner sc = new Scanner (System.in); int mount = sc.nextInt(); for(int i = 0 ; i < mount; i ++){ int x = sc.nextInt(); int y = sc.nextInt(); int n = sc.nextInt(); int sum = n % x ; if(sum>y){ sum = sum-y; System.out.println(n-sum); } else if(sum<y){ sum = sum+(x-y); System.out.println(n-sum); } else{ System.out.println(n); } } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
80ad3126d3e5ba596d5363b34d6d6185
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class RequiredRemainder { 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 == ' ') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { try { Reader scan=new Reader(); int pos=0; int t=scan.nextInt(); for(int i=0;i<t;i++) { int[] arr=new int[3]; arr[0]= scan.nextInt(); arr[1]=scan.nextInt(); arr[2]=scan.nextInt(); int temp=arr[2]; int rem=arr[2]%arr[0]; arr[2]=arr[2]-rem; if(arr[2]+arr[1]>temp) { arr[2]=arr[2]-(arr[0]-arr[1]); } else { arr[2]=arr[2]+arr[1]; } System.out.println(arr[2]); } } catch (Exception e) { return; } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
6ec6753adc90d179121e96ec8fe3811b
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int x=s.nextInt(); int y=s.nextInt(); int n=s.nextInt(); if(n%x==y) System.out.println(n); else {if(n%x > y) System.out.println(n-(n%x-y)); else System.out.println(n-x+(y-n%x));} } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
0758a6f4b3ae2ee483da594efa815a24
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.*; import java.io.*; public class Remainder { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int x = sc.nextInt(); int y = sc.nextInt(); int n = sc.nextInt(); if (x > n && y == 0) System.out.println("0"); else { int r = n % x; if(y==r) System.out.println(n); else { int c = n - r; int b=0; if((c+y)<n) b=c+y; else { n=n-r; int a = Math.abs(x - y); b = n - a; } System.out.println(b); } } } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
4c46a40eebc573dac85bf5dc200a9b59
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { int p=sc.nextInt(); while(p-->0) { long a,b,c,e,ff; a=sc.nextLong();b=sc.nextLong();c=sc.nextLong(); long g=c%a; if (g>=b) { System.out.println(c-(g-b)); }else { System.out.println(c-g-(a-b)); } } } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
85ad0a8f77a2fc99add6a61ecbc961f0
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); while (test-- != 0) { int x = scn.nextInt(), y = scn.nextInt(), n = scn.nextInt(); int k = n / x; k = k * x + y; if (k > n) k -= x; System.out.printf("%d\n", k); } scn.close(); } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
c04d4d3e7510dff370e4eba8f7b233a5
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.util.Scanner; public class RequiredRemainder { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); int[] ans = new int[t]; int h = 0; for(int i = 0 ; i < t ; i++){ int x = input.nextInt(); int y = input.nextInt(); int n = input.nextInt(); for(int j = n/x ; j >= 0 ; j--){ if(((j*x)+y) <= n){ ans[h] = (j*x)+y; h++; break; } } } for(int i = 0 ; i < ans.length ; i++){ System.out.println(ans[i]); } input.close(); } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
5065ef143286cb1148c140a9a4765b40
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++){ String[] integersInString = br.readLine().split(" "); int x = Integer.parseInt(integersInString[0]); int y = Integer.parseInt(integersInString[1]); int n = Integer.parseInt(integersInString[2]); if (n%x>=y) System.out.println(n-((n%x)-y)); else System.out.println(n-((n%x)+(x-y))); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
ea528220f953f65438b9becac79435d9
train_003.jsonl
1593354900
You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ long x=sc.nextInt(); long y=sc.nextInt(); long n=sc.nextInt(); long ans; if(y<=n%x){ ans=(n-(n%x))+y; } else{ ans=(n-(n%x))-x+y; } System.out.println(ans); } } }
Java
["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"]
1 second
["12339\n0\n15\n54306\n999999995\n185\n999999998"]
NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.
Java 11
standard input
[ "math" ]
2589e832f22089fac9ccd3456c0abcec
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y &lt; x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.
800
For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.
standard output
PASSED
f36912df1c71b8383702a0591c078dd2
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class S { public static void main (String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=0;i<t;i++) { int n=in.nextInt(); if(n<10) { System.out.println(n); } else if(n==10) { System.out.println(9); } else { String s="11"; int count=0,j=1; int z=Integer.valueOf(s); int x=z; while(x<=n) { count++; j++; if(j==10) { s=s+"1"; z=Integer.valueOf(s); j=1; } x=z*j; } System.out.println(count+9); } } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
3664158655e48d35cb5bec1615846673
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.*; import java.math.*; public class Main{ public static void main(String [] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); int i; for(i=0;i<t;i++) { int n=scan.nextInt(); int ans=0; if(n <10) ans=n; else{ String s=Integer.toString(n); ans=9 * (s.length()-1); String d=""; char c=s.charAt(0); int j; for(j=0;j<s.length();j++) { d=""+d+c; } int check=Integer.parseInt(d); int num = Integer.parseInt(String.valueOf(c)); if(n< check) ans=ans+num-1; else ans=ans+num; } System.out.println(ans); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
a08ff698fbbb8f59b653ee52f186f081
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class c1 { public static MyScanner scan; public static PrintWriter out; public static void main(String[] args) { scan=new MyScanner(); out=new PrintWriter(new BufferedOutputStream(System.out)); // int t=1; int t=scan.nextInt(); while(t-->0) { int n=scan.nextInt(); int cnt=0,cur=1; while(true) { boolean change=false; int num=1; while(cur*num<=n) { num++; cnt++; change=true; if(num==10) break; } cur*=10; cur++; if(!change) break; } out.println(cnt); } out.close(); } //-----------------------------------------------------COMP-SPACE----------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------- //node static class Node implements Comparable<Node> { public int x,y; public Node(int x,int y) { this.x=x; this.y=y; } public int compareTo(Node other) { if(this.x==other.x) return 0; else if(this.x>other.x) return 1; else return -1; } public boolean equals(Node other) { return this.x==other.x; } // public int compareTo(Node other) { // if(this.y==other.y) return 0; // else if(this.y>other.y) return 1; // else return -1; // } // // public boolean equals(Node other) { // return this.y==other.y; // } } //edge static class Edge implements Comparable<Edge> { public Node a,b; public int weight; public Edge(Node a,Node b,int weight) { this.a=a; this.b=b; this.weight=weight; } public int compareTo(Edge other) { if(this.weight==other.weight) return 0; else if(this.weight>other.weight) return 1; else return -1; } } //util public static int gcd(int a, int b) { if (b == 0) return a; if (a == 0) return b; return (a > b) ? gcd(a % b, b) : gcd(a, b % a); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static void sort(int[] a) { ArrayList<Integer> list=new ArrayList<>(); for (int i:a) list.add(i); Collections.sort(list); for (int i=0; i<a.length; i++) a[i]=list.get(i); } public static int min(int a,int b) { if(a<=b) return a; else return b; } public static int max(int a,int b) { if(a>=b) return a; else return b; } public static int abs(int a) { if(a<0) return a*-1; else return a; } public static ArrayList<Integer> getPrimes(int n) { boolean prime[]=new boolean[n+1]; Arrays.fill(prime,true); for (int p=2;p*p<=n;p++) { if (prime[p]) { for (int i=p*2;i<=n;i+=p) { prime[i]=false; } } } ArrayList<Integer> primeNumbers=new ArrayList<Integer>(); for (int i = 2; i <= n; i++) { if (prime[i]) { primeNumbers.add(i); } } return primeNumbers; } public static boolean isPrime(int a) { if (a == 0 || a == 1) { return false; } if (a == 2) { return true; } for (int i = 2; i < Math.sqrt(a) + 1; i++) { if (a % i == 0) { return false; } } return true; } public static ArrayList<Integer> getDivisors(int n) { ArrayList<Integer> div = new ArrayList<Integer>(); for (int i=1;i*i<=n;i++) { if (n%i==0) { div.add(i); if (n/i!=i) div.add(n/i); } } return div; } //scanner public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int[] nextIntArray(int n) { int[] a=new int[n]; for(int c=0;c<n;c++) a[c]=scan.nextInt(); return a; } public static Integer[] nextIntegerArray(int n) { Integer[] a=new Integer[n]; for(int c=0;c<n;c++) a[c]=scan.nextInt(); return a; } public static int[][] nextIntMatrix(int n,int m) { int[][] a=new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=scan.nextInt(); return a; } public static Integer[][] nextIntegerMatrix(int n,int m) { Integer[][] a=new Integer[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=scan.nextInt(); return a; } //print public static void printIntArray(int[] a) { for(int c:a) out.print(c+" "); out.println(); } public static void printIntegerArray(Integer[] a) { for(int c:a) out.print(c+" "); out.println(); } public static void printIntMatrix(int[][] a) { for(int[] i:a) { for(int j:i) out.print(j+" "); out.println(); } } public static void printIntegerMatrix(Integer[][] a) { for(Integer[] i:a) { for(Integer j:i) out.print(j+" "); out.println(); } } public static void help(int t,int n) { out.println("Test Case: "+(t+1)+" | Variable Output: "+n); } public static void help(int t,String n) { out.println("Test Case: "+(t+1)+" | Variable Output: "+n); } public static void help(int t,char[] n) { out.print("Test Case: "+(t+1)+" | Variable Output: "); for(char c:n) out.print(c+" "); out.println(); } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
8e15a1b858109e0fd2fda00666dfcf31
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import java.util.*; import java.util.logging.SimpleFormatter; public class solution{ static int counter(int n){ int count = 0 ; int temp = 0 ; for (int i=1 ; i<=9 ; i++){ temp = temp*10 + 1 ; for (int j=1 ; j<=9 ; j++){ if (temp*j <= n) count++ ; else break; } } return count ; } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt() ; while (t-- > 0){ int n = sc.nextInt() ; System.out.println(counter(n)); } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
cdc8ad3085f81d585f19eccc7c9cf361
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; /** * * @author Administrator */ public class A1277 { /** * @param args the command line arguments */ public static boolean lucky(int num) { int numLen = String.valueOf(num).length(); int ones=0; for (int k = 0; k < numLen; k++) { ones += 1*(Math.pow(10, k)); } return (num%ones == 0); } public static int numGenerator(int num, int n) { if (num ==0) { num = 9; n--; } int result = 0; for (int i = 0; i < n; i++) { result += num*(Math.pow(10, i)); } return result; } public static int numLength(int num) { return String.valueOf(num).length(); } public static int downlLevel(int num) { return numGenerator(9, numLength(num)-1); } public static void main(String[] args) { // TODO code application logic here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); //number of tests int tempResult=0; int tempN; //test ArrayList<Integer> Result = new ArrayList<Integer>(); for (int i = 0; i < t; i++) { //n.add(sc.nextInt()); tempN = sc.nextInt(); tempResult = 0; if (tempN < 10) { tempResult = tempN; }else { // System.out.println("int is "+ String.valueOf(tempN).charAt(0)); //char temp2 = String.valueOf(tempN).charAt(0); // String temp3 = String.valueOf(temp2); int indexZero = Integer.valueOf(String.valueOf(String.valueOf(tempN).charAt(0))); // int indexZero = Integer.valueOf(String.valueOf(tempN).charAt(0)); // System.out.println("indexZero " +indexZero); //**********************MAIN WORK************************************************ if (!lucky(tempN)) { boolean lessThanOrEqAllRight = true; int temp ; //************ CHECK IF num at index 0 <= All Right************************** for (int j = 1; j < String.valueOf(tempN).length(); j++) { temp = Integer.valueOf(String.valueOf(String.valueOf(tempN).charAt(j))); if(indexZero < temp) { break; } if(indexZero > temp) { lessThanOrEqAllRight = false; break; } } // System.out.println("before adjust = "+ tempN); // System.out.println("TEST: "+ lessThanOrEqAllRight); if (lessThanOrEqAllRight) tempN = numGenerator (indexZero, numLength(tempN)); else tempN = numGenerator (indexZero-1, numLength(tempN)); indexZero = Integer.valueOf(String.valueOf(String.valueOf(tempN).charAt(0))); // System.out.println("WAS not lucky but now = "+ tempN); } //************* after this point tempN must be lucky **************************** tempResult += indexZero; while(tempN > 10){ tempN = downlLevel(tempN); tempResult += 9; } // tempResult += 9; } Result.add(tempResult); } // PRINTING RESULTS!! for (int i = 0; i < t; i++) { System.out.println(Result.get(i)); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
b1fc5c81fc00420780846840c83d40e9
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import static java.lang.Math.pow; import java.util.Scanner; public class Codeforse { public static void main(String[] args) { Scanner in = new Scanner(System.in); long num = in.nextLong(); while (num != 0) { long n = in.nextLong(); long c = 0; long v = n; if (n < 10) { System.out.println(n); } else if (n <= 100) { System.out.println(n / 11 + 9); } else { long y = 0; while (n > 0) { c++; if (n / 10 == 0) { y = n % 10; } n /= 10; } c--; long b = 0, k = 0; while (k != c ) { b = b * 10 + 1; k++; } long h = (long) (y * (pow(10, c ) + b)); y--; if (h <= v) { y++; } System.out.println(9 * c + y); } num--; } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
0d0c24f55de70a95bf318649d7f00844
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static int digit(long n) { int c=0; while(n!=0) { n=n/10; c++; } return c; } public static void main(String[] zima) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { long n=sc.nextLong(); if(n<10) { System.out.println(n); } // long te=n; else { int d=digit(n); int ans=(d-1)*9; long a=(long)Math.pow(10,d-1); int b=(int)(n/a); long rem=n%a; long no=0; ans+=b-1; for(int j=0;j<d-1;j++) { no=no*10+b; } if(rem>=no) ans+=1; System.out.println(ans); } } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
70ce3bc34905d5a3b02ebbbe8c9143ec
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; /* Name of the class has to be "Main" only if the class is public. */ public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main (String[] args) throws java.lang.Exception { // your code goes here Reader s = new Reader(); PrintWriter p = new PrintWriter(System.out); int t = s.nextInt(); for(int i=0;i<t;i++){ long n = s.nextLong(); if(n<10){ p.println(n); continue; } long cur = 10; int count = 9; while(cur<=n){ cur*=10; count+=9; } count -=9; cur/=10; count += n/cur; long dig = 0; long d = n/cur; while(cur!=0){ dig += d*cur; cur/=10; } if(n<dig){ count--; } p.println(count); } p.close(); } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
14baa64eee0c7caef5164576c84ffa33
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.*; public class rc1{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t--!=0){ long n = sc.nextLong(),c=0,a=0; for(int i=1; i<=9; i++){ a = a*10+1; for(int j=1; j<=9; j++){ if(j*a<=n) c++; } } System.out.println(c); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
f4e7c49890602d848efe9efb0ae65b84
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.Scanner; public class Main { // final static int N = (int) (1e5 + 10); // static char[] a=new char[N]; // static String s; static int sum; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int n=sc.nextInt(); long a=(n+"").length(); long sum=(a-1)*9; for(int i=1;i<=9;i++) { int b=(int)Math.pow(10,a-1); long c=0; for(int j=1;j<=a;j++) { c+=i*b; b=b/10; } if(n>=c) sum++; else break; } System.out.println(sum); } sc.close(); } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
472ccdac21a574359bcb88de4c514920
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String[] a = new String[t]; for (int i = 0; i < t; i++) { a[i] = sc.next(); } int s, n; long v; for (int i = 0; i < t; i++) { n = a[i].length(); s = (n - 1) * 9; v = Long.parseLong(a[i]); s += 9 * v / (Math.pow(10, n) - 1); System.out.println(s); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
c2c15eefe374d6e4ec31cb3ab9294d1b
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.*; public class hello{ public static void main(String[] args){ Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0){ long n=s.nextInt(); int count=0; for(long i=1;i<=9;i++){ if(n>=i){ long temp=i; while(temp<=n){ //System.out.println(temp); count++; temp=temp*10+i; } } } System.out.println(count); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
7113526b88ba4d6cc27e817be4b1af0c
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Solution implements Runnable { static class InputReader { private InputStream stream;private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream){this.stream = stream;} public int read(){if (numChars==-1) throw new InputMismatchException();if (curChar >= numChars){curChar = 0;try{numChars = stream.read(buf);}catch (IOException e){throw new InputMismatchException(); }if(numChars <= 0)return -1;}return buf[curChar++];} public String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;} public int nextInt(){int c = read(); while(isSpaceChar(c))c = read(); int sgn = 1; if (c == '-'){sgn = -1;c = read(); }int res = 0;do{if(c<'0'||c>'9') throw new InputMismatchException();res *= 10;res += c - '0';c = read();}while (!isSpaceChar(c));return res * sgn;} public long nextLong(){int c = read();while (isSpaceChar(c))c = read();int sgn = 1;if (c == '-'){sgn = -1;c = read();}long res = 0;do{if (c < '0' || c > '9')throw new InputMismatchException();res *= 10;res += c - '0';c = read();}while (!isSpaceChar(c));return res * sgn;} public double nextDouble(){int c = read();while (isSpaceChar(c))c = read();int sgn = 1;if (c == '-'){sgn = -1;c = read();}double res = 0;while (!isSpaceChar(c) && c != '.'){if (c == 'e' || c == 'E')return res * Math.pow(10, nextInt());if (c < '0' || c > '9') throw new InputMismatchException();res *= 10;res += c - '0';c = read();}if (c == '.'){c = read();double m = 1;while (!isSpaceChar(c)){if (c == 'e' || c == 'E')return res * Math.pow(10, nextInt());if (c < '0' || c > '9')throw new InputMismatchException();m /= 10;res += (c - '0') * m;c = read();}}return res * sgn;} public String readString(){int c = read();while (isSpaceChar(c))c = read();StringBuilder res = new StringBuilder();do{res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public boolean isSpaceChar(int c){if (filter != null)return filter.isSpaceChar(c);return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;} public String next(){return readString();} public interface SpaceCharFilter{public boolean isSpaceChar(int ch);} } public static void main(String args[]) throws Exception{new Thread(null, new Solution(),"Main",1<<27).start();} public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a);} public static long findGCD(long arr[], int n) { long result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result);return result; } static void sortbycolomn(long arr[][], int col) { Arrays.sort(arr, new Comparator<long[]>() { @Override public int compare(final long[] entry1,final long[] entry2) { if (entry1[col] > entry2[col])return 1; else return -1; } }); } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t=in.nextInt(); while(t--!=0){ int n=in.nextInt(); int nn=n; int x=0; long k=1; while(nn>0){ nn=nn/10; x++; k=(long)(k*10); } k=(long)(k/10); int ans=9*(x-1); ans=ans+n/(int)k; k=n/(int)k; String s=Integer.toString(n); for(int i=0;i<s.length();i++){ if((s.charAt(i)-48)>k) break; else if((s.charAt(i)-48)<k){ ans--; break; } } w.println(ans); } w.flush(); w.close(); } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
d6e055456e5cde5570f38c2b4d17809d
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.io.*; import java.lang.*; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; import java.math.BigInteger; public class CodeforcesDiv2_606 { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int q = in.nextInt(); while(q-->0){ String s = in.next(); int n = Integer.parseInt(s); int count = 0; int length = 1; String temp = "1"; while(length<s.length()){ count+=9; ++length; temp = temp + "1"; } int temp2 = Integer.parseInt(temp); int index = temp2; while(temp2<=n){ ++count; temp2+=index; } out.println(count); } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = reader.readLine(); } catch (Exception e) { } if (next == null) { return false; } tokenizer = new StringTokenizer(next); return true; } public BigInteger nextBigInteger() { return new BigInteger(next()); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
946c61b1097f4d7bbd3cd32f95f32f05
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.Scanner; public class HAPPY_BIRTHDAY_POLYCARP { public static void main(String[] args) { Scanner nik = new Scanner(System.in); int t = nik.nextInt(); while (t-- > 0) { long n = nik.nextLong(); long d = (long) Math.log10(n); if (n < 10) { System.out.println(n); } else { long res = 11; long p = 1; for (int i = 0; i < d - 1; i++) { long res1=res*10; res =res1+1; } d *= 9; long temp = res; // System.out.println(temp+" "+d); while (res <= n) { d++; res += temp; } System.out.println(d); } } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output
PASSED
27264fba8113af73a46d6a287538a5ed
train_003.jsonl
1576321500
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.
256 megabytes
import java.util.*; import java.io.*; import java.text.*; public class A1277 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); ArrayList<Long> a = new ArrayList<Long>(); for (int i = 1; i <= 9; i++) { String s = i + ""; for (int j = 0; j < 9; j++) { a.add(Long.parseLong(s)); s += i; } } while (t-- > 0) { int n = sc.nextInt(); int c = 0; for (long x : a) if (x <= n) c++; pw.println(c); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n18\n1\n9\n100500\n33\n1000000000"]
1 second
["10\n1\n9\n45\n12\n81"]
NoteIn the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
Java 8
standard input
[ "implementation" ]
99a5b21a5984d5248e82f843f82e9420
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.
1,000
Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.
standard output