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
003f37dcf1fbc84b2f9e30680f3177bf
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
// practice with kaiboy import java.io.*; import java.util.*; public class CF1486C2 extends PrintWriter { CF1486C2() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1486C2 o = new CF1486C2(); o.main(); o.flush(); } int query(int l, int r) { println("? " + l + " " + r); return sc.nextInt(); } void main() { int n = sc.nextInt(); int m = query(1, n); int ans; if (m < n && query(m, n) == m) { int lower = m, upper = n; while (upper - lower > 1) { int i = (lower + upper) / 2; if (query(m, i) == m) upper = i; else lower = i; } ans = upper; } else { int lower = 1, upper = m; while (upper - lower > 1) { int i = (lower + upper) / 2; if (query(i, m) == m) lower = i; else upper = i; } ans = lower; } println("! " + ans); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
436cf6667588af895779e5d40999d459
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.util.concurrent.CompletableFuture; import javax.swing.text.Segment; import java.io.*; import java.math.*; import java.sql.Array; public class Main { static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static void rvrs(int[] arr) { int i =0 , j = arr.length-1; while(i>=j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static void rvrs(long[] arr) { int i =0 , j = arr.length-1; while(i>=j) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long mod_mul(long a , long b ,long mod) { return ((a%mod)*(b%mod))%mod; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m){ if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; private long[] z1; private long[] z2; public Combinations(long N , long mod) { z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return invrsFac((int)n); } long ncr(long N, long R, long mod) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return Math.min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ /** static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = Math.min(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(1e17); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return Math.min(left, right); } public void update(int index , int val) { arr[index] = val; for(long e:arr) System.out.print(e+" "); update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } */ /* ***************************************************************************************************************************************************/ static Reader sc = new Reader(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = 1; // tc = sc.nextInt(); for(int i = 1 ; i<=tc ; i++) { // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.println(sb); // System.out.println(); } static void TEST_CASE() { int l = 1, r = sc.nextInt(), mid, x; int second_max = query(l, r); while (l < r) { mid = (l + r) / 2; if (second_max <= mid) { x = query(Math.min(l, second_max), mid); if (x == second_max) r = mid; else l = mid + 1; } else { x = query(mid + 1, Math.max(second_max, r)); if (x == second_max) l = mid + 1; else r = mid; } } System.out.printf("! %d\n", l); } static int query(int a, int b) { if(a>=b) return 0; System.out.println("? "+a+" "+b); System.out.flush(); return sc.nextInt(); } } /*******************************************************************************************************************************************************/ /** */
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
2f9af0a844afeed42060dc32a698b7e1
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.PrintWriter; public class guess { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int sec = ask(in, out, 1, n); if(sec == n || ask(in, out, sec, n) != sec) { // greatest is on the left int low = 1, high = sec - 1; int ans = -1; while(low <= high) { int mid = (low + high) / 2; if(ask(in, out, mid, sec) == sec) { ans = mid; low = mid+1; } else { high = mid-1; } } out.printf("! %d\n", ans); } else { // greatest is on the right int low = sec+1, high = n; int ans = -1; while(low <= high) { int mid = (low + high) / 2; if(ask(in, out, sec, mid) == sec) { ans = mid; high = mid-1; } else { low = mid+1; } } out.printf("! %d\n", ans); } out.flush(); } static int ask(FastScanner in, PrintWriter out, int l, int r) { out.printf("? %d %d\n", l, r); out.flush(); int ans = in.nextInt(); return ans; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() { while(!tok.hasMoreTokens()) { try { tok = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return tok.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
fe9b0035d0a4f8fb8f5fafe8b461dae8
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; // @author : Dinosparton public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Sort implements Comparator<Pair> { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static Scanner sc = new Scanner(); static int query (int l,int r){ System.out.println('?'+" "+l+" "+r); System.out.flush();; int a = sc.nextInt();; return a; } public static void main(String args[]) throws Exception { StringBuilder res = new StringBuilder(); int tc = 1; while(tc-->0) { int n = sc.nextInt(); int max =0; int low = 0; int high =0; max=query(1,n); if(max>1 && query(1,max)==max){ low=1; high=max; while (low!=high-1){ int mid=low+(high-low)/2; int c=query(mid,max); if(c==max){ low=mid; } else{ high=mid; } } System.out.println('!'+" "+low); } else{ low=max; high=n; while(low!=high-1){ int mid=low+(high-low)/2; int c=query(max,mid); if(c==max){ high=mid; } else{ low=mid; } } System.out.println('!'+" "+high); } } System.out.println(res); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
445b6ec3634099ada264402e59883d0b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer tok; public static void main(String[] args) throws Exception { solution(); } public static void solution() throws Exception { // arr = new int[]{9,1,2,3,4,10,5,6,7,8}; // int n = arr.length; int n = Integer.parseInt(rd.readLine()); int res = sol_rec(1,n); System.out.println("! "+res); } public static int sol_rec(int left, int right) throws Exception { int Mid = sol_query(left,right); int fMid = -1, fLeft = left, fRight = Mid-1; // 첫번째로 중간값을 찾는다. if(Mid > left) fMid = sol_query(left,Mid); // 찾은 중간값이 맨 왼쪽값이 아니라면, 왼쪽값중에 최댓값이 있는지 탐색한다. while(fMid == Mid) { // 최댓값이 있는경우 왼쪽값 내부로 탐색한다. int gLeft = fLeft; fLeft = (fLeft + fRight)/2; // 이진탐색을 위하여 기준값을 절반으로 나눈다. fMid = sol_query(fLeft, Mid); // 해당 절반값에 최댓값이 존재하는지 확인한다. if(fMid != Mid) { // 절반값에 없다면 절반값 바깥쪽에 있는 것이므로 바깥쪽의 재차 절반체크를 위해 값을 조정한다. fRight = fLeft-1; fLeft = gLeft; fMid = Mid; } if(fRight == fLeft) return fLeft; else if (fRight - fLeft <= 1) return sol_res2(fLeft,fRight); // 값이 줄어들어서 특정할 수 있게되면 출력한다. } fLeft = Mid+1; fRight = right; while(true) { int gRight = fRight; fRight = (fLeft+fRight)/2; fMid = sol_query(Mid, fRight); if(fMid != Mid) { fLeft = fRight+1; fRight = gRight; fMid = Mid; } if(fRight == fLeft) return fLeft; else if(fRight - fLeft <= 1) return sol_res2(fLeft,fRight); } } public static int sol_query(int left, int right) throws Exception { System.out.println("? "+left +" "+right); System.out.flush(); return Integer.parseInt(rd.readLine()); // System.out.println(left+" "+right); // int[] copy = new int[right-left+1]; // int copc = 0; // for(int i=left-1;i<right;i++) copy[copc++] = arr[i]; // Arrays.sort(copy); // int sec = copy[copy.length-2]; // for(int i=left-1;i<right;i++) { // if( sec == arr[i]) return i+1; // } // return -1; } public static int sol_res2(int left, int right) throws Exception { int q = sol_query(left,right); return q == left ? right : left; } static int[] arr; }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
8e26ef5c358cede61a8ca072157c895a
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//@author->.....future_me......// //..............Learning.........// /*Compete against yourself*/ import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class C { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { try { // int t = sc.nextInt(); // while (t-- > 0) C.go(); out.flush(); } catch (Exception e) { return; } } static void go() { int n=sc.nextInt(); int x=ask(1,n); int ans=0; if(x==ask(1,x)) { int l=1;int r=x-1; while(l<=r) { int mid=(l+r)/2; if(ask(mid,x)==x) { ans=mid; l=mid+1; }else { r=mid-1; } } }else { int l=x+1;int r=n; while(l<=r) { int mid=(l+r)/2; if(ask(x,mid)==x) { ans=mid; r=mid-1; }else { l=mid+1; } } } out.println("! "+ans); out.flush(); } static int ask(int l ,int r) { if(l==r) { return -1; } out.println("? "+l+" "+r); out.flush(); int x=sc.nextInt(); return x; } // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static int mod = (int) 1e9 + 7; // Returns nCr % p using Fermat's // little theorem. static long fac[]; static long ncr(int n, int r, int p) { if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } private static long modInverse(long l, int p) { return pow(l,p-2); } static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = x * res % mod; } y /= 2; x = (x * x) % mod; } return res; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } //use this for double quotes inside string // " \"asdf\""
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
2d0aa960a84fd1d15fd8f856e356204b
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.*; public class Main { static Scanner scn = new Scanner(System.in) ; public static int query(int l , int r) { System.out.println("? " + l +" " + r ) ; int q = scn.nextInt() ; return q ; } public static int getLeft(int l , int k) { int r = k-1 ;int ans = k-1 ; while(l <= r) { int mid = (l+r)/2 ; int q = query(mid, k) ; if(q == k) { l =mid+1 ; ans = mid ; } else{ r=mid-1 ; } } return ans ; } public static int getRight(int k , int r) { int l = k+1 ;int ans = k+1 ; while( l <= r ) { int mid = ( l+r)/2 ; int q = query(k , mid ) ; if(q== k) { r=mid-1 ; ans = mid ; } else{ l =mid+1 ; } } return ans ; } public static void solve() { int n = scn.nextInt() ; int q = query(1,n) ; if(n ==2) { System.out.println("! " + (3-q)) ; } else{ int ans = 0 ; if(q ==1) { ans = getRight(1,n) ; } else if(q== n) { ans = getLeft(1,n) ; } else{ int temp = query(1,q) ; if(temp == q) { ans = getLeft(1,q) ; } else{ ans = getRight(q,n) ; } } System.out.println("! " + (ans)) ; } } public static void main (String[] args) throws java.lang.Exception { solve() ; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
51e8a8fc341f3165298075b46a77a4c1
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = false; private int query(int l, int r) { out.println(String.format("? %d %d", l, r)); out.flush(); return sc.nextInt(); } private void answer(int value) { out.println(String.format("! %d", value)); out.flush(); } private void solve() { int n = sc.nextInt(); int secIdx = query(1, n); int l, r; if (secIdx != n && query(secIdx, n) == secIdx) { l = secIdx + 1; r = n; if (l == r) { answer(l); return; } while (l + 1 < r) { int mid = (l + r) / 2; if (query(secIdx, mid) == secIdx) { r = mid; } else { l = mid + 1; } } if (l == r) { answer(l); return; } answer(query(l, r) == l ? r : l); } else { l = 1; r = secIdx - 1; if (l == r) { answer(l); return; } while (l + 1 < r) { int mid = (l + r) / 2; if (query(mid, secIdx) == secIdx) { l = mid; } else { r = mid - 1; } } if (l == r) { answer(l); return; } answer(query(l, r) == l ? r : l); } } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static CommonUtils cu; public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } } class CommonUtils { private <T> List<List<T>> createGraph(int n) { List<List<T>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) { g.add(new ArrayList<>()); } return g; } private void fill(int[][] a, int value) { for (int[] row : a) { fill(row, value); } } private void fill(int[] a, int value) { Arrays.fill(a, value); } } class MyScanner { private BufferedReader br; private StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int[][] nextIntArray(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] = 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 long[][] nextLongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } public List<Integer> nextList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } public List<Long> nextLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } public char[] nextCharArray(int n) { return next().toCharArray(); } public char[][] nextCharArray(int n, int m) { char[][] c = new char[n][m]; for (int i = 0; i < n; i++) { String s = next(); for (int j = 0; j < m; j++) { c[i][j] = s.charAt(j); } } return c; } } class MyWriter extends PrintWriter { public MyWriter(OutputStream outputStream) { super(outputStream); } public void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } public void printArray(long[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } public void println(int[] a) { for (int v : a) { println(v); } } public void print(List<Integer> list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } public void println(List<Integer> list) { list.forEach(this::println); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
354d0fcc6e8d2ee0e90ecf82772678be
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; public class Cf { public static void main(String [] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt();int a=1;int b=n; System.out.println("? "+a+" "+b ); System.out.flush(); int t=sc.nextInt(); int q=0; if(t==1){} else{ System.out.println("? "+a+" "+t); System.out.flush(); q=sc.nextInt(); } int ans=0;int l=0;int r=0; if(q==t){ l=1;r=t; while(l<=r){ if(l==r){ans=l;break;} if(r==l+1){ if(r==t){ans=l;break;} System.out.println("? "+r+" "+t); System.out.flush(); int check=sc.nextInt(); if(check==t){ans=r;break;} ans=l;break; } int m=(l+r)/2; System.out.println("? "+m+" "+t); System.out.flush(); int res=sc.nextInt(); if(res==t){l=m;} else{ r=m-1; } } } else{ l=t;r=n; while(r>=l){ if(l==r){ans=l;break;} if(r==l+1){ if(l==t){ans=r;break;} System.out.println("? "+t+" "+l); System.out.flush(); int check=sc.nextInt(); if(check==t){ans=l;break;} ans=r;break; } int m=(l+r)/2; System.out.println("? "+t+" "+m); System.out.flush(); int res=sc.nextInt(); if(res==t){r=m;} else{ l=m+1; } } } System.out.println("! "+ans); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
a2b5f2f81e1c41f6e037f13c1bdf9c64
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; public class cf703 { public static Scanner sc = new Scanner(System.in); public static int problemA(int n, int arr[]){ long tillNow=0; for(int i=0; i<n; i++){ tillNow+=arr[i]; if(tillNow<i) return 0; tillNow-=i; } return 1; } public static int binSrch(int arr[], int T){ //get index k of arr which is last element arr[k]<T and arr[k+1]>=T int n = arr.length; int start = 0, end=n-1; while(start<end){ int mid = (start+end)/2; if(arr[mid]<T && arr[mid+1]>=T) return mid; else if(arr[mid]>=T) end=mid-1; else start=mid; } return (end==0)?end:-1; } public static long calcD(int n, int id, long[] pr, int v){ long sum=0; if(id>=0){ sum = Math.abs((id+1)*v-pr[id])+Math.abs((pr[n-1]-pr[id]) - v* (n-1-id)); } else { sum= Math.abs((pr[n-1]) - v* (n)); } return sum; } public static int ask(int l, int r){ System.out.println("? "+l+" "+r); int index = sc.nextInt(); return index; } public static long problemB(int n, int x[], int y[]){ // int maxX = 1000000000; // int maxY = 1000000000; Arrays.sort(x); Arrays.sort(y); long res1 = x[(n/2)]-x[(n-1)/2] +1; long res2 = y[(n/2)]-y[(n-1)/2] +1; return res1*res2; } public static void problemCD(int n){ int smaxIndex = ask(1,n); int start=1; int end=n; if(smaxIndex==1 || ask(1,smaxIndex)!=smaxIndex){ //between smaxindex-n int l=smaxIndex; int r=n; while((r-l)>1){ int mid = (l+r)/2; if(ask(smaxIndex,mid)==smaxIndex) r=mid; else l=mid; } System.out.println("! "+r); System.out.flush(); } else{ //between 1-smaxindex int l=1; int r=smaxIndex; while((r-l)>1){ int mid = (l+r)/2; if(ask(mid,smaxIndex)==smaxIndex) l=mid; else r=mid; } System.out.println("! "+l); System.out.flush(); } } public static void main(String[] args) { int tcases; tcases = sc.nextInt(); problemCD(tcases); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
8c59a772abf077d842b872584cd1d897
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class feb20 { // *** ++ // +=-==+ +++=- // +-:---==+ *+=----= // +-:------==+ ++=------== // =-----------=++========================= // +--:::::---:-----============-=======+++==== // +---:..:----::-===============-======+++++++++ // =---:...---:-===================---===++++++++++ // +----:...:-=======================--==+++++++++++ // +-:------====================++===---==++++===+++++ // +=-----======================+++++==---==+==-::=++**+ // +=-----================---=======++=========::.:-+***** // +==::-====================--: --:-====++=+===:..-=+***** // +=---=====================-... :=..:-=+++++++++===++***** // +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+ // +=======++++++++++++=+++++++============++++++=======+****** // +=====+++++++++++++++++++++++++==++++==++++++=:... . .+**** // ++====++++++++++++++++++++++++++++++++++++++++-. ..-+**** // +======++++++++++++++++++++++++++++++++===+====:. ..:=++++ // +===--=====+++++++++++++++++++++++++++=========-::....::-=++* // ====--==========+++++++==+++===++++===========--:::....:=++* // ====---===++++=====++++++==+++=======-::--===-:. ....:-+++ // ==--=--====++++++++==+++++++++++======--::::...::::::-=+++ // ===----===++++++++++++++++++++============--=-==----==+++ // =--------====++++++++++++++++=====================+++++++ // =---------=======++++++++====+++=================++++++++ // -----------========+++++++++++++++=================+++++++ // =----------==========++++++++++=====================++++++++ // =====------==============+++++++===================+++==+++++ // =======------==========================================++++++ /* * created by : Nitesh Gupta * */ public static void main(String[] args) throws Exception { first(); // sec(); // third(); // four(); // fif(); // six(); } private static void first() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); int sm = ask(1, n); int left = ask(1, sm); int ans = 1; if (left == sm) { int st = 1, en = sm - 1; while (st <= en) { int mid = (st + en) / 2; int pos = ask(mid, sm); if (pos == sm) { ans = mid; st = mid + 1; } else { en = mid - 1; } } } else { int st = sm + 1, en = n; while (st <= en) { int mid = (st + en) / 2; int pos = ask(sm, mid); if (pos == sm) { ans = mid; en = mid - 1; } else { st = mid + 1; } } } System.out.println("! " + ans); return; } public static int ask(int l, int r) throws Exception { if(l==r)return -1; System.out.println("? " + l + " " + r); System.out.flush(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); return n; } private static void sec() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int t = Integer.parseInt(scn[0]); StringBuilder sb = new StringBuilder(); while (t-- > 0) { scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } private static void third() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int t = Integer.parseInt(scn[0]); StringBuilder sb = new StringBuilder(); while (t-- > 0) { scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } private static void four() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int t = Integer.parseInt(scn[0]); StringBuilder sb = new StringBuilder(); while (t-- > 0) { scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } private static void fif() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int t = Integer.parseInt(scn[0]); StringBuilder sb = new StringBuilder(); while (t-- > 0) { scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } private static void six() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] scn = (br.readLine()).trim().split(" "); int t = Integer.parseInt(scn[0]); StringBuilder sb = new StringBuilder(); while (t-- > 0) { scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } public static void sort(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } public static void print(long[][] dp) { for (long[] a : dp) { for (long ele : a) { System.out.print(ele + " "); } System.out.println(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
c20ac575d82774c77bfc2322b922e58e
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.System.in; import static java.lang.System.out; public class Main { public static void main(String[] args){ FastReader fr=new FastReader(); int tc=1; while(tc-->0){ int n=fr.nextInt(); int sm=query(1,n,fr); if(sm!=1 && query(1,sm,fr)==sm){ int l=1,r=sm-1; while(l<=r){ int mid=(l+r)/2; if(query(mid,sm,fr)==sm){ l=mid+1; }else{ r=mid-1; } } out.println("! "+r); }else{ int l=sm+1,r=n; while(l<=r){ int mid=(l+r)/2; if(query(sm,mid,fr)==sm){ r=mid-1; }else{ l=mid+1; } } out.println("! "+l); } out.flush(); } } static int query(int l,int r,FastReader fr){ out.println("? "+l+" "+r); out.flush(); return fr.nextInt(); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(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()); } byte nextByte(){ return Byte.parseByte(next()); } Float nextFloat(){ return Float.parseFloat(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } short nextShort(){ return Short.parseShort(next()); } Boolean nextbol(){ return Boolean.parseBoolean(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
d4c78a2f2f9b915dd936e953902fa136
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.Arrays; public class codeforces { static int ask(int l,int r)throws IOException { if(l==r) { return -1; } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int out1; System.out.println("? "+l+" "+r); System.out.flush(); out1=Integer.parseInt(br.readLine()); return out1; } public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int l=1; int r=n; int pos=ask(1,n); if(ask(1,pos)==pos) { l=1; r=pos; while(l<r) { int mid=(l+r+1)/2; if(ask(mid,n)==pos) { l=mid; } else { r=mid-1; } } System.out.println("! "+l); } else { l=pos; r=n; while(l<r) { int mid=(l+r)/2; if(ask(pos,mid)==pos) { r=mid; } else { l=mid+1; } } System.out.println("! "+l); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
af324e16436937c4de84bb54f56392bd
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.ListIterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; import javax.swing.text.html.HTMLDocument.Iterator; /* * Printing Euler Tour in an Eulerian Graph */ public class E { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int l = 1; int r = n; System.out.println("? " + l + " " + r); int pos = sc.nextInt(); int ans = -1; int mid = l + r >> 1; if (pos == l) { int s = pos + 1; int e = n; while (s <= e) { mid = s + e >> 1; System.out.println("? " + pos + " " + mid); int p = sc.nextInt(); if (p == pos) { ans = mid; e = mid - 1; } else { s = mid + 1; } } System.out.println("! " + ans); } else { System.out.println("? " + 1 + " " + pos); int p = sc.nextInt(); if (p == pos) { int s = 1; int e = pos - 1; while (s <= e) { mid = s + e >> 1; System.out.println("? " + mid + " " + pos); p = sc.nextInt(); if (p == pos) { s = mid + 1; ans = mid; } else { e = mid - 1; } // System.out.println(s + " " + e); } System.out.println("! " + ans); } else { int s = pos + 1; int e = n; while (s <= e) { mid = s + e >> 1; System.out.println("? " + pos + " " + mid); p = sc.nextInt(); if (p == pos) { ans = mid; e = mid - 1; } else { s = mid + 1; } } System.out.println("! " + ans); } } pw.flush(); } static class pair implements Comparable<pair> { int x; int y; public pair(int d, int u) { x = d; y = u; } @Override public int compareTo(pair o) { // // TODO Auto-generated method stub // if (y == o.y) // return x - o.x; return 0; } } static ArrayList<Integer>[] adjList; static LinkedList<Integer> tour; static ArrayList<Integer>[] g; static ArrayList<Integer> primes; // generated by sieve /* * 1. Generating a list of prime factors of N */ static HashMap<Integer, Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { HashMap<Integer, Integer> factors = new HashMap<>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { int c = 0; while (N % p == 0) { N /= p; c++; } if (c > 0) factors.put(p, c); if (idx + 1 == primes.size()) break; p = primes.get(++idx); } if (N != 1) // last prime factor may be > sqrt(N) factors.put(N, 1); // for integers whose largest prime factor has a power of 1 return factors; } static int[] isComposite; static void sieve(int N) // O(N log log N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance { primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in // modified sieve isComposite[j] = 1; } } static HashSet<Integer> hs; static int[] a; static long[] cum; static void divide(int s, int e) { // System.out.println(s + " " + e); if (e == -1 || s > e) return; if (cum[e + 1] - cum[s] <= 1e9) hs.add((int) (cum[e + 1] - cum[s])); // System.out.println(cum[e + 1] - cum[s]); if (e == s) return; int temp = a[e] + a[s] >> 1; int lo = s; int hi = e; int ans = -1; while (lo <= hi) { int mid = lo + hi >> 1; if (a[mid] <= temp) { ans = mid; lo = mid + 1; } else hi = mid - 1; } if (ans == -1 || ans == e) return; divide(s, ans); divide(ans + 1, e); } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (int) ((res * 1l * x) % p); // y must be even now y = y >> 1; // y = y/2 x = (int) ((x * 1l * x) % p); } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (int) (((fac[n] * 1l * modInverse(fac[r], p)) % p * modInverse(fac[n - r], p) % p) % p); } static int[] fac; static void init() { fac = new int[200001]; fac[0] = 1; for (int i = 1; i <= 200000; i++) fac[i] = (int) ((fac[i - 1] * 1l * i) % mod); } static int mod = 1000000007; static long ans = 10000000000l; static void find(int i, int sum, String s, int t) { if (sum == t) { ans = Math.min(ans, Integer.parseInt(s)); return; } // System.out.println(s); if (i == 0) return; find(i - 1, sum, s, t); find(i - 1, sum + i, i + s, t); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
4b978f5cc80fdca8d2aaff76b9a09637
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//Implemented By Aman Kotiyal Date:-19-Feb-2021 Time:-1:07:15 am import java.io.*; import java.util.*; public class ques1 { public static void main(String[] args)throws Exception{ new ques1().run();} long mod=1000000000+7; void solve() throws Exception { int n=ni(); int l=1,r=n; out.println("? "+l+" "+r); out.flush(); int ind=ni(); int sec = ind; int cnt=1; if(sec!=1&&sec!=n) { out.println("? "+l+" "+sec); out.flush(); ind=ni(); if(ind==sec) // left side r=sec; else// right side l=sec; cnt++; } int ans=1; if(sec==n-1) ans=n; while(l<=r) { int mid=(l+r)/2; if(sec<mid) { cnt++; out.println("? "+sec+" "+mid); out.flush(); ind=ni(); if(ind==sec) { r=mid-1; ans=mid; } else { l=mid+1; ans=mid+1; } } else if(sec>mid) { cnt++; out.println("? "+mid+" "+sec); out.flush(); ind=ni(); if(ind==sec) { l=mid+1; ans=mid; } else { r=mid-1; ans=mid-1; } } else { if(l+1==r) { out.println("? "+l+" "+r); out.flush(); ind=ni(); if(sec==ind) { if(sec==l) ans=r; if(sec==r) ans=l; } break; } break; } } out.println("! "+ans); out.flush(); } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
eaa2854c13abc0c68a7f823e1d0859a0
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class GuessingTheGreatest { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastScanner f=new FastScanner(); int n=f.nextInt(); int smax=secmax(0,n-1); int l=0; int r=n-1; if(smax==0||secmax(0, smax)!=smax) { l=smax; r=n-1; while(r-l>1) { int mid=(l+r)/2; if(secmax(smax,mid)==smax) { r=mid; }else l=mid; } System.out.println("!"+" "+(r+1)); System.out.flush(); }else { l=0; r=smax; while(r-l>1) { int mid=(l+r)/2; if(secmax(mid,smax)==smax) { l=mid; }else r=mid; } System.out.println("!"+" "+(l+1)); System.out.flush(); } } static int secmax(int l,int r) { FastScanner f=new FastScanner(); if(l<r) { System.out.println("?"+" "+(l+1)+" "+(r+1)); System.out.flush(); int q=f.nextInt(); return (q-1); }else return -1; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
aef1d6dd3953bf470195e19c8b684006
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { static Scanner sc=new Scanner(System.in); static int ask(int l,int r) { if(l==r) return -1; System.out.println("? "+" "+l+" "+r); int x=sc.nextInt(); return x; } public static void main (String[] args) throws java.lang.Exception { // your code goes here int n=sc.nextInt(); int secmaxpos=ask(1,n); if(ask(1,secmaxpos)==secmaxpos) { int low=1; int high=secmaxpos; while(low<high) { int mid=(low+high+1)/2; if(ask(mid,n)==secmaxpos) low=mid; else high=mid-1; } System.out.println("! "+" "+low); } else { int low=secmaxpos; int high=n; while(low<high) { int mid=(low+high)/2; if(ask(1,mid)==secmaxpos) high=mid; else low=mid+1; } System.out.println("! "+" "+low); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
a24809b02b6d19a296dd16b21d854f13
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package credit; import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ static boolean v[]; static int ans[]; int size[]; static int count=0; static int dsu=0; static int c=0; static int e9=1000000007; int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; long max1=Long.MIN_VALUE; long min1=Long.MAX_VALUE; boolean aBoolean=true; boolean y=false; long m=0; static boolean t1=false; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } int parent[]; int rank[]; int n=0; ArrayList<ArrayList<Integer>> arrayLists; boolean v1[]; static boolean t2=false; boolean r=false; int fib[]; int fib1[]; int ind[]; int min3=Integer.MAX_VALUE; public static void main(String[] args) throws IOException { Main g = new Main(); g.go(); } public void go() throws IOException { FastReader scanner = new FastReader(); // Scanner scanner = new Scanner(System.in); // BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); // String s; PrintWriter printWriter = new PrintWriter(System.out); int t =1; for (int i = 0; i < t; i++) { int n=scanner.nextInt(); int s=1; int e=n; int u=0; int ans=0; u = sm(s, e); if(u==sm(s,u)){ s=1; e=u-1; while(s<=e){ int m=(s+e)/2; if(sm(m,u)==u){ ans=m; s=m+1; }else{ e=m-1; } } }else{ s=u+1; e=n; while(s<=e){ int m=(s+e)/2; if(sm(u,m)==u){ ans=m; e=m-1; }else{ s=m+1; } } } printWriter.println("! "+(ans)); } printWriter.flush(); } public int sm(int l,int r){ FastReader scanner =new FastReader(); if(l==r){ return -1; } System.out.println("? "+(l)+" "+(r)); int h=scanner.nextInt(); System.out.flush(); return h; } static final int mod=1_000_000_007; public long mul(long a, long b) { return a*b; } public long fact(int x) { long ans=1; for (int i=2; i<=x; i++) ans=mul(ans, i); return ans; } public long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } public long modInv(long x) { return fastPow(x, mod-2); } public long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k)))); } // public void sieve(int n){ // fib=new int[n+1]; // fib[1]=-1; // fib[0]=-1; // for (int i =2; i*i<=n; i++) { // if(fib[i]==0) // for (int j =i*i; j <=n; j+=i){ // fib[j]=-1; //// System.out.println("l"); // } // } // } public void parent(int n){ for (int i = 0; i < n; i++) { parent[i]=i; rank[i]=1; size[i]=1; } } public void union(int i,int j){ int root1=find(i); int root2=find(j); // if(root1 != root2) { // parent[root2] = root1; //// sz[a] += sz[b]; // } if(root1==root2){ return; } if(rank[root1]>rank[root2]){ parent[root2]=root1; size[root1]+=size[root2]; } else if(rank[root1]<rank[root2]){ parent[root1]=root2; size[root2]+=size[root1]; } else{ parent[root2]=root1; rank[root1]+=1; size[root1]+=size[root2]; } } public int find(int p){ if(parent[p]!=p){ parent[p]=find(parent[p]); } return parent[p]; // if(parent[p]==-1){ // return -1; // } // else if(parent[p]==p){ // return p; // } // else { // parent[p]=find(parent[p]); // return parent[p]; // } } public double dist(double x1,double y1,double x2,double y2){ double e=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); double e1=Math.sqrt(e); return e1; } public void make(int p){ parent[p]=p; rank[p]=1; } Random rand = new Random(); public void sort(int[] a, int n) { for (int i = 0; i < n; i++) { int j = rand.nextInt(i + 1); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } Arrays.sort(a, 0, n); } public long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } public void dfs(ArrayList<Integer> arrayLists1){ for (int i = 0; i < arrayLists1.size(); i++) { if(v1[arrayLists1.get(i)]==false){ System.out.println(arrayLists1.get(i)); v1[arrayLists1.get(i)]=true; count++; dfs(arrayLists.get(arrayLists1.get(i))); } } } private void dfs2(ArrayList<Integer>[]arrayList,int j,int count){ ans[j]=count; for(int i:arrayList[j]){ if(ans[i]==-1){ dfs2(arrayList,i,count); } } } private void dfs3(ArrayList<Integer>[] arrayList, int j) { v[j]=true; count++; for(int i:arrayList[j]){ if(v[i]==false) { dfs3(arrayList, i); } } } public double fact(double h){ double sum=1; while(h>=1){ sum=(sum%e9)*(h%e9); h--; } return sum%e9; } public long primef(double r){ long c=0; long ans=1; while(r%2==0){ c++; r=r/2; } if(c>0){ ans*=2; } c=0; // System.out.println(ans+" "+r); for (int i = 3; i <=Math.sqrt(r) ;i+=2) { while(r%i==0){ // System.out.println(i); c++; r=r/i; } if(c>0){ ans*=i; } c=0; } if(r>2){ ans*=r; } return ans; } public long divisor(double r){ long c=0; for (int i = 1; i <=Math.sqrt(r); i++) { if(r%i==0){ if(r/i==i){ c++; } else{ c+=2; } } } return c; } } class Pair{ int x; int y; double z; public Pair(int x,int y){ this.x=x; this.y=y; } @Override public int hashCode() { int hash =37; return this.x * hash + this.y; } @Override public boolean equals(Object o1){ if(o1==null||o1.getClass()!=this.getClass()){ return false; } Pair o=(Pair)o1; if(o.x==this.x&&o.y==this.y){ return true; } return false; } } class Sorting implements Comparator<Pair> { public int compare(Pair p1,Pair p2){ // if(p1.x==p2.x){ // return -1*Double.compare(p1.y,p2.y); // } // else { return Double.compare(p1.x, p2.x); // } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
eb97d2700669ed84b10fdf8f32871d5a
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.System.out; public class C2_GuessingTheGreatest_HardVersion { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { int n = readInt(); int secondMaxPos = query(1, n); int low = 1; int high = n; while (low + 1 < high) { int mid = low + high >> 1; if (secondMaxPos <= mid) { int newSecondMaxPos = query(Math.min(low, secondMaxPos), mid); if (newSecondMaxPos == secondMaxPos) { high = mid; } else { low = mid + 1; } } else { int newSecondMaxPos = query(mid, Math.max(high, secondMaxPos)); if (newSecondMaxPos == secondMaxPos) { low = mid; } else { high = mid - 1; } } } if (low == high) { answer(low); } else { // low + 1 = high int notMaxPos = query(low, high); answer(notMaxPos == low ? high : low); } } private static int query(int l, int r) throws IOException { out.println("? " + l + " " + r); out.flush(); return readInt(); } private static void answer(int a) { out.println("! " + a); out.flush(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
12a54cce13b31a13ed959e44cb624388
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); int n=input.scanInt(); if(n==1) { System.out.println("! 1"); return; } System.out.println("? "+1+" "+n); System.out.flush(); int indx=input.scanInt(); int ans=-1; int lft=-1; if(indx!=1) { System.out.println("? "+1+" "+indx); System.out.flush(); lft=input.scanInt(); } int l,r; //Right if(lft!=indx) { l=indx+1; r=n; while(l<=r) { int mid=(l+r)/2; System.out.println("? "+indx+" "+mid); System.out.flush(); int idx=input.scanInt(); if(idx==indx) { ans=mid; r=mid-1; } else { l=mid+1; } } } if(lft==indx) { //left l=1; r=indx-1; while(l<=r) { int mid=(l+r)/2; System.out.println("? "+mid+" "+indx); System.out.flush(); int idx=input.scanInt(); if(idx==indx) { ans=mid; l=mid+1; } else { r=mid-1; } } } System.out.println("! "+ans); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
fe77c6dc08ca0e540629493fd8d4d719
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class C { private static PrintWriter out; private static FS sc; private static class FS { StringTokenizer st; BufferedReader br; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();} } return st.nextToken(); } public String nextLine() { String s = null; try {s = br.readLine();} catch (IOException e) {e.printStackTrace();} return s; } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} } private static void bs(int l, int r, boolean left) { // out.println(left); int smax = 0, ans = -1; if (left) smax = l - 1; else smax = r + 1; while (l <= r) { int m = l + ((r-l)>>1); if (left) { out.println("? " + smax + " " + m); if (sc.nextInt() == smax) {ans = m; r = m-1;} else l = m+1; } else { out.println("? " + m + " " + smax); if (sc.nextInt() == smax) {ans = m; l = m + 1;} else r = m - 1; } } out.println("! " + ans); } public static void main(String[] args) { sc = new FS(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int n = sc.nextInt(); out.println("? 1 " + n); int smax = sc.nextInt(); if (smax == n) bs(1, n - 1, false); else if (smax == 1) bs(2, n, true); else { out.println("? 1 " + smax); if (sc.nextInt() == smax) bs(1, smax - 1, false); else bs(smax + 1, n, true); } out.close(); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
aed714ae854f172d06c204b7fb6d81c3
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
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.StringTokenizer; public class problemC { static class Solution { HashMap<Point, Integer> map = new HashMap<>(); int query(int l, int r) { Point p = new Point(l, r); if (map.containsKey(p)) return map.get(p); System.out.println("? " + l + " " + r); System.out.flush(); int ans = fs.nextInt(); map.put(p, ans); return ans; } void solve() { int n = fs.nextInt(); int second = query(1, n); int left = second == 1 ? -1 : query(1, second); if (left == second) { // search left. int ans = -1; for (int low=1, high=second-1; low <= high; ) { int mid = (low+high)/2; int got = query(mid, second); if (got == second) { // contains max. ans = Math.max(ans, mid); low = mid + 1; } else { // does not contain max. high = mid - 1; } } print(ans); return; } else { // search right. int ans = n+1; for (int low=second+1, high=n; low <= high ; ) { int mid = (low+high)/2; int got = query(second, mid); if (got == second) { // contains max. high = mid - 1; ans = Math.min(ans, mid); } else { // does not contain max. low = mid + 1; } } print(ans); return; } } void print(int ans) { System.out.println("! " + ans); System.out.flush(); } } static class Point implements Comparable<Point> { int x, y; Point(int x, int y) {this.x = x; this.y = y;} @Override public int compareTo(Point other) { if (this.x == other.x) return Integer.compare(this.y, other.y); return Integer.compare(this.x, other.x); } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false; Point other = (Point)o; return this.x == other.x && this.y == other.y; } @Override public int hashCode() { return Arrays.hashCode(new int[]{this.x , this.y}); } } public static void main(String[] args) throws Exception { Solution solution = new Solution(); solution.solve(); } static void debug(Object... O) { System.err.println("DEBUG: " + Arrays.deepToString(O)); } private static FastScanner fs = new FastScanner(); static class FastScanner { // Thanks SecondThread. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } ArrayList<Integer> readArrayList(int n) { ArrayList<Integer> a = new ArrayList<>(n); for (int i = 0 ; i < n; i ++ ) a.add(fs.nextInt()); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
b655dae2464cfb1ddd1404bfe9177e2d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class C { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long MOD = (long) (1e9 + 7); //static int MOD = 998244353; static long MOD2 = MOD * MOD; static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; public static void main(String[] args) { int test = 1; //test = sc.nextInt(); for (int i = 1; i <= test; i++){ //out.print("Case #"+i+": "); solve(); } out.flush(); out.close(); } static long oo = (long) (1e16); static void solve(){ int n = sc.nextInt(); int l = 1; int r = n; int ele = query(1,n); int lele = -1; int rele = -1; if(ele>l){ lele = query(l,ele); } if(r>ele){ rele = query(ele,r); } if(lele==ele){ r = ele; l--; while(r-l>1){ int m = l+(r-l)/2; int idx = query(m,ele); if(idx==ele){ l = m; }else{ r = m; } } out.println("! "+l); out.flush(); }else if(rele==ele){ l = ele; r++; while(r-l>1){ int m = l+(r-l)/2; int idx = query(ele,m); if(idx==ele){ r = m; }else{ l = m; } } out.println("! "+r); out.flush(); } } static int query(int l,int r){ out.println("? "+l+" "+r); out.flush(); return sc.nextInt(); } static long nC2(long n) { return add((n * (n + 1)) / 2, 0); } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long add(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Pair Class static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if ((this.x == o.x)) { return (this.y - o.y); } return (this.x - o.x); } } //Shuffle Sort 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); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Euclidean Algorithm static long gcd(long A, long B) { if (B == 0) return A; return gcd(B, A % B); } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD; return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD; } //AKS Algorithm static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
2fe90f39f7369288c4fdbba97d16630d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class _1486_C2 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int sec = query(1, n, in); int lbound = sec + 1; int rbound = n; if(sec > 1 && query(1, sec, in) == sec) { lbound = 1; rbound = sec; } int res = 0; while(lbound < rbound) { if(rbound == lbound + 1) { int sec2 = query(lbound, rbound, in); if(sec2 == lbound) res = rbound; else res = lbound; break; } int avg = (lbound + rbound) / 2; if(avg < sec) { int sec2 = query(avg, sec, in); if(sec2 == sec) { lbound = avg; }else { rbound = avg - 1; } }else { int sec2 = query(sec, avg, in); if(sec2 == sec) { rbound = avg; }else { lbound = avg + 1; } } } if(res == 0) { System.out.println("! " + lbound); }else { System.out.println("! " + res); } in.close(); } static int query(int l, int r, BufferedReader in) throws IOException { System.out.println("? " + l + " " + r); System.out.flush(); return Integer.parseInt(in.readLine()); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
523ff66af306e2fec3590b73e08ed7f7
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class Main2 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc = new FastReader(); static int n; public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; // t = sc.nextInt(); while(t-->0) { n = sc.nextInt(); int sm = ask(1,n); int left = ask(1,sm); int ans = -1; if(left==sm) { int l=1, h=sm-1; while(l<=h) { int mid = (l+h)/2; left = ask(mid,sm); if(left == sm) { ans = mid; l = mid+1; } else h = mid-1; } } else { int l=sm+1, h=n; while(l<=h) { int mid = (l+h)/2; left = ask(sm,mid); if(left == sm) { ans = mid; h = mid-1; } else l = mid+1; } } System.out.println("! "+ans); } out.close(); } private static int ask(int idx, int h) { if(idx>n || h>n || idx == 0 || h == 0) return -1; if(idx == h) return -1; System.out.println("? "+idx+" "+h); System.out.flush(); int ret = sc.nextInt(); return ret; } private static long pow(int i, int x) { long ans = 1; while(x>0) { if(x%2==0) { i*=i; x/=2; } else { ans*=i; x--; } } // System.out.println(ans+" ans"); return ans; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
1de958aa6383710d05502884886f7686
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { static BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); public static void main(String args[])throws Exception { StringBuilder sb=new StringBuilder(); int n=Integer.parseInt(bu.readLine()); System.out.println("? 1 "+n); System.out.flush(); int in=Integer.parseInt(bu.readLine()); if(n==2) { int ans=1; if(in==1) ans=2; System.out.println("! "+ans); System.out.flush(); return; } int ans=1; if(in==1) ans=getright(1,n); else if(in==n) ans=getleft(1,n); else { System.out.println("? 1 "+in); System.out.flush(); int x=Integer.parseInt(bu.readLine()); if(x==in) ans=getleft(1,in); else ans=getright(in,n); } System.out.println("! "+ans); System.out.flush(); } static int getright(int k,int r)throws Exception { int l=k+1,mid,ans=l; while(l<=r) { mid=(l+r)/2; System.out.println("? "+k+" "+mid); System.out.flush(); int v=Integer.parseInt(bu.readLine()); if(v==k) { ans=mid; r=mid-1; } else l=mid+1; } return ans; } static int getleft(int l,int k)throws Exception { int r=k-1,mid,ans=r; while(l<=r) { mid=(l+r)/2; System.out.println("? "+mid+" "+k); System.out.flush(); int v=Integer.parseInt(bu.readLine()); if(v==k) { ans=mid; l=mid+1; } else r=mid-1; } return ans; } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
72bc21a7290b5ea38629e595bdea13e4
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class C{ static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { // FastScanner fs = new FastScanner(); // PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(); int ind = query(1, n); int l = -1, r = -1; boolean inleft = false; if(ind==1) { l = 2; r = n; inleft = false; } else if(ind==n) { l = 1; r = n-1; inleft = true; } else { int q = query(1, ind); if(q==ind) { l = 1; r = ind-1; inleft = true; } else { l = ind + 1; r = n; inleft = false; } } if(inleft) { while(l<r) { int mid = (l+r+1)/2; int k = query(mid, ind); if(k==ind) { l = mid; } else { r = mid - 1; } } output(l); } else { while(l<r) { int mid = (l+r)/2; int k = query(ind, mid); if(k==ind) { r = mid; } else { l = mid + 1; } } output(l); } } out.close(); } static int query(int l, int r) { out.println("? "+l+" "+r); out.flush(); int x = fs.nextInt(); return x; } static void output(int x) { out.println("! "+x); out.flush(); return; } static final Random random=new Random(); static <T> void shuffle(T[] arr) { int n = arr.length; for(int i=0;i<n;i++ ) { int k = random.nextInt(n); T temp = arr[k]; arr[k] = arr[i]; arr[i] = temp; } } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void reverse(int[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ int temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static void reverse(long[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ long temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static <T> void reverse(T[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++) { T temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } 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
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
8a4debcb62d6ce7a652101a5cd5f90eb
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.util.*; public class TestClass { public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final int readInt() throws IOException { return (int) readLong(); } public final long readLong() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); if (c == -1) throw new IOException(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } public final int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public final long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } static long mulmod(long a, long b, long mod) { long res = 0; // Initialize result a = a % mod; while (b > 0) { // If b is odd, add 'a' to result if (b % 2 == 1) { res = (res + a) % mod; } // Multiply 'a' with 2 a = (a * 2) % mod; // Divide b by 2 b /= 2; } // Return result return res % mod; } static long pow(long a, long b, long MOD) { long x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); if (x > MOD) x %= MOD; } y = (y * y); if (y > MOD) y %= MOD; b /= 2; } return x; } static long[] f = new long[100001]; static long InverseEuler(long n, long MOD) { return pow(n, MOD - 2, MOD); } static long C(int n, int r, long MOD) { return (f[n] * ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) % MOD; } static int[] h = {0, 0, -1, 1}; static int[] v = {1, -1, 0, 0}; public static class Pair { public int a; public int b; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { return Objects.hash(a, b); } public Pair(int a, int b) { this.a = a; this.b = b; } } static class Pair2 { public long cost; int node; public Pair2(long cos, int node) { this.cost = cos; this.node = node; } } static long compute_hash(String s) { int p = 31; int m = 1000000007; long hash_value = 0; long p_pow = 1; for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m; p_pow = (p_pow * p) % m; } return hash_value; } public static class SegmentTree { long[][] tree; int n; public SegmentTree(int[] nodes) { tree = new long[nodes.length * 4][2]; n = nodes.length; build(0, n - 1, 0, nodes); } private void build(int l, int r, int pos, int[] nodes) { if (l == r) { tree[pos][0] = nodes[l]; tree[pos][1] = l; return; } int mid = (l + r) / 2; build(l, mid, 2 * pos + 1, nodes); build(mid + 1, r, 2 * pos + 2, nodes); if (tree[2 * pos + 1][0] > tree[2 * pos + 2][0]) { tree[pos][1] = tree[2 * pos + 1][1]; } else { tree[pos][1] = tree[2 * pos + 2][1]; } tree[pos][0] = Math.max(tree[2 * pos + 1][0], tree[2 * pos + 2][0]); } // public void update(int pos, int val) { // updateUtil(0, n - 1, 0, pos, val); // } public long[] get(int l, int r) { return getUtil(0, n - 1, 0, l, r); } private long[] getUtil(int l, int r, int pos, int ql, int qr) { if (ql > r || qr < l) { return new long[]{-1, -1}; } if (l >= ql && r <= qr) { return tree[pos]; } int mid = (l + r) / 2; long[] left = getUtil(l, mid, 2 * pos + 1, ql, qr); long[] right = getUtil(mid + 1, r, 2 * pos + 2, ql, qr); long choice = right[1]; if (left[0] > right[0]) choice = left[1]; return new long[]{Math.max(left[0], right[0]), choice}; } // private void updateUtil(int l, int r, int pos, int i, int val) { // if (i < l || i > r) { // return; // } // if (l == r) { // tree[pos] = val; // return; // } // int mid = (l + r) / 2; // updateUtil(l, mid, 2 * pos + 1, i, val); // updateUtil(mid + 1, r, 2 * pos + 2, i, val); // tree[pos] = tree[2 * pos + 1] + tree[2 * pos + 2]; // } } static int counter = 0; static int[] rIn; static int[] rOut; static int[] lIn; static int[] lOut; private static int[] flatten; private static int[] lFlatten; static long answer = 0; static int VISITED = 1; static int VISITING = 2; static int[] DIRX = new int[]{0, 0, 1, -1}; static int[] DIRY = new int[]{1, -1, 0, 0}; public static class Pair22 { int num, pos; public Pair22(int x, int y) { this.num = x; this.pos = y; } } public static void main(String[] args) throws Exception { //https://i...content-available-to-author-only...e.com/ebRGa6 InputReader in = new InputReader(System.in); FastWriter out = new FastWriter(System.out); int t =1; while (t-- > 0) { int n = in.readInt(); System.out.println("? 1 " + n); System.out.flush(); int second = in.readInt(); int firstHalf = -1; if (1 != second) { System.out.println("? 1 " + second); System.out.flush(); firstHalf = in.readInt(); } int secondHalf = -1; if (second != n) { System.out.println("? " + second + " " + n); System.out.flush(); secondHalf = in.readInt(); } if (firstHalf == second) { search2(n, second, second); } else { assert (secondHalf == second); search(n, second, n); } } } private static void search2(int n, int pos, int hii) throws IOException { InputReader in = new InputReader(System.in); int lo = 1; int hi = pos - 1; while (lo < hi) { int mi = (lo + hi + 1) / 2; System.out.println("? " + mi + " " + pos); System.out.flush(); int check = in.readInt(); if (check == pos) { lo = mi; } else { hi = mi - 1; } } System.out.println("! " + lo ); System.out.flush(); } private static void search(int n, int pos, int hi) throws IOException { InputReader in = new InputReader(System.in); int lo = pos + 1; while (lo < hi) { int mi = (lo + hi) / 2; System.out.println("? " + pos + " " + mi); System.out.flush(); int check = in.readInt(); if (check == pos) { hi = mi; } else { lo = mi + 1; } } System.out.println("! " + lo ); System.out.flush(); } private static void solvedd(int[] arr, int left, int right, int[] ans, int depth) { if (left > right) return; int maxInd = left; for (int i = left; i <= right; ++i) { if (arr[i] > arr[maxInd]) { maxInd = i; } } ans[maxInd] = depth; solvedd(arr, left, maxInd - 1, ans, depth + 1); solvedd(arr, maxInd + 1, right, ans, depth + 1); } private static void solved(List<List<Integer>> g, int node, int[][] dp, int last, int[] a) { int donttake = 0; int take = 0; for (int i = 0; i < g.get(node).size(); ++i) { int ntb = g.get(node).get(i); if (ntb != last) { solved(g, ntb, dp, node, a); donttake += Math.max(dp[ntb][0], dp[ntb][1]); take += dp[ntb][1]; } } dp[node][0] = a[node] + take; dp[node][1] = donttake; } private static boolean solve(int n, List<Integer> nums, int cur, int pos, Boolean[][] dp) { if (cur > n) return false; if (cur == n) return true; if (pos >= nums.size()) return false; if (dp[cur][pos] != null) { return dp[cur][pos]; } boolean without = solve(n, nums, cur, pos + 1, dp); boolean with = false; int ogcur = cur; for (int i = 1; i < 12; ++i) { with |= solve(n, nums, cur + nums.get(pos), pos + 1, dp); cur += nums.get(pos); } return dp[ogcur][pos] = with | without; } // private static Pair22 dfss(List<LinkedHashSet<Integer>> g, int node, HashSet<Integer> vis, int[] dis, int[] dis2) { // if (vis.contains(node)) return new Pair22(dis[node], dis2[node], -1); // vis.add(node); // int min = dis[node]; // for (Integer ntb : g.get(node)) { // if (dis[ntb] > dis[node]) // dfss(g, ntb, vis, dis, dis2); // if (dis[ntb] <= dis[node]) { // min = Math.min(min, dis[ntb]); // } else { // min = Math.min(min, dis2[ntb]); // } // } // // if (dis) // dis2[node] = min; // return new Pair22(dis[node], min, -1); // } private static int dfs(HashMap<Pair, TreeSet<Pair>> grid, int x, int y, int ti, HashSet<Pair> vis, int r, int startX, int startY) { // System.out.println(x + " " + y); int taken = ti - Math.abs(startX - x) - Math.abs(startY - y); if (taken < 0) return 0; if (x < 0 || y < 0 || x > r || y > r) return 0; if (vis.contains(new Pair(x, y))) return 0; int max = 0; if (grid.containsKey(new Pair(x, y))) { TreeSet<Pair> times = grid.get(new Pair(x, y)); for (Pair t : times) { if (t.a <= taken) { max = Math.max(t.b, max); } else break; } } vis.add(new Pair(x, y)); max = Math.max(dfs(grid, x + 1, y, ti, vis, r, startX, startY), max); max = Math.max(dfs(grid, x, y + 1, ti, vis, r, startX, startY), max); max = Math.max(dfs(grid, x - 1, y, ti, vis, r, startX, startY), max); max = Math.max(dfs(grid, x, y - 1, ti, vis, r, startX, startY), max); return max; } private static int solver(int[] nums, int pos, int[] dp) { if (pos >= nums.length) return 0; if (dp[pos] != Integer.MAX_VALUE) return dp[pos]; int min = solver(nums, pos + 2, dp) + nums[pos]; min = Math.min(solver(nums, pos + 3, dp) + nums[pos], min); if (pos + 1 < nums.length) min = Math.min(min, nums[pos] + nums[pos + 1] + solver(nums, pos + 3, dp)); if (pos + 1 < nums.length) min = Math.min(min, nums[pos] + nums[pos + 1] + solver(nums, pos + 4, dp)); // System.out.println(pos + " " + min); return dp[pos] = min; } static int countFreq(String pattern, String text) { int m = pattern.length(); int n = text.length(); int res = 0; for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == m) { res++; j = 0; } } return res; } private static void dfsR(List<List<Integer>> g, int node, int[] v) { rIn[node] = counter; flatten[counter++] = v[node]; for (int i = 0; i < g.get(node).size(); ++i) { dfsR(g, g.get(node).get(i), v); } rOut[node] = counter; flatten[counter++] = v[node] * -1; } private static void dfsL(List<List<Integer>> g, int node, int[] v) { lIn[node] = counter; lFlatten[counter++] = v[node]; for (int i = 0; i < g.get(node).size(); ++i) { dfsL(g, g.get(node).get(i), v); } lOut[node] = counter; lFlatten[counter++] = v[node] * -1; TreeMap<String, Integer> map = new TreeMap<>(); } private static void preprocess(int pos, int[][] pre, List<List<Integer>> tree, int[] traverse, int depth, int last, int[] tin, int[] tout) { tin[pos] = counter++; traverse[depth] = pos; for (int i = 0; depth - (1 << i) >= 0; ++i) { pre[pos][i] = traverse[depth - (1 << i)]; } for (int i = 0; i < tree.get(pos).size(); ++i) { if (tree.get(pos).get(i) != last) preprocess(tree.get(pos).get(i), pre, tree, traverse, depth + 1, pos, tin, tout); } tout[pos] = counter++; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static boolean submit = true; static void debug(String s) { if (!submit) System.out.println(s); } static void debug(int s) { LinkedHashSet<Integer> exist = new LinkedHashSet<>(); /* 4 2 3 _ 2 4 3 _ */ } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
8599550cf7d5b990a4683091b6002dcb
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class _703 { static MyScanner sc; public static void main(String[] args) { sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = 1; while (t-- > 0) { int n = sc.nextInt(); if (n == 2) { int pos = query(1, n); System.out.println("! " + (3 - pos)); return; } int pos = query(1, n); int left = -1; if (pos > 1) left = query(1, pos); if (left == pos) { int l = 1; int r = pos - 1; while (l < r) { int mid = (l + r) / 2; if (query(pos - mid, pos) != pos) { l = mid + 1; } else { r = mid; } } System.out.println("! " + (pos - l)); } else { int l = 1; int r = n - pos; while (l < r) { int mid = (l + r) / 2; if (query(pos, pos + mid) != pos) { l = mid + 1; } else { r = mid; } } System.out.println("! " + (pos + l)); } } out.close(); } static int query(int l, int r) { System.out.println("? " + l + " " + r); System.out.flush(); return sc.nextInt(); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } //-----------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
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
7952c6e4cf0cdeb2b139b3d506063c6f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
//package round703; import java.io.*; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Queue; public class C3 { InputStream is; FastWriter out; String INPUT = ""; // Random gen = new Random(99999); // // public static int[] shuffle(int n, Random gen){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = i; for(int i = 0;i < n;i++){ int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } // // int[] a = shuffle(1145, gen); // int get(int l, int r) { out.println("? " + (l+1) + " " + (r+1-1)); out.flush(); // assert l < r; // int[] t = Arrays.copyOfRange(a, l, r); // Arrays.sort(t); // for(int i = l;i < r;i++){ // if(a[i] == t[t.length-2]){ // return i; // } // } // return -1; return ni()-1; } void solve() { int n = ni(); int l = 0, r = n; int sec = get(l, r); while(r-l > 3) { int h = l + r >> 1; if (sec < h) { int nsec = get(Math.min(sec, l), Math.max(sec+1, h)); if (sec == nsec) { r = h; } else { l = h; } } else { int nsec = get(Math.min(sec, h), Math.max(sec+1, r)); if (sec == nsec) { l = h; } else { r = h; } } } while(r-l > 1){ int k = get(l, r); if(l == k){ l++; }else if(r-1 == k){ r--; }else{ int t = get(l+1, r); if(k == t){ l = l+2; }else{ r = l+1; } } } assert r-l == 1; out.println("! " + (l+1)); out.flush(); // tr(a); // assert a[l] == n-1; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C3().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 11
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
531884c5c46b98a4d5802f0a45f28c03
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.io.*; import java.text.*; import java.util.*; import java.util.function.*; /** * Provide prove of correctness before implementation. Implementation can cost a lot of time. * Anti test that prove that it's wrong. * <p> * Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation * <p> * Will program ever exceed limit? * Try all approaches with prove of correctness if task is not obvious. * If you are given formula/rule: Try to play with it. * Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data * Number theory * Game theory (optimal play) that consider local and global strategy. * Start writing the hardest code first */ public class Stage001_050_CF1486_C1_C2 { final boolean ONLINE_JUDGE = java.lang.System.getProperty("ONLINE_JUDGE") != null; final boolean ANTI_TEST_FINDER_MODE = false; final Random r = new Random(42); Interactor interactor = ONLINE_JUDGE ? new CFInteractor() : new MockInteractor(); //log2 (10^5) + 1 private int solveOne(int testCase, int n) { interactor.setUp(n); //[..........second.........first...........] int secondIndex = interactor.getSecondMaximum(1, n); int len = upperBound(0, n, delta -> { int l = Math.max(1, secondIndex - delta); int r = Math.min(secondIndex + delta, n); return secondIndex != interactor.getSecondMaximum(l, r); } ); //[ 3 2 1 secondInd 1 2 3 n - 1] // ^ ^ int l = Math.max(1, secondIndex - len) - 1; int r = Math.min(secondIndex + len, n) + 1; if (1 <= l) { if(interactor.getSecondMaximum(l, secondIndex) == secondIndex){ interactor.printMaximumIndex(l); } else { interactor.printMaximumIndex(r); } } else if(r <= n) { if(interactor.getSecondMaximum(secondIndex, r) == secondIndex){ interactor.printMaximumIndex(r); } else interactor.printMaximumIndex(l); }// else { // throw new RuntimeException("wtf"); // } return 0; } interface Interactor { void setUp(int n); void printMaximumIndex(int i); int getSecondMaximum(int l, int r); } class CFInteractor implements Interactor { public void setUp(int n) { } public void printMaximumIndex(int i) { System.out.printf("! %d\n", i); System.out.flush(); } public int getSecondMaximum(int l, int r) { System.out.printf("? %d %d\n", l, r); System.out.flush(); return nextInt(); } } class MockInteractor implements Interactor { int[] a; int n; int maxInd; int tries; //Random r = new Random(); public void setUp(int n) { tries = 0; a = new int[n + 1]; this.n = n; // for (int i = 1; i <= n; i++) { // a[i] = nextInt(); // } for (int i = 1; i <= n; i++) { a[i] = i; } for (int i = 1; i <= n; i++) { int j = 1 + r.nextInt(n); int temp = a[i]; a[i] = a[j]; a[j] = temp; } int max = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { if (a[i] > max) { max = a[i]; maxInd = i; } } System.out.println(Arrays.toString(a)); //maxInd = getSecondMaximum(1, n); } public void printMaximumIndex(int i) { if (i != maxInd) { throw new RuntimeException(); } else { System.out.printf("OK %d\n", i); } } public int getSecondMaximum(int l, int r) { if(tries + 1 > 20) { throw new RuntimeException("LIMIT"); } tries++; int max = Integer.MIN_VALUE; //int lMaxInd = -1; for (int i = l; i <= r; i++) { if (a[i] > max) { max = a[i]; // lMaxInd = i; } } int max_ = Integer.MIN_VALUE; int lMaxInd_ = -1; for (int i = l; i <= r; i++) { if (a[i] > max_ && a[i] != max) { max_ = a[i]; lMaxInd_ = i; } } return lMaxInd_; } } private int upperBound(int inclusiveLeft, int exclusiveRight, Predicate<Integer> predicate) { while (exclusiveRight - inclusiveLeft > 1) { int middle = inclusiveLeft + (exclusiveRight - inclusiveLeft) / 2; if (predicate.test(middle)) { inclusiveLeft = middle; } else { exclusiveRight = middle; } } return inclusiveLeft; } private int lowerBound(int exclusiveLeft, int inclusiveRight, Predicate<Integer> predicate) { while (inclusiveRight - exclusiveLeft > 1) { int middle = exclusiveLeft + (inclusiveRight - exclusiveLeft) / 2; if (predicate.test(middle)) { inclusiveRight = middle; } else { exclusiveLeft = middle; } } return inclusiveRight; } private int solveOneNaive(int testCase) { return 0; } private void solve() { if (ANTI_TEST_FINDER_MODE) { int t = 100_000; for (int testCase = 0; testCase < t; testCase++) { // int expected = solveOneNaive(testCase); int actual = solveOne(testCase, 10); // if (expected != actual) { // throw new AssertionRuntimeException( // this.getClass().getSimpleName(), // testCase, // expected, // actual); // } } } else { int t = 1;//nextInt(); for (int testCase = 0; testCase < t; testCase++) { int n = nextInt(); solveOne(testCase, n); } } } class AssertionRuntimeException extends RuntimeException { AssertionRuntimeException(String testName, int testCase, Object expected, Object actual, Object... input) { super("Testcase: " + testCase + "\n expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input)); } } private void assertThat(boolean b) { if (!b) throw new RuntimeException(); } private void assertThat(boolean b, String s) { if (!b) throw new RuntimeException(s); } private int assertThatInt(long a) { assertThat(Integer.MIN_VALUE <= a && a <= Integer.MAX_VALUE, "Integer overflow long = [" + a + "]" + " int = [" + (int) a + "]"); return (int) a; } void _______debug(String str, Object... os) { if (!ONLINE_JUDGE) { System.out.println(MessageFormat.format(str, os)); System.out.flush(); } } void _______debug(Object o) { if (!ONLINE_JUDGE) { _______debug("{0}", String.valueOf(o)); } } private int nextInt() { return System.in.readInt(); } private long nextLong() { return System.in.readLong(); } private String nextString() { return System.in.readString(); } private int[] nextIntArr(int n) { return System.in.readIntArray(n); } private long[] nextLongArr(int n) { return System.in.readLongArray(n); } public static void main(String[] args) { new Stage001_050_CF1486_C1_C2().run(); } static class System { private static FastInputStream in; private static FastPrintStream out; } private void run() { // final long startTime = java.lang.System.currentTimeMillis(); final boolean USE_IO = ONLINE_JUDGE; if (USE_IO) { System.in = new FastInputStream(java.lang.System.in); System.out = new FastPrintStream(java.lang.System.out); solve(); System.out.flush(); } else { final String nameIn = "input.txt"; final String nameOut = "output.txt"; try { System.in = new FastInputStream(new FileInputStream(nameIn)); System.out = new FastPrintStream(new PrintStream(nameOut)); solve(); System.out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } } // final long endTime = java.lang.System.currentTimeMillis(); // _______debug("Execution time: {0}", endTime - startTime); } private static class FastPrintStream { private static final int BUF_SIZE = 8192; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastPrintStream() { this(java.lang.System.out); } public FastPrintStream(OutputStream os) { this.out = os; } public FastPrintStream(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastPrintStream print(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerFlush(); return this; } public FastPrintStream print(char c) { return print((byte) c); } public FastPrintStream print(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerFlush(); } return this; } public FastPrintStream print(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerFlush(); }); return this; } //can be optimized public FastPrintStream print0(char[] s) { if (ptr + s.length < BUF_SIZE) { for (char c : s) { buf[ptr++] = (byte) c; } } else { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerFlush(); } } return this; } //can be optimized public FastPrintStream print0(String s) { if (ptr + s.length() < BUF_SIZE) { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); } } else { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); if (ptr == BUF_SIZE) innerFlush(); } } return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastPrintStream print(int x) { if (x == Integer.MIN_VALUE) { return print((long) x); } if (ptr + 12 >= BUF_SIZE) innerFlush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastPrintStream print(long x) { if (x == Long.MIN_VALUE) { return print("" + x); } if (ptr + 21 >= BUF_SIZE) innerFlush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastPrintStream print(double x, int precision) { if (x < 0) { print('-'); x = -x; } x += Math.pow(10, -precision) / 2; print((long) x).print("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; print((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(String x) { return print(x).println(); } public FastPrintStream println(Object x) { return print(x.toString()).println(); } public FastPrintStream println(double x, int precision) { return print(x, precision).println(); } public FastPrintStream println() { return print((byte) '\n'); } public FastPrintStream printf(String format, Object... args) { return print(String.format(format, args)); } private void innerFlush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerFlush"); } } public void flush() { innerFlush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } } private static class FastInputStream { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastInputStream(InputStream stream) { this.stream = stream; } public double[] readDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = readDouble(); } return array; } public String[] readStringArray(int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) { array[i] = readString(); } return array; } public char[] readCharArray(int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) { array[i] = readCharacter(); } return array; } public String readText() { StringBuilder result = new StringBuilder(); while (true) { int character = read(); if (character == '\r') { continue; } if (character == -1) { break; } result.append((char) character); } return result.toString(); } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } 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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int peekNonWhitespace() { while (isWhitespace(peek())) { read(); } return peek(); } 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 long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public char[] readStringAsCharArray() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); char[] resArr = new char[res.length()]; res.getChars(0, res.length(), resArr, 0); return resArr; } 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; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double readDouble() { 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, readInt()); } 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, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return readString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 17
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
fc26fe014937400329f842068f00de2f
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
256 megabytes
import java.util.*; import java.io.*; public class GuessingTheGreatest_Hard { public static void main(String[] args) throws IOException { Soumit sc = new Soumit(); int n = sc.nextInt(); System.out.println("? 1 "+n); System.out.flush(); int smax_ind = sc.nextInt(); if(n == 2){ if(smax_ind == 1){ System.out.println("! 2"); } else{ System.out.println("! 1"); } System.exit(0); } if(smax_ind == 1){ int l = smax_ind + 1; int r = n; while(l < r){ int mid = (l + r) / 2; System.out.println("? "+smax_ind+" "+mid); System.out.flush(); int ind = sc.nextInt(); if(ind == smax_ind){ r = mid; } else{ l = mid + 1; } } System.out.println("! "+l); System.exit(0); } System.out.println("? 1 "+smax_ind); System.out.flush(); int ind = sc.nextInt(); int l, r; if(ind == smax_ind){ l = 1; r = smax_ind - 1; while(l < r){ int mid = (l + r + 1) / 2; System.out.println("? "+mid+" "+smax_ind); System.out.flush(); ind = sc.nextInt(); if(ind == smax_ind){ l = mid; } else{ r = mid - 1; } } } else{ l = smax_ind + 1; r = n; while(l < r){ int mid = (l + r) / 2; System.out.println("? "+smax_ind+" "+mid); System.out.flush(); ind = sc.nextInt(); if(ind == smax_ind){ r = mid; } else{ l = mid + 1; } } } System.out.println("! "+l); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { /*if (din == null) return;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 17
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
a1d75aa294c45ea70fdd7c04b9e2ba8d
train_109.jsonl
1613658900
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
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.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); C2GuessingTheGreatestHardVersion solver = new C2GuessingTheGreatestHardVersion(); solver.solve(1, in, out); out.close(); } static class C2GuessingTheGreatestHardVersion { InputReader in; OutputWriter out; int n; public void solve(int testNumber, InputReader in, OutputWriter out) { this.in = in; this.out = out; n = in.nextInt(); int x = ask(1, n); int l = 1; int r = n; boolean f = false; if (x == 1) { l = 2; f = false; } else if (x == n) { r = n - 1; f = true; } else { int res = ask(1, x); if (res == x) { r = x - 1; f = true; } else { l = x + 1; f = false; } } if (f) { while (l < r) { int mid = l + r + 1 >> 1; int res = ask(mid, x); if (res == x) { l = mid; } else { r = mid - 1; } } } else { while (l < r) { int mid = l + r >> 1; int res = ask(mid, x); if (res == x) { r = mid; } else { l = mid + 1; } } } out.println("!", l); out.flush(); } private int ask(int l, int r) { if (l > r) { int t = l; l = r; r = t; } out.println("?", l, r); out.flush(); return in.nextInt(); } } 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 UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int 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 UnknownError(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
Java
["5\n\n3\n\n4"]
1 second
["? 1 5\n\n? 4 5\n\n! 1"]
NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
Java 17
standard input
[ "binary search", "interactive" ]
eb660c470760117dfd0b95acc10eee3b
The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.
1,900
null
standard output
PASSED
ae9ff591d0fb5557ed0c72806ba11f42
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class A implements Runnable { public static void main(String args[]) throws Exception { new Thread(null, new A(), "A", 1 << 27).start(); } void merge1(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++; } } void sort1(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort1(arr, l, m); sort1(arr, m + 1, r); merge1(arr, l, m, r); } } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.ni(); while (t-- > 0) { int n = in.ni(); int[] p = in.nextIntArray(n); double sum = 0L; int prev = p[0]; boolean failed = false; for (int i = 0; i < p.length; i++) { prev = p[i]; sum += prev; if (sum < ((double) (i * (i + 1)) / 2)) { failed = true; break; } } if (!failed) { out.println("YES"); } else { out.println("NO"); } } out.close(); } catch (Exception e) { e.printStackTrace(); //throw new RuntimeException(); } } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f = fi; s = se; } public int compareTo(Object o)//asc order { pair pr = (pair) o; if (s > pr.s) return 1; if (s == pr.s) { if (f > pr.f) return 1; else return -1; } else return -1; } public boolean equals(Object o) { pair ob = (pair) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s).hashCode(); } } 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; } } public class triplet implements Comparable { int f; int s; long t; triplet(int f, int s, long t) { this.f = f; this.s = s; this.t = t; } public boolean equals(Object o) { triplet ob = (triplet) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s) && (ob.t == this.t)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s + " " + this.t).hashCode(); } public int compareTo(Object o)//desc order { triplet tr = (triplet) o; if (t > tr.t) return -1; else if (t == tr.t) { if (f > tr.f) return 1; else return -1; } else return 1; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
66bd4e558b5daa485dfebfad79c429ce
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class A implements Runnable { public static void main(String args[]) throws Exception { new Thread(null, new A(), "A", 1 << 27).start(); } void merge1(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++; } } void sort1(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort1(arr, l, m); sort1(arr, m + 1, r); merge1(arr, l, m, r); } } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.ni(); while (t-- > 0) { int n = in.ni(); int[] p = in.nextIntArray(n); double sum = 0L; boolean failed = false; int iSum = 0; for (int i = 0; i < p.length; i++) { sum += p[i]; iSum = (i*(i+1))/2; if (sum < iSum) { failed = true; break; } } if (failed) { out.println("NO"); } else { out.println("YES"); } } out.close(); } catch (Exception e) { e.printStackTrace(); //throw new RuntimeException(); } } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f = fi; s = se; } public int compareTo(Object o)//asc order { pair pr = (pair) o; if (s > pr.s) return 1; if (s == pr.s) { if (f > pr.f) return 1; else return -1; } else return -1; } public boolean equals(Object o) { pair ob = (pair) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s).hashCode(); } } 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; } } public class triplet implements Comparable { int f; int s; long t; triplet(int f, int s, long t) { this.f = f; this.s = s; this.t = t; } public boolean equals(Object o) { triplet ob = (triplet) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s) && (ob.t == this.t)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s + " " + this.t).hashCode(); } public int compareTo(Object o)//desc order { triplet tr = (triplet) o; if (t > tr.t) return -1; else if (t == tr.t) { if (f > tr.f) return 1; else return -1; } else return 1; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
fa4fdb57b971943341c616ee53b7f1af
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class A implements Runnable { public static void main(String args[]) throws Exception { new Thread(null, new A(), "A", 1 << 27).start(); } void merge1(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++; } } void sort1(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort1(arr, l, m); sort1(arr, m + 1, r); merge1(arr, l, m, r); } } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.ni(); while (t-- > 0) { int n = in.ni(); int[] p = in.nextIntArray(n); double sum = 0L; boolean failed = false; int iSum = 0; for (int i = 0; i < p.length; i++) { sum += p[i]; iSum += i; if (sum < iSum) { failed = true; break; } } if (failed) { out.println("NO"); } else { out.println("YES"); } } out.close(); } catch (Exception e) { e.printStackTrace(); //throw new RuntimeException(); } } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f = fi; s = se; } public int compareTo(Object o)//asc order { pair pr = (pair) o; if (s > pr.s) return 1; if (s == pr.s) { if (f > pr.f) return 1; else return -1; } else return -1; } public boolean equals(Object o) { pair ob = (pair) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s).hashCode(); } } 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; } } public class triplet implements Comparable { int f; int s; long t; triplet(int f, int s, long t) { this.f = f; this.s = s; this.t = t; } public boolean equals(Object o) { triplet ob = (triplet) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s) && (ob.t == this.t)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s + " " + this.t).hashCode(); } public int compareTo(Object o)//desc order { triplet tr = (triplet) o; if (t > tr.t) return -1; else if (t == tr.t) { if (f > tr.f) return 1; else return -1; } else return 1; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
f974abf4d2b2cc0189cf5c186b2c17e4
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(--t>=0) { int k=0; int i=0; long m=0; int n=sc.nextInt(); long a[]=new long[n]; for(i=0;i<n;i++) a[i]=sc.nextLong(); for(i=0;i<n-1;i++) { m=a[i]-(k++); if(m<0)break; a[i]=i; a[i+1]=a[i+1]+m; } if(n==1){ System.out.println("yes"); continue; } if(n>1&&a[i]>a[i-1]&&m>=0) System.out.println("yes"); else System.out.println("no"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
a0454c6bf5770d944f3f2c920414978b
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
/*Author Adityaraj*/ import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Template { // initialize variable public static void main(String[] args) throws IOException { long start = System.nanoTime(); // Initialize the reader FastScanner scan = new FastScanner(); // Initialize the writer FastOutput out = new FastOutput(); /**********************************************************************************************************************************/ // writer your code here int t = scan.readInt(); while(t-->0){ int n= scan.readInt(); int[] arr =scan.readIntArray(n); long sum = 0L; long j =0L; boolean flag = true; for (int i = 0; i < n; i++) { sum+=arr[i]; j+=i; if(sum<j){ out.writeString("NO"); flag =false; break; } } if(flag) out.writeString("YES"); } // your code end here /**********************************************************************************************************************************/ // compute the time elapsed if (System.getProperty("ONLINE_JUDGE") == null) { try { long end = System.nanoTime(); System.out.print((end - start) / 1000000 + " ms"); System.exit(0); } catch (Exception exception) { } } } /**************************************************************************************************************************************/ /**************************************************************************************************************************************/ // Function public static boolean palindrome(String str){ StringBuffer s = new StringBuffer(str); if(str.equals(String.valueOf(s.reverse()))){ return true; } return false; } /**************************************************************************************************************************************/ // this function will the gcd of two numbers public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } // this will return the pow of a^b public static long binexp(long a, long b) { long res = 1; while (b != 0) { if (b % 2 != 0) res *= a; a *= a; b /= 2; } return res; } // this will return true if a is prime and false if not public static boolean primeornot(long a) { for (int i = 2; i * i <= a; i++) { if (a % i == 0) { // System.out.println(i); return false; } } return true; } // this funciton will count the number of bit in a binary representation of a // number public static int countbit(Long n) { int count = 0; while (n > 0) { count++; n &= n - 1; } return count; } // this method check that a number is a perfect square or not public static boolean perfectsquare(long n) { if (n >= 0) { int sr = (int) Math.sqrt(n); return sr * sr == n; } return false; } /*************************************************************************************************************************/ /*************************************************************************************************************************/ // pair class of Pair<Integer, Integer> // like list,queue,etc;(Integer,Integer) // Pair class of Generic type static class Pair<A, B> { A first; B second; Pair(A first, B second) { this.first = first; this.second = second; } } /********************************************************************* */ // Fast Reader Class public static class FastScanner { // Reader object BufferedReader reader; public int[] intarr; public Long[] longarr; public Float[] floatarr; public Double[] doublearr; // Constructor public FastScanner() { // Initialize the reader reader = new BufferedReader(new InputStreamReader(System.in)); if (System.getProperty("ONLINE_JUDGE") == null) { try { reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } catch (Exception e) { } } } // String tokenizer StringTokenizer tokenizer; // Function to read a // single integer // to extend the fast reader class writer your function here public int readInt() throws IOException { return Integer.parseInt(reader.readLine()); } // Function to read a // single long public long readLong() throws IOException { return Long.parseLong(reader.readLine()); } // Function to read string public String readString() throws IOException { return reader.readLine(); } // Function to read a array // of numsInts integers // in one line public int[] readIntArray(int n) throws IOException { intarr = new int[n]; tokenizer = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; i++) { intarr[i] = Integer.parseInt(tokenizer.nextToken()); } return intarr; } public Float[] readfloatArray(int n) throws IOException { floatarr = new Float[n]; tokenizer = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; i++) { floatarr[i] = Float.parseFloat(tokenizer.nextToken()); } return floatarr; } public Double[] readDoubleArray(int n) throws IOException { doublearr = new Double[n]; tokenizer = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; i++) { doublearr[i] = Double.parseDouble(tokenizer.nextToken()); } return doublearr; } public Long[] readLongArray(int n) throws IOException { tokenizer = new StringTokenizer(reader.readLine()); longarr = new Long[n]; int i = 0; while (tokenizer.hasMoreTokens()) { longarr[i] = Long.parseLong(tokenizer.nextToken()); i++; } return longarr; } public List<Integer> readIntAsList() throws IOException { List<Integer> list = new ArrayList<Integer>(); tokenizer = new StringTokenizer(reader.readLine()); while (tokenizer.hasMoreTokens()) { list.add(Integer.parseInt(tokenizer.nextToken())); } return list; } public List<Long> readLongAsList() throws IOException { List<Long> list = new ArrayList<Long>(); tokenizer = new StringTokenizer(reader.readLine()); while (tokenizer.hasMoreTokens()) { list.add(Long.parseLong(tokenizer.nextToken())); } return list; } } // Fast Writer Class public static class FastOutput { // Writer object BufferedWriter writer; // Constructor public FastOutput() { // Initialize the writer writer = new BufferedWriter(new OutputStreamWriter(System.out)); if (System.getProperty("ONLINE_JUDGE") == null) { try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt"))); } catch (Exception e) { } } } // Function to write the // single integer public void writeInt(int i) throws IOException { writer.write(Integer.toString(i)); writer.newLine(); writer.flush(); } // Function to writer a single character public void writeChar(char c) throws IOException { writer.write(Character.toString(c)); writer.newLine(); writer.flush(); } // Function to write single long public void writeLong(long i) throws IOException { writer.write(Long.toString(i)); writer.newLine(); writer.flush(); } // Function to write String public void writeString(String s) throws IOException { writer.write(s); writer.newLine(); writer.flush(); } public void writeStringWithSpace(String s) throws IOException { writer.write(s); writer.write(" "); writer.flush(); } // Function to write a Integer of // array with spaces in one line public void writeIntArray(int[] arr) throws IOException { for (int i = 0; i < arr.length; i++) { writer.write(arr[i] + " "); } writer.newLine(); writer.flush(); } // Function to write Integer of // array without spaces in 1 line public void writeIntArrayWithoutSpaces(int[] arr) throws IOException { for (int i = 0; i < arr.length; i++) { writer.write(Integer.toString(arr[i])); } writer.newLine(); writer.flush(); } public void writeIntegerlist(List<Integer> list) throws IOException { if (list != null) { for (Integer integer : list) { writer.write(integer + " "); } } writer.newLine(); writer.flush(); } public void writeintmatrix(int[][] matrix) throws IOException { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { writer.write(matrix[i][j] + " "); } writer.newLine(); } writer.flush(); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
f5999a2860e9aae93c3d179cc8eaf63b
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in); } } private static void solve(Scanner in) { int n = in.nextInt(); long extra = 0; boolean fail = false; for (int i = 0; i < n; i++) { int num = in.nextInt(); extra += num - i; if (extra < 0) { fail = true; } } System.out.println(fail ? "NO" : "YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
dc6719784dd475cc8af4f5f835f40e7d
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); for (int i = 0; i < x; i++) { int y = scan.nextInt(); int[] arr = new int[y]; for (int j = 0; j < y; j++) { arr[j] = scan.nextInt(); } boolean ye = true; long free = 0; for (int j = 0; j < y; j++) { if (free < 0) { ye = false; break; } free += arr[j] - j; } if (free >= 0 && ye) System.out.println("YES"); else System.out.println("NO"); } scan.close(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
909a66459d239bac41d3b95ecbcd0d3c
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); for (int i = 0; i < x; i++) { int y = scan.nextInt(); int[] arr = new int[y]; for (int j = 0; j < y; j++) { arr[j] = scan.nextInt(); } boolean ye = true; long free = 0; for (int j = 0; j < y-1; j++) { free += arr[j] - j; if (free < 0 || j+1 > arr[j+1] + free) { ye = false; break; } } if (free >= 0 && ye) System.out.println("YES"); else System.out.println("NO"); } scan.close(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
0778ea44cc0f68a11023fa20389241a6
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //p sure this will owrk this time, im j double chekcing int x = scan.nextInt(); for (int i = 0; i < x; i++) { int y = scan.nextInt(); int[] arr = new int[y]; for (int j = 0; j < y; j++) { arr[j] = scan.nextInt(); } boolean ye = true; long free = 0; for (int j = 0; j < y; j++) { free += arr[j] - j; if (free < 0) { ye = false; break; } } if (ye) System.out.println("YES"); else System.out.println("NO"); } scan.close(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
ebd0cfbf6f813bb3c58c5b9d5825d8d3
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class shift { public static void main(String args[]) { Scanner in=new Scanner(System.in); long n,i,t,f; int j; long a[]=new long[100]; t=in.nextLong(); for(i=0;i<t;i++) { n=in.nextLong(); a[0]=in.nextLong(); f=1; for(j=0;j<n-1;j++) { a[j+1]=in.nextLong(); } for(j=0;j<n-1;j++) { if(a[j]<j) { f=0; break; } if(a[j]>j) a[j+1]=a[j+1]+(a[j]-j); } j=(int)n-1; if(a[j]<j) f=0; if(f==0) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
be2fe698b9ed4d4623deab5961c4542e
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class shift { public static void main(String args[]) { Scanner in=new Scanner(System.in); long n,i,t,f; int j; long a[]=new long[100]; t=in.nextLong(); for(i=0;i<t;i++) { n=in.nextLong(); for(j=0;j<n;j++) { a[j]=in.nextLong(); } f=1; for(j=0;j<n-1;j++) { if(a[j]>j) a[j+1]=a[j+1]+(a[j]-j); if(a[j]<j) { f=0; break; } } j=(int)n-1; if(a[j]<j) f=0; if(f==0) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
cb641b89f7c9f1f445061262c4840f9f
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ShiftingStacks { //1486A public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); int a[] = new int[n]; StringTokenizer st = new StringTokenizer(br.readLine() , " "); for(int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); sb.append(solve(n, a)).append("\n"); } System.out.println(sb); } private static String solve(int n, int[] a) { long r = 0; for(int i = 0; i < n; i++) { r += (a[i] - i); if(r < 0) return "NO"; } return "YES"; } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
6e5b5910206a6b520b530cfe1320cfc0
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
//package letsgo.c703; //import DataStructures.Linkedlist; import java.util.Arrays; import java.util.Scanner; public class p1 { public static boolean func ( Long [] arr ) { Long Sum[] = new Long[arr.length]; Sum[0]=arr[0]; for (int i = 1; i < arr.length; i++) { Sum[i] = Sum[i - 1] + arr[i]; } // System.out.println(Arrays.toString(Sum)); for (int i = 1; i < arr.length; i++) { if(Sum[i]<(i*(i+1))/2){ return false ; } } return true ; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt() ; for (int i = 0; i < test ; i++) { int rt = sc.nextInt(); Long [ ] arr = new Long[rt]; for (int j = 0; j <rt ; j++) { arr[j]=sc.nextLong(); } if ( func( arr)){ System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
b25007fec317e4f8550fcdb633b08be9
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class ShiftingStacks { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int num = 0; long sumNum = 0; long sumT; for(int j = 0; j < n; j++) { num = input.nextInt(); sumNum = 0; sumT = 0; boolean check = true; long[] m = new long[num]; for(int i = 0; i < num; i++) { m[i] = input.nextLong(); } for(int i = 0; i < num - 1; i++) { if(m[i] >= (long)(i)) { m[i + 1] += m[i] - (long)(i); } else { check = false; break; } } if(!check) { System.out.println("NO"); } else { if(m[num - 1] >= num - 1) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
6702e13818d8f9c83498633c3baa5830
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.io.*; public class UVa { static class FastIO { InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } public int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } public long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } public byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } public String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } public static void main(String[] args) throws Exception{ FastIO r = new FastIO(System.in); int t = r.nextInt(); while(t-- > 0){ int n = r.nextInt(); long[] arr = new long[101]; long[] s = new long[101]; for(int i = 1; i <= n; i++){ s[i] = r.nextLong(); arr[i] = arr[i - 1] + s[i]; } long sum = 0; boolean bad = false; for(int i = 1; i <= n; i++){ sum += (i - 1); if(arr[i] < sum){ System.out.println("NO"); bad = true; break; } } if(!bad) System.out.println("YES"); } } } /* #include<bits/stdc++.h> using namespace std; int main(){ long long t,n,i,j,flag,sum,a[101],s[101]; cin>>t; for(j=1;j<=t;j++){ memset(a,0,sizeof(a)); cin>>n; for(i=1;i<=n;i++){ cin>>s[i]; a[i]=a[i-1]+s[i]; } flag=1,sum=0; for(i=1;i<=n;i++){ sum+=i-1; if(a[i]<sum) { printf("NO\n"); flag=0; break; } } if(flag!=0) printf("YES\n"); } return 0; } */
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
2d1ae5dc276a6e6946f388a232653549
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; public class coding { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try { String str = br.readLine(); if(str == null) throw new InputMismatchException(); st = new StringTokenizer(str); } 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(); } if(str == null) throw new InputMismatchException(); return str; } public BigInteger nextBigInteger() { // TODO Auto-generated method stub return null; } } public static <K, V> K getKey(Map<K, V> map, V value) { for (Map.Entry<K, V> entry: map.entrySet()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; } public static int median(ArrayList<Integer> x) { int p=0; if(x.size()==1) { p=x.get(0); } else { p=x.get((x.size()-1)/2 ); } return p; } public static void main(String args[] ) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; long s1=0; long s=0; boolean p=true; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); s1=s1+arr[i]; s=s+i; if(s>s1) { p=false; } } if(p==true) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
9ce34e88bef88fafbaa27d59c5a8a2b6
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class Round703 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T!=0) { int n = scanner.nextInt(); long cur_sum = 0, need = 0; boolean ok = true; for (int i = 0; i < n; i++) { long x = scanner.nextInt(); cur_sum += x; need += i; if (cur_sum < need) { ok = false; } } System.out.println(ok ? "YES\n" : "NO\n"); T--; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
c6f71a49db4668be40fca60993e0ac80
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { long t = nextLong(); for (int i = 0; i < t; i++) { int n = nextInt(); long sum =0; long need = 0; long[]a =new long[n]; for (int j = 0; j < n; j++) { a[j] = nextLong(); } boolean ans = true; for (int j = 0; j < n; j++) { sum+=a[j]; need+=j; if (sum < need){ ans = false; } } if (!ans){ out.println("NO"); }else{ out.println("YES"); } } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static boolean hasNext() throws IOException { if (in.hasMoreTokens()) return true; String s; while ((s = br.readLine()) != null) { in = new StringTokenizer(s); if (in.hasMoreTokens()) return true; } return false; } public static String nextToken() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
5e7e8f84dae85c63071880dd6b3d6c8a
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class ShiftingStacks { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int j = 0; j < n; j++) { int length = scanner.nextInt(); long temp1=0; long temp2=0; boolean state=true; int []nums=new int[length]; for (int i = 0; i < length; i++) { nums[i] = scanner.nextInt(); } for (int i = 0; i <length; i++) { temp2=nums[i]+temp2-temp1; if(i>0&&temp2>temp1){ temp1++; }else if(i==0){ continue; } else { state=false; } } if (state) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
78668fb6fce54384aa3fee493ad8ca7c
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class ShiftingStacks { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int j = 0; j < n; j++) { int length = scanner.nextInt(); long temp1=0; long temp2=0; boolean state=true; int []nums=new int[length]; for (int i = 0; i < length; i++) { nums[i] = scanner.nextInt(); } for (int i = 0; i <length; i++) { temp2=nums[i]+temp2-temp1; if(i>0&&temp2>temp1){ temp1++; }else if(i==0){} else { state=false; } } if (state) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
417a5c980aa68aa8b546bd881aa03cec
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Test2 { static Scanner sc; public static void main(String args[]) { sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { solve(); } } public static void solve() { int n = sc.nextInt(); long sum = 0, minsum = 0; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); for (int i = 0; i < n; i++) { sum += arr[i]; minsum += i; if (sum < minsum) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
b5754ca3c31bd9188a279eda5b43721f
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Test { static Scanner sc; public static void main(String args[]) { sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { solve(); } } public static void solve() { int n = sc.nextInt(); long sum = 0, minsum = 0; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); for (int i = 0; i < n; ++i) { sum += arr[i]; minsum += i; if (sum < minsum) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
7e82d0fa275090d9da8f737146174854
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Result { public static int t, n; public static long[] a; public static void main (String[] args) { Scanner sc = new Scanner(System.in); t = sc.nextInt(); while (t-- > 0) { n = sc.nextInt(); a = new long[n]; for (int i = 0; i < n; ++i) a[i] = sc.nextLong(); boolean ok = true; for (int i = 0; i < n; ++i) { if (a[i] < i) { ok = false; break; } if (i < n - 1) a[i + 1] += a[i] - i; } if (ok == true) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
00b39abefcf1b59e2474abc1c3b27389
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[] = sc.readArrayLong(n); boolean flag=false; long sum=0; for(int i=0;i<n;i++) { sum+=arr[i]; if(sum<(i*(i+1)/2)) { flag=true; break; } } if(flag==true) { System.out.println("NO"); } else { System.out.println("YES"); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(int arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if(b==0) { return a; } else { return gcdLong(b,a%b); } } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
50913be173764222f741464277c9beea
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class cf { public static void main(String args[]) { Scanner scan = new Scanner(System.in); long t, s = 0, a; int n; boolean ans; t = scan.nextLong(); for (int i=0; i<t; i++) { n = scan.nextInt(); s = 0; ans = true; long need = 0; for (int j=0; j<n; j++) { a = scan.nextLong(); s += a; need += j; if (s < need) { ans = false; } } if (ans) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
89c96046e31e031b713fa681cf469a85
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Cf{ static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int t=1; t=scan.nextInt(); while(t-->0){ solve(); } } static void solve(){ int N=scan.nextInt(); long a[]=new long[N+1]; for(int i=0;i<N;i++){ a[i]=scan.nextInt(); } for(int i=0;i<N;i++){ if(a[i]>=i){ a[i+1]=a[i+1]+a[i]-i; a[i]=i; }else{ System.out.println("NO"); return; } } // for(int i=0;i<N;i++){ // System.out.print(a[i]+" "); // } System.out.println("YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
2cedcb3e2fac8027c8962e200f20dd1a
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Cf{ static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int t=1; t=scan.nextInt(); while(t-->0){ solve(); } } static void solve(){ int N=scan.nextInt(); long a[]=new long[N+5]; for(int i=0;i<N;i++){ a[i]=scan.nextInt(); } for(int i=0;i<N;i++){ if(a[i]>=i){ a[i+1]=a[i+1]+a[i]-i; a[i]=i; }else{ System.out.println("NO"); return; } } // for(int i=0;i<N;i++){ // System.out.print(a[i]+" "); // } System.out.println("YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
9c024a75f2f4ca195f51ed4b9900eb86
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; public class codeforces { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t--!=0) { int n=Integer.parseInt(br.readLine()); String s[]=br.readLine().split(" "); long sum=0; boolean flag=true; long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=Long.parseLong(s[i]); sum+=a[i]; if(sum<i*(i+1)/2) { //System.out.println("sum: "+sum+" i:"+i); flag=false; break; } } n=n*(n-1)/2; if(flag) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
68b0d5bae66256f5ee0faa485703dd67
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; public class codeforces { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t--!=0) { int n=Integer.parseInt(br.readLine()); String s[]=br.readLine().split(" "); long sum=0; boolean flag=true; int a; me:for(int i=0;i<n;i++) { a=Integer.parseInt(s[i]); if(a>=i) { sum+=a-i; } else { if(i-a>sum) { flag=false; break me; } else { sum-=(i-a); } } } if(flag) { System.out.println("YES"); } else { System.out.println("NO"); } } br.close(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
cca3d416892c96b4e0a41afb9c0395a0
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; public class Main { static long TIME_START, TIME_END; public static class Task { public void solve(Scanner sc, PrintWriter pw) throws IOException { int testCase = sc.nextInt(); Outer: while(testCase-- > 0) { int n = sc.nextInt(); long[] arr = new long[n]; for(int i=0; i<n; i++) arr[i] = sc.nextLong();; long sum = 0L; long compare = 0L; for(int i=0; i<n; i++) { sum += arr[i]; compare += i; if(sum < compare) { System.out.println("NO"); continue Outer; } } System.out.println("YES"); } } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); //Scanner sc = new Scanner(new FileReader(new File("out/input.txt"))); //PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File("out/output.txt")))); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); //System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000); //System.err.println("Time used: " + (TIME_END - TIME_START) + "."); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
35d2749936dacfa436ef4f4e8dfb00d0
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int testCases = sc.nextInt(); for (int t = 0; t < testCases; t++) { int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } long has = arr[0]; boolean poss = true; for (int i = 1; i < n; i++) { int cur = arr[i]; if (cur + has < i) { poss = false; break; } has = has + cur - i; } if (n == 1) { sb.append("YES").append("\n"); continue; } if (poss) { sb.append("YES").append("\n"); continue; } sb.append("NO").append("\n"); } System.out.print(sb); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
d432c80ec0d3100635571d5808773979
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int testCases = sc.nextInt(); for (int t = 0; t < testCases; t++) { long n = sc.nextInt(); int arr[] = new int[(int) n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } if (n == 1) { sb.append("YES").append("\n"); continue; } long req = (n * (n - 1)) / 2; long has = 0; long cur_sum = 0; boolean poss = true; for (int i = 0; i < n; i++) { int cur = arr[i]; if (cur + cur_sum < i) { poss = false; } has = has + cur; if (cur >= i) { cur_sum = cur_sum + cur - i; continue; } cur_sum = cur_sum - (i - cur); } if (!poss) { sb.append("NO").append("\n"); continue; } if (has >= req) { sb.append("YES").append("\n"); continue; } sb.append("NO").append("\n"); } System.out.print(sb); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
47b03de26c63d901740859a1198184f6
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.io.*; public class Main { static InputReader scn = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) { // Running Number Of TestCases (t) int t = scn.nextInt(); while(t-- > 0) solve(); out.close(); } static long[] dp; public static void dummy(){ // Dummy Code for Rough List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(3, 4); out.println(list); } public static void solve() { // Main Solution (AC) int n = scn.nextInt(); long[] H = new long[n]; for(int i = 0; i < n; i++){ H[i] = scn.nextLong(); } if(n == 1) { out.println("YES"); return; } for(int i = 0; i + 1 < n; i++){ // out.println("i : " + i); if(H[i] == i){ continue; }else if(H[i] < i){ out.println("NO"); return; }else{ H[i+1] = H[i+1] + (H[i] - i); H[i] = i; } } if(H[n-2] >= H[n-1]){ out.println("NO"); return; } out.println("YES"); } public static long max(long a, long b, long c){ long v1 = Math.abs(a-b); long v2 = Math.abs(b-c); long v3 = Math.abs(a-c); return v1+v2+v3; } public static long DP(int[] arr, int n, int k){ if(n == 1) return arr[0]; long ans = 0; dp[0] = arr[0]; dp[1] = max(arr[0], arr[1]); for(int i = 2; i < n; i++){ int a = arr[i]; dp[i] = max(dp[i-1], dp[i]); dp[i] = max(dp[i], dp[i-2] + arr[i]); } ans = dp[n - 1]; return ans; } public static long max(long x, long y){ if(x > y) return x; return y; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){ return (o1.getValue()).compareTo(o2.getValue()); } }); HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static boolean isPowerOfTwo(long n){ if(n==0) return false; return (long)(Math.ceil((Math.log(n) / Math.log(2)))) == (long)(Math.floor(((Math.log(n) / Math.log(2))))); } public static void sortbyColumn(int[][] arr, int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static HashSet<Long> primeFactors(long n, HashSet<Long> list, int d) { list.add(1L); list.add(n); for (long i = 2; i <= Math.sqrt(n); i += 1) { long a = i; if (n % a == 0) { list.add(a); if (n % (n / a) == 0) list.add(n / a); } } return list; } public static HashMap<Integer, Integer> CountFrequencies(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { if (map.containsKey(i)) map.put(i, map.get(i) + 1); else map.put(i, 1); } return map; } public static void ArraySort2D(int[][] arr, int xy) { // xy == 0, for sorting wrt X-Axis // xy == 1, for sorting wrt Y-Axis Arrays.sort(arr, Comparator.comparingDouble(o -> o[xy])); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static int firstOccurence(int array1[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == 0 || x > array1[mid - 1]) && array1[mid] == x) return mid; else if (x > array1[mid]) return firstOccurence(array1, (mid + 1), high, x, n); else return firstOccurence(array1, low, (mid - 1), x, n); } return -1; } public static int lastOccurence(int array2[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < array2[mid + 1]) && array2[mid] == x) return mid; else if (x < array2[mid]) return lastOccurence(array2, low, (mid - 1), x, n); else return lastOccurence(array2, (mid + 1), high, x, n); } return -1; } public static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; int pivot = arr[mid]; int left = lo; int right = hi; while (left <= right) { while (arr[left] < pivot) { left++; } while (arr[right] > pivot) { right--; } if (left <= right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } quickSort(arr, lo, right); quickSort(arr, left, hi); } 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[] readArrays(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scn.nextInt(); } return a; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextLine() { 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 nextLine(); } 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(1); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
5017c763567cd7dbd7c26e2aa9819e3e
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; // author : Multi-Thread public class A { static int mod = (int) 1e9 + 7; static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String[] args) { int test = fs.nextInt(); // int test = 1; for (int cases = 0; cases < test; cases++) { // solve(); System.out.println(solve()); } } static String solve() { int n = fs.nextInt(); long ar[] = new long[n]; for (int i = 0; i < n; i++) { ar[i] = fs.nextLong(); } long counter = 1, left = ar[0]; ar[0] = 0; for (int i = 1; i < n - 1; i++) { if (left + ar[i] >= counter) { if (ar[i] > counter) { left += ar[i] - counter; } else { left -= counter - ar[i]; } } else { return "NO"; } ar[i] = counter; ++counter; } ar[n - 1] += left; for (int i = 0; i < n - 1; i++) { if (ar[i] >= ar[i + 1]) return "NO"; } return "YES"; } static void intSort(int[] a, boolean reverse) { ArrayList<Integer> al = new ArrayList<Integer>(); for (int i : a) al.add(i); Collections.sort(al); if (reverse) { for (int i = 0; i < a.length; i++) a[i] = al.get(a.length - i - 1); } else { for (int i = 0; i < a.length; i++) a[i] = al.get(i); } } static void longSort(long[] a, boolean reverse) { ArrayList<Long> al = new ArrayList<>(); for (long i : al) al.add(i); Collections.sort(al); if (reverse) { for (int i = 0; i < a.length; i++) a[i] = al.get(a.length - i - 1); } else { for (int i = 0; i < a.length; i++) a[i] = al.get(i); } } public static int[] radixSort(int[] f) { return radixSort(f, f.length); } public static int[] radixSort(int[] f, int n) { int[] to = new int[n]; { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } static class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "[" + first + "," + second + "]"; } } static class LongPair { long first; long second; LongPair(long a, long b) { this.first = a; this.second = b; } public String toString() { return "[" + first + "," + second + "]"; } } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sq = (long) Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println() { writer.print("\n"); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static final FastReader fs = new FastReader(); private static final OutputWriter out = new OutputWriter(System.out); }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
da5e53a3fa94d1e48acd71b0957dd444
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class shift{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long s=0; int p=0; int ar[]=new int[n]; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); s+=ar[i]; if(s<(i+1)*(i)/2) p=1; } if(p==1) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
45b0cd429a8053693c6aefb76cd53a93
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static int mod = 1000000007; public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int arr[] = fs.readArray(n); long sum[] = new long[n]; sum[0] = arr[0]; for (int i = 1; i < n; i++) { sum[i] += sum[i - 1] + arr[i]; } boolean out = true; for (int i = n; i > 0; i--) { long s = sum[i - 1]; long size = (long) i; if (s < size * (size - 1) / 2) { out = false; break; } } if (!out) System.out.println("NO"); else System.out.println("YES"); } } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] a) { // suffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } // then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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()); } double nextDouble() { return Double.parseDouble(next()); } ArrayList<Integer> readList(int n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(nextInt()); return list; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
a542ad2a547b9251828effe7cb387a12
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
//<———My cp———— import java.util.*; import java.io.*; public class A_Shifting_Stacks{ public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int l = fr.nextInt(); int[] data = new int[l]; for(int i = 0;i<l;i++){ data[i] = fr.nextInt(); } long sum = 0; boolean possible = true; for(int i =0;i<l;i++){ sum+=data[i]; if(sum<((i*(i+1))/2)){ possible = false; } } if(possible){ System.out.println("YES"); }else{ System.out.println("NO"); } } } public static void print(Object val){ System.out.print(val); } public static void println(Object val){ System.out.println(val); } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } } //https://codeforces.com/problemset/problem/1541/B
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
8a8e0b3d509ea2e0cbb67787e12e9fbd
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; public class ShiftingStacks { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int k = 0; k < t; k++) { int n = Integer.parseInt(br.readLine()); long arr[] = new long[n]; String num[] = br.readLine().split(" "); long have = 0, need = 0; int flag = 1; for(int i = 0; i < n; i++) { arr[i] = Long.parseLong(num[i]); have += arr[i]; need += i; if(have < need) { flag = 0; break; } } if(flag == 1) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
b9c37f70eabe7b9ba23655e25225f131
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class d2 { static ArrayList<ArrayList<Integer>> graph; static boolean[] visited; static int[] distance; public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int p = Integer.parseInt(st.nextToken()); outer : while(p-->0){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); long need = 0L; long min = 0L; int i = 0; while(st.hasMoreTokens()){ int k = Integer.parseInt(st.nextToken()); need += k; min += i; i++; if(min > need){ System.out.println("NO"); continue outer; } } System.out.println("YES"); } } static void bfs(int vertex,int des,int dist){ ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(vertex); while(!queue.isEmpty()){ int current = queue.pollFirst(); visited[current] = true; // System.out.println(current+1); dist++; ArrayList<Integer> demo = new ArrayList<>(); for(int v : graph.get(current)){ if(!visited[v]){ queue.add(v); demo.add(v); if(des == v){ System.out.println("distance = "+dist); } } } for(int i=0;i<demo.size();i++){ System.out.print((demo.get(i)+1)+" "); } if(demo.size() != 0){ System.out.println(); } } } static int gcd(int a,int b){ int k = Math.max(a,b); int k1 = Math.min(a,b); return (k1 == 0) ? k : gcd(k1,k%k1); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
ed1f65fa2622694437e32bc32f3944a7
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tests= sc.nextInt(); for(int i=0;i<tests;i++){ int height=sc.nextInt(); long sum=0; int flag=1; long arr[]=new long[height]; for(int j=0;j<height;j++) arr[j]=sc.nextLong(); for(int k=0;k<height;k++){ int formula1 = (k+1) * (k) / 2; sum = sum + arr[k]; //System.out.println(arr[k]); if (sum < formula1) { flag=0; System.out.println("NO"); break; } } //System.out.println(); if(flag==1) System.out.println("YES"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
5ea784fb7b69f09848009f4df12f5269
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class sol { static Scanner scr=new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub int t=scr.nextInt(); // prime=new boolean[50000+1]; // Arrays.fill(prime, false); // fill(); int count=1; while(t-->0) { solve(count); count++; } } static void solve(int count) { int n=read(); long []a=new long[n]; long sum=0; for(int i=0;i<n;i++) { a[i]=scr.nextLong(); } long need=0; for(int i=0;i<n;i++) { need+=i; sum+=a[i]; if(sum<need) { println("NO"); return; } } println("YES"); } static boolean prime[]; static void fill() { int n=50000; for(int i=2;i<=n;i++) { prime[i]=true; } for(int i=2;i<=Math.sqrt(n);i++) { if(prime[i]) { for(int j=i*i;j<=n;j+=i) { prime[i]=false; } } } } static String getString(String s,int n) { String res=""; while(n-->0) { res+=s; } return res; } static String reverse(String s) { String st=""; for(int i=s.length()-1;i>=0;i--) { st+=s.charAt(i); } return st; } static int len(String s) { return s.length(); } static int len(int a[]) { return a.length; } static HashSet<Integer>hs(){ return new HashSet<Integer>(); } static int read() { return scr.nextInt(); } static String readS() { return scr.next(); } static void print(int b) { System.out.print(b+" "); }static void print(long b) { System.out.print(b+" "); } static void println(int b) { System.out.println(b); }static void println(long b) { System.out.println(b); }static void print(String b) { System.out.print(b+" "); } static void println(String b) { System.out.println(b); } static void println() { System.out.println(); } static PriorityQueue<Integer>pq(){ return new PriorityQueue<Integer>(); }static PriorityQueue<Integer>pqr(){ return new PriorityQueue<Integer>(Collections.reverseOrder()); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int x,int y){ return x*y/gcd(x,y); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
5eb8b650f5eda2c0ae22cdfc9a619687
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) { // Initially, all elements are in // their own set. parent[i] = i; } } // Returns representative of x's set int find(int x) { // Finds the representative of the set // that x is an element of if (parent[x] != x) { // if x is not the parent of itself // Then x is not the representative of // his set, parent[x] = find(parent[x]); // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } } 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; } } static class HashMultiSet<T> implements Iterable<T> { private final HashMap<T,Integer> map; private int size; public HashMultiSet(){map=new HashMap<>(); size=0;} public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public Iterator<T> iterator() { return new Iterator<>() { private final Iterator<T> iter = map.keySet().iterator(); private int count = 0; private T curElement; public boolean hasNext(){return iter.hasNext()||count>0;} public T next() { if(count==0) { curElement=iter.next(); count=get(curElement); } count--; return curElement; } }; } } private static long abs(long x){ if(x < 0) x*=-1l;return x; } private static int abs(int x){ if(x < 0) x*=-1;return x; } // public static int get(HashMap<Integer, Deque<Integer> > a, int key ){ // return a.get(key).getLast(); // } // public static void removeLast (HashMap<Integer,Deque<Integer> > a, int key){ // if(a.containsKey(key)){ // a.get(key).removeLast(); // if(a.get(key).size() == 0) a.remove(key); // } // } // public static void add(HashMap<Integer,Deque<Integer>> a, int key, int val){ // if(a.containsKey(key)){ // a.get(key).addLast(val); // }else{ // Deque<Integer> b = new LinkedList<>(); // b.addLast(val); // a.put(key,b); // } // } public static int solve(int n, DisjointUnionSets dsu, HashSet<Integer> s){ int sum [] = new int[n]; boolean ck [] = new boolean[n]; for(int i =0;i<n;i++){ sum[dsu.find(i)]++; if(!s.contains(i)) ck[dsu.find(i)] = true; } int ans = 0; for(int i =0;i<n;i++){ if(sum[i] != 0 && ck[i]){ ans += (sum[i] -1); } if(sum[i] > 1 && !ck[i]){ ans += (sum[i] +1); } } return ans; } public static void main(String[] args) { MyScanner sc = new MyScanner(); int t = sc.nextInt(); while (t-- != 0){ int n = sc.nextInt(); long sum =0; boolean check = true; for(int i=0;i<n;i++){ long x = sc.nextLong(); sum = sum + (x - (long)i); if(sum < 0){ check = false; } } if(check ) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
c155883e2acfd7a713fb40c93ae65756
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testcases = sc.nextInt(); for (int test = 0; test < testcases; test++) { int N = sc.nextInt(); long[] stacks = new long[N]; for (int i = 0; i < N; i++) stacks[i] = sc.nextLong(); long[] prefix_sum = new long[N + 1]; prefix_sum[0] = 0; for (int i = 0; i < N; i++) { prefix_sum[i + 1] = prefix_sum[i] + stacks[i]; } long sum = 0; boolean possible = true; for (int i = 0; i < N; i++) { sum += i; if (prefix_sum[i + 1] < sum) { possible = false; } } if (possible) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
670bbf87f189b24a0c1da89fadfa7e03
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test = 0; test < T; test++) { int N = sc.nextInt(); long[] stacks = new long[N]; for (int i = 0; i < N; i++) stacks[i] = sc.nextLong(); long[] prefix_sum = new long[N + 1]; prefix_sum[0] = 0; for (int i = 0; i < N; i++) { prefix_sum[i + 1] = prefix_sum[i] + stacks[i]; } long sum = 0; boolean possible = true; for (int i = 0; i < N; i++) { sum += i; if (prefix_sum[i + 1] < sum) { possible = false; } } if (possible) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
8d7907c1189c703c1759e82e84565525
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; public class Day1_1_ShiftingStacks { public static void main(String[] args) { InputReader reader = new InputReader(System.in); int n = reader.nextInt(); StringBuilder outBuffer = new StringBuilder(); while (n-- > 0) { int length = reader.nextInt(); int[] numbers = new int[length]; for (int i = 0; i < length; i++) { numbers[i] = reader.nextInt(); } long minNum = 0; long sum = 0; boolean possible = true; for (int i = 0; i < length; i++) { minNum += i; sum += numbers[i]; if (sum < minNum) { possible = false; break; } } if (!possible) outBuffer.append("NO\n"); else outBuffer.append("YES\n"); } System.out.print(outBuffer); } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
63ba071904ff97256937405bdc245576
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; public class Stack { public static void main(String[] args) { int tc; Scanner in = new Scanner(System.in); tc = in.nextInt(); for (int t = 0; t < tc; t++) { int n; n = in.nextInt(); // int[] arr = new int[n]; long required = 0; long sumTmp = 0; boolean ascend = true; for (int i = 0; i < n; i++) { required += i; sumTmp += in.nextLong(); // System.out.println(sumTmp); if (sumTmp < required) ascend = false; } if (ascend) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
1e5273a08023a02454dc9a6cf3fa0f2e
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class Rhombus{ static int mod = 1000000007; static InputReader in = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t=in.readInt(); while(t-->0) { int n=in.readInt(); int a[]=nextIntArray(n); long sum=0,is=0; boolean ans=true; for(int i=0;i<n;i++) { sum+=a[i]; is+=i; if(sum<is) { ans=false; break; } } if(ans) System.out.println("YES"); else System.out.println("NO"); } } static String reverse(String s1) { String s2=""; for(int i=s1.length()-1;i>=0;i--) { s2+=s1.charAt(i); } return s2; } static boolean isVowel(char c) { if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return true; return false; } static String removeChar(String s,int a,int b) { return s.substring(0,a)+s.substring(b,s.length()); } static int[] nextIntArray(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static long[] nextLongArray(int n){ long[]arr= new long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static int[] nextIntArray1(int n) { int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static long[] nextLongArray1(int n){ long[]arr= new long[n+1]; int i=1; while(i<=n) { arr[i++]=in.readLong(); } return arr; } static long gcd(long x, long y) { if (x % y == 0) return y; else return gcd(y, x % y); } static long pow(long n, long m) { if(m==0) return 1; else if(m==1) return n; else { long r=pow(n,m/2); if(m%2==0) return r*r; else return r*r*n; } } static long max(long a,long b,long c) { return Math.max(Math.max(a, b),c); } static long min(long a,long b,long c) { return Math.min(Math.min(a, b), c); } static class Pair implements Comparable<Pair> { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { if (this.a != o.a) return Integer.compare(this.a, o.a); else return Integer.compare(this.b, o.b); // return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } 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 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 double readDouble() { 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, readInt()); 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, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
1ca770ceb33a73fa306490818acba0a4
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args)throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(bufferedReader.readLine()); while ( test -- > 0 ) { int n = Integer.parseInt(bufferedReader.readLine()); String[] s = bufferedReader.readLine().split(" "); List<Long>list = new ArrayList<>(); for( int i = 0 ; i < n ; i++ ) { list.add(Long.parseLong(s[i])); } for( int i = 1 ; i < n ; i++ ) { long num = list.get(i-1) - (i-1); list.set(i,Math.max(num+list.get(i),list.get(i))); list.set(i-1,Math.min(list.get(i-1),i-1)); } boolean flag = false; for( int i = 1 ; i < n ; i++ ) { if(list.get(i-1) >= list.get(i)) flag = true; } if(flag == false) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
d275c6ef1f2b4e01765d9bedc456b122
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public final class file{ public static void main(String args[]){ Scanner scan = new Scanner(System.in); int n=scan.nextInt(); for(int i=0;i<n;i++){ int z=scan.nextInt(); long a[] = new long[z]; long sum=0; for(int j=0;j<z;j++){ a[j]=scan.nextInt(); sum=sum+a[j]; } boolean b=true; long s = a[z-1]; long t=sum-(z-2)*(z-1)/2; int d=0; if(sum>=z*(z-1)/2 && t>=s) { for(int y=z-2;y>=0;y--) { s=s+a[y]; d=d+y; if(t+d<s) { b=false; System.out.println("No"); break; } } if(b) System.out.println("Yes"); } else System.out.println("No"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
0eaa1a05d19d22e7a1e04f62c47c3cb6
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; public class Template { public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); PrintWriter out = new PrintWriter(System.out); outer: while (yo-- > 0) { int n = sc.nextInt(); long[] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextLong(); } for(int i = 0; i < n-1; i++) { if(a[i] < i) { out.println("NO"); continue outer; } else { long diff = a[i]-i; a[i+1] += diff; a[i] = i; } } for(int i = 0; i < n-1; i++) { if(a[i] >= a[i+1]) { out.println("NO"); continue outer; } } out.println("YES"); } out.close(); } static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n + 1]; for (int i = 2; i <= n; i++) { if (isPrime[i]) continue; for (int j = 2 * i; j <= n; j += i) { isPrime[j] = true; } } return isPrime; } static int mod = 1000000007; static long pow(int a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } if (b % 2 == 0) { long ans = pow(a, b / 2); return ans * ans; } else { long ans = pow(a, (b - 1) / 2); return a * ans * ans; } } 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()); } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
fd4482bad3517f2357e16f88044ae182
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { void solve() throws IOException { int t = nextInt(); for (int i = 0; i < t; ++i) { /* 4 1 4 9 0 1 0 1 2 1 4 0 1 2 10 0 0 */ int n = nextInt(); long need = 0L; long[] arr = new long[n]; for (int j = 0; j < n; ++j) { arr[j] = nextInt(); } for (int j = 0; j < n - 1; ++j) { need = j; if (arr[j] > need) { long diff = arr[j] - need; arr[j + 1] += diff; arr[j] -= diff; } } boolean can = true; for (int j = 0; j < n - 1; ++j) { if (arr[j] >= arr[j + 1]) { can = false; break; } } if (can) { writer.println("yes"); } else { writer.println("no"); } } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } StringTokenizer tokenizer; PrintWriter writer; BufferedReader reader; public void run() { try { writer = new PrintWriter(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new A().run(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
9bfc5b5ee4e3bad20e8cbbe9198ee909
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class c700A { static int ans= Integer.MIN_VALUE ; public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t = sc.nextInt(); while((t--)!=0) { int n = sc.nextInt(); long arr[] = new long[n]; boolean ans = true; long rem = 0; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } for(int i=0;i<n;i++) { arr[i]=arr[i]+rem; if(arr[i]<i) { ans = false; break; } rem = arr[i]-i; } System.out.println(ans?"YES":"NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
da8671e13fd18e5a0eff87efe42a9004
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.io.*; //My life seems to be a joke. But, one day I will conquer. public class B{ public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); a:for(int tt=0;tt<t;tt++) { int n = fs.nextInt(); long[] arr = new long[n]; for(int i=0;i<n;i++) { long a = fs.nextLong(); arr[i] = a; } for(int i=1;i<n;i++) { arr[i]+=arr[i-1]; } long[] temp = new long[n]; for(int i=0;i<n;i++) { temp[i] = i; } for(int i=1;i<n;i++) { temp[i] +=temp[i-1]; } for(int i=0;i<n;i++) { if(arr[i]<temp[i]) { System.out.println("NO"); continue a; } } System.out.println("YES"); } out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
d893f906ff32c8678dbe22f2cf31dab3
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Solution{ public static void main(String[] args) { TaskA solver = new TaskA(); int t = in.nextInt(); for (int i = 1; i <= t ; i++) { solver.solve(i, in, out); } // solver.solve(1, in, out); out.flush(); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long[]arr=input(); long[]sum=new long[arr.length]; for(int i=0;i<arr.length;i++) { sum[i]=arr[i]; if(i>=1) { sum[i]+=sum[i-1]; } } for(int i=0;i<sum.length;i++) { if(sum[i]<(i*(i+1))/2) { println("NO");return; } } println("YES"); } } static boolean check(Pair[]arr,int mid,int n) { int c=0; for(int i=0;i<n;i++) { if(mid-1-arr[i].first<=c&&c<=arr[i].second) { c++; } } return c>=mid; } static long sum(int[]arr) { long s=0; for(int x:arr) { s+=x; } return s; } static long sum(long[]arr) { long s=0; for(long x:arr) { s+=x; } return s; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void println(int[][]arr) { for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[0].length;j++) { print(arr[i][j]+" "); } print("\n"); } } static void println(long[][]arr) { for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[0].length;j++) { print(arr[i][j]+" "); } print("\n"); } } static void println(int[]arr){ for(int i=0;i<arr.length;i++) { print(arr[i]+" "); } print("\n"); } static void println(long[]arr){ for(int i=0;i<arr.length;i++) { print(arr[i]+" "); } print("\n"); } static long[]input(int n){ long[]arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); } return arr; } static long[]input(){ long n= in.nextInt(); long[]arr=new long[(int)n]; for(int i=0;i<n;i++) { arr[i]=in.nextLong(); } return arr; } //////////////////////////////////////////////////////// static class Pair { long first; long second; Pair(long x, long y) { this.first = x; this.second = y; } } static void sortS(Pair arr[]) { Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return (int)(p1.second - p2.second); } }); } static void sortF(Pair arr[]) { Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return (int)(p1.first - p2.first); } }); } ///////////////////////////////////////////////////////////// static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); static void println(long c) { out.println(c); } static void print(long c) { out.print(c); } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } static void println(boolean b) { out.println(b); } 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()); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
094a8f3e1a45b7b3624993cf9f4d6b2d
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.math.BigInteger; import java.util.*; import static java.lang.System.out; public class Round_780_Div_3 { static Scanner str = new Scanner(System.in); static ArrayList<Integer> list; final int mod = 1000000007; public static void main(String[] args) { long T = str.nextLong(); while (T-- > 0) { solve(); } } static void solve() { int n = str.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(str.nextInt()); } long sum = 0; long ind = 0; for (int i = 0; i < n; i++) { sum += list.get(i); ind += i; if (sum < ind) { out.println("NO"); return; } } out.println("YES"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
59b3c60a12fc51343b5f9203dc1b90f5
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.stream.IntStream; import java.util.Arrays; import java.util.*; public class Program { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); String r = "YES"; String[] tmp = sc.nextLine().split(" "); long[] arr = new long[n]; for (int j = 0 ; j < n; j++) { arr[j] = Long.parseLong(tmp[j]); } for (int k = 0; k < n - 1; k++) { long move = arr[k] -k; //System.out.println(move); if((move == 0) && (arr[k] < arr[k + 1])) { arr[k] -= move; arr[k+1] += move; } else if (move > 0){ arr[k] -= move; arr[k+1] += move; } else { r = "NO"; break; } } if (arr[n - 1] < n - 1) { r = "NO"; } System.out.println(r); //System.out.println(Arrays.toString(arr)); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
c2306225ebb710a0eb5f8dda0b705ccb
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.Scanner; public class ShiftingStacks { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); long[] a = new long[n]; String ans = "YES"; long count = 0; for (int i = 0; i < n; i++) { a[i] = in.nextLong(); } for (int i = 0; i < n; i++) { count += a[i]; if (count < i) { ans = "NO"; break; } count = count - i; } System.out.println(ans); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
8297fda88180c869574721534ad5920c
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class class42 { public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); while(t-->0) { int n=input.nextInt(); long a[]=new long[n]; long count=0,x=0,temp=0; for(int i=0;i<n;i++) { a[i]=input.nextLong(); } for(int i=0;i<n;i++) { count=count+(long)i; temp=temp+a[i]; if(temp<count) { x++; System.out.println("NO"); break; } } if(x==0) { System.out.println("YES"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
806593cf5cce3687889edfd7bf4198cb
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class A_AB_Balance{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long[] arr=new long[n]; int count=0; long sum=0; boolean ans=true; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); count+=i; sum+=arr[i]; if(sum<count){ ans=false; } } if(ans){ System.out.println("YES"); } else{ System.out.println("NO"); } } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
917876bfe675756d4e01ccf7decdd3d8
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Scanner; import java.util.StringTokenizer; public class P1 { public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); int t = sc.nextInt(); StringBuilder st = new StringBuilder(); while (t-- != 0) { int n=sc.nextInt(); int a[]=new int[n]; long sum=0; int flag=0; int need=0; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); sum=sum+a[i]; need+=i; if(sum<need) { flag=1; } } if(flag==1) { st.append("NO\n"); }else { st.append("YES\n"); } } System.out.println(st); } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
4a6db81c7918c6490ae64a3a5fc183f7
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n = sc.nextInt(); boolean check = true; long sum = 0; for( int i =0 ; i< n; i++) { long temp =sc.nextLong(); temp+=sum; if( temp < i) { check = false; } else { sum = (temp-i); } } out.println(check?"YES":"NO"); } out.flush(); } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
2d8b609a166b3d796a510ec25fd5bc17
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.InputStream; import java.io.OutputStream; import java.util.StringTokenizer; /* * @author : Imtiaz Adar * Stream : Koshto */ public class Shifting_Stacks { public static void main(String[] args) throws IOException { InputStream inputstream = System.in; InputStreamReader inputstreamreader = new InputStreamReader(inputstream); OutputStream outputstream = System.out; PrintWriter out = new PrintWriter(outputstream); BufferedReader br = new BufferedReader(inputstreamreader); StringTokenizer st = new StringTokenizer(br.readLine()); ArrayReader scan = new ArrayReader(br, st); int n = Integer.parseInt(st.nextToken()); while(n-->0) { StringBuilder sb = new StringBuilder(); st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); long[] dp = scan.LongArray(x); long sum = 0, indexsum = 0; boolean how = false; for (int i=0; i<dp.length; i++){ sum += dp[i]; indexsum += i; if(sum < indexsum) { how = true; break; } } sb.append((how)?"NO":"YES"); out.println(sb); out.flush(); } out.close(); } static class ArrayReader { private BufferedReader readfile; private StringTokenizer token; ArrayReader(BufferedReader br, StringTokenizer st) { this.readfile = br; this.token = st; } int[] IntArray(int size) throws IOException { int[] dp = new int[size]; token = new StringTokenizer(readfile.readLine()); for (int i = 0; i < size; i++) { dp[i] = Integer.parseInt(token.nextToken()); } return dp; } double[] DoubleArray(int size) throws IOException { double[] dp = new double[size]; token = new StringTokenizer(readfile.readLine()); for (int i = 0; i < size; i++) { dp[i] = Double.parseDouble(token.nextToken()); } return dp; } long[] LongArray(int size) throws IOException { long[] dp = new long[size]; token = new StringTokenizer(readfile.readLine()); for (int i = 0; i < size; i++) { dp[i] = Long.parseLong(token.nextToken()); } return dp; } String[] StringArray(int size) throws IOException { String[] dp = new String[size]; token = new StringTokenizer(readfile.readLine()); for (int i = 0; i < size; i++) { dp[i] = token.nextToken(); } return dp; } int[][] IntArray2d(int size1, int size2) throws IOException { int[][] dp = new int[size1][size2]; for (int i = 0; i < size1; i++) { token = new StringTokenizer(readfile.readLine()); for (int j = 0; j < size2; j++) { dp[i][j] = Integer.parseInt(token.nextToken()); } } return dp; } double[][] DoubleArray2d(int size1, int size2) throws IOException { double[][] dp = new double[size1][size2]; for (int i = 0; i < size1; i++) { token = new StringTokenizer(readfile.readLine()); for (int j = 0; j < size2; j++) { dp[i][j] = Double.parseDouble(token.nextToken()); } } return dp; } long[][] LongArray2d(int size1, int size2) throws IOException { long[][] dp = new long[size1][size2]; for (int i = 0; i < size1; i++) { token = new StringTokenizer(readfile.readLine()); for (int j = 0; j < size2; j++) { dp[i][j] = Long.parseLong(token.nextToken()); } } return dp; } String[][] StringArray2d(int size1, int size2) throws IOException { String[][] dp = new String[size1][size2]; for (int i = 0; i < size1; i++) { token = new StringTokenizer(readfile.readLine()); for (int j = 0; j < size2; j++) { dp[i][j] = token.nextToken(); } } return dp; } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
b19c6b7b3f811edb084e803dcce1e614
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.*; public class Round7032 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++) { int n = sc.nextInt(); long arr[] = new long[n]; for(int j = 0; j < n; j++) { arr[j] = sc.nextLong();} for(int j = 0; j < n - 1; j++) { long l = Math.min(j, arr[j]); long s = arr[j] - l; arr[j] = l; arr[j + 1] += s; } boolean dd = true; for(int j = 0; j < n - 1; j++) { if(arr[j] >= arr[j + 1]) { dd = false; break; } } if(dd) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
c5d55cf8b66710b36e45509beb4a4e42
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
//package codeforces; import java.util.*; public class solution { public static void main(String args[]){ Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int tt=0;tt<t;tt++){ int n=s.nextInt(); long sum=0,sum1=0; boolean ans=true; for(int i=0;i<n;i++) { int x=s.nextInt(); sum+=x; sum1+=i; if(sum1>sum) { ans=false; } } if(ans) { System.out.println("YES"); }else { System.out.println("NO"); } } s.close(); } static void sort(int [] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if (x[c] != y[c]) return(int)( x[c] - y[c]); return (int)-(x[1]+x[2] - y[1]-y[2]); }); } public static void printb(boolean ans) { if(ans) { System.out.println("YES"); }else { System.out.println("NO"); } } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
988d5e41c5507b01d58a18fa48b2fa2f
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Vector; public class codeforce { static Scanner sc = new Scanner(System.in); static Vector<Integer> ve = new Vector<Integer>(); static List<Integer> list = new ArrayList<Integer>(); public static void main(String[] args) { int t = sc.nextInt(); while(t-- > 0) { b1486A(); } } public static void b1486A() { int n = sc.nextInt(); long[] a = new long[n]; long ans = 0, res = 0; boolean ok = true; for(int i = 0 ; i < n ; i ++) { a[i] = sc.nextLong(); } for(int i = 0 ; i < n ; i ++) { ans += a[i]; res += i; if(ans < res) ok = false; } System.out.println(ok == true ? "YES" : "NO"); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
0b6d079f23835c6fc574c9f45b16b828
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ShiftingStacks { public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } long extra=0; boolean flag=false; for(int i=0;i<arr.length;i++) { if(arr[i]>=i) extra+=(arr[i]-i); else if(arr[i]+extra>=i) extra-=(i-arr[i]); else { System.out.println("NO"); flag=true; break; } } if(!flag) System.out.println("YES"); } } } 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 (Exception 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 (Exception e) { e.printStackTrace(); } return str; } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output
PASSED
4f797486df74a56c1ce928318dc68d1c
train_109.jsonl
1613658900
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class ShiftingStacks { public static void main(String[] args) throws IOException { ShiftingStacks solver = new ShiftingStacks(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); for(int i=1;i<=t;i++) { solver.solve(i, in, out); } out.close(); } public void solve(int i,BufferedReader in, PrintWriter out) throws IOException { int n = Integer.parseInt(in.readLine()); String strHeights[] = in.readLine().split(" "); long heights[] = new long[n]; for(int j=0;j<heights.length;j++) { heights[j] = Integer.parseInt(strHeights[j]); } boolean ok = true; if(n>1) { for(int j=0;j<n;j++) { if(j==0) { heights[j+1] = heights[j+1] + heights[j] ; heights[j] = 0; }else if(j==n-1) { if(heights[j]<=heights[j-1]) { ok = false; break; } } else { if(heights[j]<=heights[j-1]) { ok = false; break; } long diff = heights[j] - (heights[j-1]+1); heights[j+1] = heights[j+1] + diff; heights[j] = heights[j]-diff; } } } if(ok) { out.println("YES"); } else { out.println("NO"); } out.flush(); } }
Java
["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"]
1 second
["YES\nYES\nYES\nNO\nNO\nYES"]
NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.
Java 11
standard input
[ "greedy", "implementation" ]
7a8c4ba98a77097faff625b94889b365
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.
900
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
standard output