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
a49d9bdd518f74811bc190fa3bb98942
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i < h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class NewColony { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- != 0) fairnum(); } public static void fairnum() { int n=sc.nextInt(); int k=sc.nextInt(); int i,h[]=new int[n]; for(i=0;i<n;i++) h[i]=sc.nextInt(); if(0==n-1) { System.out.println("-1"); return; } while(k--!=0) { i=0; if(!(h[i]<h[i+1])) while(h[i]>=h[i+1]) { if(i+1!=n-1) i++; else{ System.out.println("-1"); return; } } h[i]++; } System.out.println(i+1); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
47272848a62b6a748322cf925e27f3b1
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/* ID: xfrostb1 LANG: JAVA TASK: b */ import java.io.*; import java.util.*; public class b { public static void main(String[] args) throws IOException { InputStreamReader in = new InputStreamReader(System.in); BufferedReader f = new BufferedReader(in); int N = Integer.parseInt(f.readLine()); for (int i = 0; i < N; i++) { // long M = Long.parseLong(f.readLine()); StringTokenizer s = new StringTokenizer(f.readLine()); int M = Integer.parseInt(s.nextToken()); int K = Integer.parseInt(s.nextToken()); int[] height = new int[M]; s = new StringTokenizer(f.readLine()); int max = 0; // int[] pre = new int[M]; for (int j = 0; j < M; j++) { height[j] = Integer.parseInt(s.nextToken()); // max = Math.max(height[j],max); //// if(j > 0 && height[j] > height[j-1]){ //// amount += height[j]-height[j-1]; //// } } // int curr = height[0]; // if (M == 1) { // System.out.println(-1); // continue; // } else if (M == 2) { // if (height[0] >= height[1]) { // System.out.println(-1); // } else { // if (height[1] - height[0] >= K) { // System.out.println(1); // } else { // System.out.println(-1); // } // } // continue; // } // // // int amount = 0; // // if (height[1] - height[0] >= K) { // System.out.println(0); // break; // } // // int index = 0; // int curr = 0; // boolean works = true; // while(true){ // if(index == M-1){ // System.out.println("-1"); // works = false; // break; // } // if(height[index+1] > height[index]){ // curr = height[index]; // break; // }else{ // // index++; // } // } // if(!works){ // continue; // } // int l = index-1; // int r = index+1; // while (true) { // if(r == M){ // System.out.println(-1); // break; // }else{ // if(height[l] > height[r]){ // amount += (height[r]-curr)*(r-l-1); // // if(amount >= K){ // int ret = (K-amount)%(r-l-1); // if(ret == 0){ // System.out.println(l+2); // break; // }else{ // System.out.println(r-ret+1); // break; // } // } // // curr = height[r]; // for(int j = l+1; j < r; j++){ // height[j] = curr; // } // if(r == M-1){ // System.out.println(-1); // break; // } // if(height[r+1] > height[r]){ // r+=1; // }else{ // index = r+1; // l = r; // while(true){ // if(height[index+1] > height[index]){ // curr = height[index]; // r = index+1; // break; // }else{ // if(index == M-1){ // System.out.println("-1"); // break; // } // index++; // } // } // } // }else if(height[l] < height[r]) { // amount += (height[l] - curr)*(r-l-1); // curr = height[l]; // for (int j = l; j < r; j++) { // height[j] = curr; // } // int newindex = Math.max(0,l); // if(newindex != index) { // while (true) { // if (height[newindex] > height[l]) { // l = newindex; // break; // }else{ // newindex = Math.max(0,newindex-1); // if(newindex == 0){ // l = newindex; // break; // } // } // } // } // // }else{ // if(l == 0 && r == M-1 && amount < K){ // System.out.println(-1); // break; // } // l = Math.max(0,l-1); // r = Math.min(M-1,r+1); // // } // } // } // for(int j = 0; j < M-1; j++){ // if(height[j] < height[j+1]){ // // if(j == 0){ // amount += height[j+1]-height[j]; // if(amount >= K){ // System.out.println(0); // break; // } // }else { // for (int k = j; k >= 0; k--) { // if(height[k] >= height[j+1]){ // break; // }else{ // amount += height[j+1]-height[k]; // height[k] = height[j+1]; // } // } // // // } // } // } int last = -1; int amount = 0; boolean failed = false; for(int j = 0; j < K; j++){ last = -1; for(int k = 0; k < M; k++){ if(k == M-1){ failed = true; break; } if(height[k] < height[k+1]){ // int newval = Math.min(height[k+1], height[k-1]) height[k]++; last = k+1; break; } } if(failed){ last = -1; break; } } System.out.println(last); } } static class Input { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9dd16fd37a6a2481ec2d8da0bbb8833b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.*; // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ //JUst keep faith in ur strengths .................................................. // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; // Rectangle r = new Rectangle(int x , int y,int widht,int height) //Creates a rect. with bottom left cordinates as (x, y) and top right as ((x+width),(y+height)) //BY DEFAULT Priority Queue is MIN in nature in java //to use as max , just push with negative sign and change sign after removal // We can make a sieve of max size 1e7 .(no time or space issue) // In 1e7 starting nos we have about 66*1e4 prime nos // In 1e6 starting nos we have about 78,498 prime nos public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// static long factorial(long a) { if(a== 0L || a==1L)return 1L ; return a*factorial(a-1L) ; } /////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a/gc)*b ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a/gc)*b; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 1000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } static ArrayList<Integer> getPrimeFactorization(int x) { ArrayList<Integer> ret = new ArrayList<Integer>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int a ,int b) { //time comp : o(logn) long x = (long)(a) ; long n = (long)(b) ; if(n==0)return 1 ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n = n/2L ; x = x*x ; } return ans ; } public static long power(long a ,long b) { //time comp : o(logn) long x = (a) ; long n = (b) ; if(n==0)return 1L ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n = n/2L ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. a = a % p ; if(a == 0)return 0L ; long res = 1L; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x =x/2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod p. //(assuming p is prime). return modPow(a, p-2, p); } static long[] factorial = new long[1000001] ; static void modfac(long mod) { factorial[0]=1L ; factorial[1]=1L ; for(int i = 2; i<= 1000000 ;i++) { factorial[i] = factorial[i-1] *(long)(i) ; factorial[i] = factorial[i] % mod ; } } static long modBinomial(long n, long r, long p) { // calculates C(n,r) mod p (assuming p is prime). if(n < r) return 0L ; long num = factorial[(int)(n)] ; long den = (factorial[(int)(r)]*factorial[(int)(n-r)]) % p ; long ans = num*(modInverse(den,p)) ; ans = ans % p ; return ans ; } static void update(int val , long[] bit ,int n) { for( ; val <= n ; val += (val &(-val)) ) { bit[val]++ ; } } static long query(int val , long[] bit , int n) { long ans = 0L; for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val]; return ans ; } static int countSetBits(long n) { int count = 0; while (n > 0) { n = (n) & (n - 1L); count++; } return count; } static int abs(int x) { if(x < 0)x = -1*x ; return x ; } static long abs(long x) { if(x < 0)x = -1L*x ; return x ; } //////////////////////////////////////////////////////////////////////////////////////////////// static void p(int val) { out.print(val) ; } static void p() { out.print(" ") ; } static void pln(int val) { out.println(val) ; } static void pln() { out.println() ; } static void p(long val) { out.print(val) ; } static void pln(long val) { out.println(val) ; } static void yes() { out.println("YES") ; } static void no() { out.println("NO") ; } //////////////////////////////////////////////////////////////////////////////////////////// // calculate total no of nos greater than or equal to key in sorted array arr static int bs(int[] arr, int s ,int e ,int key) { if( s> e)return 0 ; int mid = (s+e)/2 ; if(arr[mid] <key) { return bs(arr ,mid+1,e , key) ; } else{ return bs(arr ,s ,mid-1, key) + e-mid+1; } } // static ArrayList<Integer>[] adj ; // static int mod= 1000000007 ; ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //sieve() ; //ArrayList<Integer> arr[] = new ArrayList[n] ; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> lista = new ArrayList<>() ; ArrayList<Long> listb = new ArrayList<>() ; // ArrayList<Integer> lista = new ArrayList<>() ; // ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; //HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> mapx = new HashMap<>() ; HashMap<Integer,Integer> mapy = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; //adj = new ArrayList[n] ; // for(int i = 0; i< n; i++) // { // adj[i] = new ArrayList<Integer>(); // } // long n = scn.nextLong() ; //String s = scn.next() ; int n= scn.nextInt() ;int k = scn.nextInt() ; int[] arr= new int[n+1] ; for(int i=1; i <= n;i++) { arr[i]= scn.nextInt(); } if(k > (n*100)) { pln(-1) ; } else{ for(int b = 1; b <= k ; b++) { int i =1 ; for( ; i <= n-1 ; i++ ) { if(arr[i] < arr[i+1]) { arr[i]++ ; break ; } } if(b == k ) { if(i==n)pln(-1) ; else pln(i) ; } } } //out.println(ans) ; //out.println(ans+" "+in) ; //out.println("Case #" + testcases + ": " + ans ) ; //out.println("@") ; set.clear() ; sb.delete(0 , sb.length()) ; list.clear() ;lista.clear() ;listb.clear() ; map.clear() ; mapx.clear() ; mapy.clear() ; setx.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) { return false ; } Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
4b7669fb2f3dd874d90828edd3680086
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.*; // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ //JUst keep faith in ur strengths .................................................. // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; // Rectangle r = new Rectangle(int x , int y,int widht,int height) //Creates a rect. with bottom left cordinates as (x, y) and top right as ((x+width),(y+height)) //BY DEFAULT Priority Queue is MIN in nature in java //to use as max , just push with negative sign and change sign after removal // We can make a sieve of max size 1e7 .(no time or space issue) // In 1e7 starting nos we have about 66*1e4 prime nos // In 1e6 starting nos we have about 78,498 prime nos public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } 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 solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; //adj = new ArrayList[n] ; // for(int i = 0; i< n; i++) // { // adj[i] = new ArrayList<Integer>(); // } // long n = scn.nextLong() ; //String s = scn.next() ; int n= scn.nextInt() ;int k = scn.nextInt() ; int[] arr= new int[n+1] ; for(int i=1; i <= n;i++) { arr[i]= scn.nextInt(); } int[] dp = new int[n+1] ; for(int i =2; i <= n ; i++) { if(arr[i] > arr[i-1]) { dp[i] = arr[i] ; } else{ dp[i] = Integer.MIN_VALUE ; } } int[] st = new int[n+1] ; int besttill = dp[n] ;int diff= 0 ; for(int i = n ; i >= 1 ; i--) { st[i] = Math.max(besttill , arr[i]) ; diff = diff + (st[i] - arr[i]) ; besttill = Math.max(besttill , dp[i]) ; } if(diff < k)out.println(-1) ; else{ int ans = 0 ; for(int b = 1; b <= k ; b++) { int i = 1; for(; i<= n-1 ; i++){ if(arr[i] < arr[i+1]) { arr[i]++ ;break ; } } if(b == k) { if(i== n)ans =-1 ; else{ ans = i ; } } } out.println(ans) ; } //out.println(ans+" "+in) ; //out.println("Case #" + testcases + ": " + ans ) ; //out.println("@") ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) { return false ; } Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d59971e94ac2af4fce29f9cc2651a0e3
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package contestCodeforces; import java.util.Scanner; public class TaskB { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int quantity = scanner.nextInt(); StringBuilder info = new StringBuilder(); for (int i = 0; i < quantity; i++) { int quantityMountain = scanner.nextInt(); int quantityBoulder = scanner.nextInt(); scanner.nextLine(); int[] mountains = new int[quantityMountain]; String[] strings = scanner.nextLine().split(" "); for (int ind = 0; ind < quantityMountain; ind++) { mountains[ind] = Integer.parseInt(strings[ind]); } int position = 0; for (int j = 1; j < quantityMountain; j++) { position = j; while (mountains[j - 1] < mountains[j]) { quantityBoulder--; mountains[j - 1]++; if (quantityBoulder <= 0) { break; } if (j - 2 >= 0) { if (mountains[j - 2] < mountains[j - 1]) { j -= 2; break; } } } if (quantityBoulder <= 0) { break; } } if (quantityBoulder > 0) { position = -1; } info.append(position).append(System.lineSeparator()); // for (int j = 1; j <= quantityBoulder; j++) { // int position = 1; // // for (int k = 1; k < quantityMountain; k++) { // if (mountains[k - 1] >= mountains[k]) { // position++; // } else { // mountains[k - 1]++; // break; // } // } // // if (position == quantityMountain) { // info.append(-1).append(System.lineSeparator()); // break; // } // // if (j == quantityBoulder) { // info.append(position).append(System.lineSeparator()); // } // } } System.out.println(info); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
26d358ecd8eadab4dc745c2c4c5710f4
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class p2 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextInt(); } deployk obj=new deployk(); int ans=obj.solve(n,a,k); System.out.println(ans); } } } class deployk { public int solve(int n,int a[],int k) { if(1000*n<k) return -1; int temp=k; for(int j=0;j<temp;j++) { for(int i=1;i<n;i++) { if(a[i]<a[i+1]) { a[i]++; k--; if(k<=0) return i; break; } } } return -1; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
05055338ccaef82f9cd9316abadb4af6
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.System.*; import static java.lang.Math.*; import static java.util.Collections.*; import static java.util.Arrays.*; public class B<Array, Target> { public static int swap(int... args) { return args[0]; } static ArrayList<Integer> primes = new ArrayList<>(); //static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); public static void main(String[] args) throws IOException { do { startTime = currentTimeMillis(); //setOut(new PrintStream("output")); //setIn(new FileInputStream("input")); int T = sc.nextInt();while (T-- != 0) { solve(); } endTime = currentTimeMillis(); long duration = endTime - startTime; //out.println(duration); if (false) { out.println(max(3, 2)); Integer[] ARRAY = new Integer[2]; ArrayList<Integer> LIST = new ArrayList<>(); LIST.add(2); // sort(LIST, reverseOrder()); LIST.sort(reverseOrder()); sort(ARRAY, reverseOrder()); out.println(LIST + " " + Arrays.toString(ARRAY)); } } while (LOCAL); } public static boolean isPal(String s) { StringBuilder stringBuilder = new StringBuilder(s); return s.equals(stringBuilder.reverse().toString()); } public static void solve() throws IOException { B main = new B<>(); ///////////////////////////////////////////////////////////////////// int n =sc.nextInt(),k=sc.nextInt(); int [] arr = new int[n]; int [] arr1 = new int[n]; for (int i = 0; i < n; i++) { arr[i]=sc.nextInt(); arr1[i]=arr[i]; } int cn; while (k!=0) { cn=0; for (int i = 0; i < n - 1; i++) { if (arr1[i] < arr1[i + 1]) { cn++; arr1[i]++; k--; if (k <= 0) { out.println(i + 1); return; } i=-1; } dbg(arr1); } if(cn==0) break; } out.println(-1); /////////////////////////////////////////////////////////////////////// } public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; // public static final boolean LOCAL = System.getProperty("LOCAL")!=null; public static final String ANSI_RED = "\u001B[31m"; public static final String ANSI_RESET = "\u001B[0m"; static long startTime; static long endTime; static boolean[] prime; static final int M = (int) 1e9 + 7; static final long MM = (long) M * M; static final int MAX = Integer.MAX_VALUE; static final int MIN = Integer.MIN_VALUE; static final int SIZE = (int) 1e9 * 2; 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 Pair implements Comparable<Pair> { private int key; private int value; public Pair(int key, int value) { this.key = key; this.value = value; } public int second() { return value; } @Override public int compareTo(Pair o) { return -Integer.compare(key, o.key); } @Override public String toString() { return "Pair{" + "x=" + key + ", y=" + value + '}'; } } int count(Array[] array, Target target){ int cn=0; for (Array value : array) { if (value.equals(target)) cn++; } return cn; } static int count (String string , char target){ int cn=0; for (int i = 0; i < string.length(); i++) { if(string.charAt(i)==target)cn++; } return cn; } void revArray(Array [] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) { Array temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } } void printArray(Array [] arr) { int n = arr.length; for (Array array : arr)out.print(array + " "); out.println(); } static void yes() {out.println("YES");} static void no() {out.println("NO");} public static int lowerBound(Integer[] array, int length, int value) { int low = -1; int hi = length; while (low + 1 < hi) { int mid = (low + hi) / 2; if (array[mid] <= value) { low = mid; } else { hi = mid; } } return low; } public static int upperBound(Integer[] array, int length, int value) { int low = -1; int hi = length; while (low + 1 < hi) { int mid = (low + hi) >> 1; if (array[mid] >= value) { hi = mid; } else low = mid; } return hi; } public static int binarySearch(Integer[] arr, int length, int value) { int low = 0; int hi = length - 1; int ans = -1; while (low <= hi) { int mid = (low + hi) >> 1; if (arr[mid] > value) { hi = mid - 1; } else if (arr[mid] < value) { low = mid + 1; } else { ans = mid; break; } } return ans; } public static int gcd(int a, int b) { return b==0?a:gcd(b, a % b); } public static int lcm(int a, int b) { return a / gcd(a, b) * b; } public static int countDivs(int n) { int cn = 0; int sqr = (int) Math.sqrt(n); for (int i = 1; i <= sqr; ++i) { if (n % i == 0) { ++cn; } } cn *= 2; if (sqr * sqr == n) cn--; return cn; } static void prime(int x) { //sieve algorithm. nlog(log(n)). prime = new boolean[(x + 1)]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= x; i++) if (prime[i]) for (int j = i * i; j <= x; j += i) prime[j] = false; } static boolean isEven(long x) { return x % 2 == 0; } static boolean isPrime(long x) { boolean flag = true; int sqr = (int) Math.sqrt(x); if (x < 2) return false; for (int i = 2; i <= sqr; i++) { if (x % i == 0) { flag = false; break; } } return flag; } static long factorial(long x) { long total = 1; for (int i = 2; i <= x; i++) total = (total * i) % M; return total; } static long power(long x, long n) { if (n == 0) { return 1; } long pow = power(x, n / 2L); if ((n & 1) == 1) { return (x * (pow) * (pow)); } return (pow * pow); } private static <T> String ts(T t) { if(t==null) { return "null"; } try { return ts((Iterable) t); }catch(ClassCastException e) { if(t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}"; } try { return ts((Object[]) t); }catch(ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: arr) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: iter) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(Object... o) { if(LOCAL) { System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": ["); for(int i = 0; i<o.length; i++) { if(i!=0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
95ea8ec9f492e7ea6834ed4a42487185
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; import java.util.function.BiFunction; public class Main { static Comparator<ArrayList<Integer>> C = Comparator.comparing(x -> x.get(0)); static long MOD = 1_000_000_000 + 7; static String alpha = "abcdefghijklmnopqrstuvwxyz"; public static void main(String[] args) throws Exception { long startTime = System.nanoTime(); int t = in.nextInt(); while (t-- > 0) { solve(); } long endTime = System.nanoTime(); err.println("Execution Time : +" + (endTime - startTime) / 1000000 + " ms"); exit(0); } static void solve() { int n = in.nextInt(); int k = in.nextInt(); long[] h = in.readAllLongs(n); int ans = 0; for (int i = 0; i < k; i++) { int count = 0; for (int j = 0; j + 1 < n; j++) { if (h[j] < h[j + 1]) { h[j]++; ans = j + 1; count++; break; } } if (count == 0) { fail(); return; } } out.println(ans); } static void debug(Object... args) { for (Object a : args) { out.println(a); } } static int dist(Pair<Integer, Integer> a, Pair<Integer, Integer> b) { return Math.abs(a.first - b.first) + Math.abs(a.second - b.second); } static void y() { out.println("YES"); } static void n() { out.println("NO"); } static int[] stringToArray(String s) { return s.chars().map(x -> Character.getNumericValue(x)).toArray(); } static <T> T min(T a, T b, Comparator<T> C) { if (C.compare(a, b) <= 0) { return a; } return b; } static <T> T max(T a, T b, Comparator<T> C) { if (C.compare(a, b) >= 0) { return a; } return b; } static void fail() { out.println("-1"); } static class Pair<T, R> { public T first; public R second; public Pair(T first, R second) { this.first = first; this.second = second; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "Pair{" + "a=" + first + ", b=" + second + '}'; } public T getFirst() { return first; } public R getSecond() { return second; } } static <T, R> Pair<T, R> make_pair(T a, R b) { return new Pair<>(a, b); } static long mod_inverse(long a, long m) { Number x = new Number(0); Number y = new Number(0); extended_gcd(a, m, x, y); return (m + x.v % m) % m; } static long extended_gcd(long a, long b, Number x, Number y) { long d = a; if (b != 0) { d = extended_gcd(b, a % b, y, x); y.v -= (a / b) * x.v; } else { x.v = 1; y.v = 0; } return d; } static class Number { long v = 0; public Number(long v) { this.v = v; } } static long lcm(long a, long b, long c) { return lcm(a, lcm(b, c)); } static long lcm(long a, long b) { long p = 1L * a * b; return p / gcd(a, b); } static long gcd(long a, long b) { while (b != 0) { long t = b; b = a % b; a = t; } return a; } static long gcd(long a, long b, long c) { return gcd(a, gcd(b, c)); } static class ArrayUtils { static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void print(char[] a) { for (char c : a) { out.print(c); } out.println(""); } static int[] reverse(int[] data) { int[] p = new int[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static void prefixSum(long[] data) { for (int i = 1; i < data.length; i++) { data[i] += data[i - 1]; } } static void prefixSum(int[] data) { for (int i = 1; i < data.length; i++) { data[i] += data[i - 1]; } } static void suffixSum(int[] data) { for (int i = data.length - 2; i > 0; i--) { data[i] += data[i + 1]; } } static long[] reverse(long[] data) { long[] p = new long[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static char[] reverse(char[] data) { char[] p = new char[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static int[] MergeSort(int[] A) { if (A.length > 1) { int q = A.length / 2; int[] left = new int[q]; int[] right = new int[A.length - q]; System.arraycopy(A, 0, left, 0, q); System.arraycopy(A, q, right, 0, A.length - q); int[] left_sorted = MergeSort(left); int[] right_sorted = MergeSort(right); return Merge(left_sorted, right_sorted); } else { return A; } } static int[] Merge(int[] left, int[] right) { int[] A = new int[left.length + right.length]; int i = 0; int j = 0; for (int k = 0; k < A.length; k++) { // To handle left becoming empty if (i == left.length && j < right.length) { A[k] = right[j]; j++; continue; } // To handle right becoming empty if (j == right.length && i < left.length) { A[k] = left[i]; i++; continue; } if (left[i] <= right[j]) { A[k] = left[i]; i++; } else { A[k] = right[j]; j++; } } return A; } static long[] MergeSort(long[] A) { if (A.length > 1) { int q = A.length / 2; long[] left = new long[q]; long[] right = new long[A.length - q]; System.arraycopy(A, 0, left, 0, q); System.arraycopy(A, q, right, 0, A.length - q); long[] left_sorted = MergeSort(left); long[] right_sorted = MergeSort(right); return Merge(left_sorted, right_sorted); } else { return A; } } static long[] Merge(long[] left, long[] right) { long[] A = new long[left.length + right.length]; int i = 0; int j = 0; for (int k = 0; k < A.length; k++) { // To handle left becoming empty if (i == left.length && j < right.length) { A[k] = right[j]; j++; continue; } // To handle right becoming empty if (j == right.length && i < left.length) { A[k] = left[i]; i++; continue; } if (left[i] <= right[j]) { A[k] = left[i]; i++; } else { A[k] = right[j]; j++; } } return A; } static int upper_bound(int[] data, int num, int start) { int low = start; int high = data.length - 1; int mid = 0; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (data[mid] < num) { low = mid + 1; } else if (data[mid] >= num) { high = mid - 1; ans = mid; } } if (ans == -1) { return 100000000; } return data[ans]; } static int lower_bound(int[] data, int num, int start) { int low = start; int high = data.length - 1; int mid = 0; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (data[mid] <= num) { low = mid + 1; ans = mid; } else if (data[mid] > num) { high = mid - 1; } } return ans + 1; } } static boolean[] primeSieve(int n) { boolean[] primes = new boolean[n + 1]; Arrays.fill(primes, true); primes[0] = false; primes[1] = false; for (int i = 2; i <= Math.sqrt(n); i++) { if (primes[i]) { for (int j = i * i; j <= n; j += i) { primes[j] = false; } } } return primes; } // Iterative Version static HashMap<Integer, Boolean> subsets_sum_iter(int[] data) { HashMap<Integer, Boolean> temp = new HashMap<Integer, Boolean>(); temp.put(data[0], true); for (int i = 1; i < data.length; i++) { HashMap<Integer, Boolean> t1 = new HashMap<Integer, Boolean>(temp); t1.put(data[i], true); for (int j : temp.keySet()) { t1.put(j + data[i], true); } temp = t1; } return temp; } static HashMap<Integer, Integer> subsets_sum_count(int[] data) { HashMap<Integer, Integer> temp = new HashMap<>(); temp.put(data[0], 1); for (int i = 1; i < data.length; i++) { HashMap<Integer, Integer> t1 = new HashMap<>(temp); t1.merge(data[i], 1, Integer::sum); for (int j : temp.keySet()) { t1.merge(j + data[i], temp.get(j) + 1, Integer::sum); } temp = t1; } return temp; } static class Graph { ArrayList<Integer>[] g; boolean[] visited; ArrayList<Integer>[] graph(int n) { g = new ArrayList[n]; visited = new boolean[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } return g; } void BFS(int s) { Queue<Integer> Q = new ArrayDeque<>(); visited[s] = true; Q.add(s); while (!Q.isEmpty()) { int v = Q.poll(); for (int a : g[v]) { if (!visited[a]) { visited[a] = true; Q.add(a); } } } } } static class SparseTable { int[] log; int[][] st; public SparseTable(int n, int k, int[] data, BiFunction<Integer, Integer, Integer> f) { log = new int[n + 1]; st = new int[n][k + 1]; log[1] = 0; for (int i = 2; i <= n; i++) { log[i] = log[i / 2] + 1; } for (int i = 0; i < data.length; i++) { st[i][0] = data[i]; } for (int j = 1; j <= k; j++) for (int i = 0; i + (1 << j) <= data.length; i++) st[i][j] = f.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } public int query(int L, int R, BiFunction<Integer, Integer, Integer> f) { int j = log[R - L + 1]; return f.apply(st[L][j], st[R - (1 << j) + 1][j]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 2048); 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public int[] readAllInts(int n) { int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); } return p; } public int[] readAllInts(int n, int s) { int[] p = new int[n + s]; for (int i = s; i < n + s; i++) { p[i] = in.nextInt(); } return p; } public long[] readAllLongs(int n) { long[] p = new long[n]; for (int i = 0; i < n; i++) { p[i] = in.nextLong(); } return p; } public long[] readAllLongs(int n, int s) { long[] p = new long[n + s]; for (int i = s; i < n + s; i++) { p[i] = in.nextLong(); } return p; } public double nextDouble() { return Double.parseDouble(next()); } } static void exit(int a) { out.close(); err.close(); System.exit(a); } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static OutputStream errStream = System.err; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); static PrintWriter err = new PrintWriter(errStream); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
808dde750cdaeccb1c828e43fd98e939
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Queue; import java.util.StringTokenizer; import java.util.function.BiFunction; import java.util.function.Function; public class Main { static BiFunction<Integer, Integer, Integer> ADD = (x, y) -> (x + y); static BiFunction<ArrayList<Integer>, ArrayList<Integer>, ArrayList<Integer>> ADD_ARRAY_LIST = (x, y) -> { x.addAll(y); return x; }; static Function<Pair<Integer, Integer>, Integer> GET_FIRST = (x) -> (x.first); static Function<Pair<Integer, Integer>, Integer> GET_SECOND = (x) -> (x.second); static Comparator<Pair<Integer, Integer>> C = Comparator.comparing(GET_FIRST).thenComparing(GET_SECOND); static long MOD = 1_000_000_000 + 7; public static void main(String[] args) throws Exception { long startTime = System.nanoTime(); int t = in.nextInt(); while (t-- > 0) { solve(); } long endTime = System.nanoTime(); err.println("Execution Time : +" + (endTime - startTime) / 1000000 + " ms"); exit(0); } static void solve() { int n = in.nextInt(); int k = in.nextInt(); int[] h = in.readAllInts(n); Map<Integer, Integer> stop = new HashMap<>(); for (int i = 0; i <= Math.min(10000, k); i++) { for (int j = 0; j + 1 < n; j++) { if (h[j] >= h[j + 1]) { continue; } h[j] += 1; stop.put(i + 1, j + 1); break; } } if (stop.containsKey(k)) { out.println(stop.get(k)); } else { fail(); } } static void debug(Object... args) { for (Object a : args) { out.println(a); } } static int dist(Pair<Integer, Integer> a, Pair<Integer, Integer> b) { return Math.abs(a.first - b.first) + Math.abs(a.second - b.second); } static void y() { out.println("YES"); } static void n() { out.println("NO"); } static int[] stringToArray(String s) { return s.chars().map(x -> Character.getNumericValue(x)).toArray(); } static <T> T min(T a, T b, Comparator<T> C) { if (C.compare(a, b) <= 0) { return a; } return b; } static <T> T max(T a, T b, Comparator<T> C) { if (C.compare(a, b) >= 0) { return a; } return b; } static void fail() { out.println("-1"); } static class Pair<T, R> { public T first; public R second; public Pair(T first, R second) { this.first = first; this.second = second; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "Pair{" + "a=" + first + ", b=" + second + '}'; } public T getFirst() { return first; } public R getSecond() { return second; } } static <T, R> Pair<T, R> make_pair(T a, R b) { return new Pair<>(a, b); } static long mod_inverse(long a, long m) { Number x = new Number(0); Number y = new Number(0); extended_gcd(a, m, x, y); return (m + x.v % m) % m; } static long extended_gcd(long a, long b, Number x, Number y) { long d = a; if (b != 0) { d = extended_gcd(b, a % b, y, x); y.v -= (a / b) * x.v; } else { x.v = 1; y.v = 0; } return d; } static class Number { long v = 0; public Number(long v) { this.v = v; } } static long lcm(long a, long b, long c) { return lcm(a, lcm(b, c)); } static long lcm(long a, long b) { long p = 1L * a * b; return p / gcd(a, b); } static long gcd(long a, long b) { while (b != 0) { long t = b; b = a % b; a = t; } return a; } static long gcd(long a, long b, long c) { return gcd(a, gcd(b, c)); } static class ArrayUtils { static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void print(char[] a) { for (char c : a) { out.print(c); } out.println(""); } static int[] reverse(int[] data) { int[] p = new int[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static void prefixSum(long[] data) { for (int i = 1; i < data.length; i++) { data[i] += data[i - 1]; } } static void prefixSum(int[] data) { for (int i = 1; i < data.length; i++) { data[i] += data[i - 1]; } } static long[] reverse(long[] data) { long[] p = new long[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static char[] reverse(char[] data) { char[] p = new char[data.length]; for (int i = 0, j = data.length - 1; i < data.length; i++, j--) { p[i] = data[j]; } return p; } static int[] MergeSort(int[] A) { if (A.length > 1) { int q = A.length / 2; int[] left = new int[q]; int[] right = new int[A.length - q]; System.arraycopy(A, 0, left, 0, q); System.arraycopy(A, q, right, 0, A.length - q); int[] left_sorted = MergeSort(left); int[] right_sorted = MergeSort(right); return Merge(left_sorted, right_sorted); } else { return A; } } static int[] Merge(int[] left, int[] right) { int[] A = new int[left.length + right.length]; int i = 0; int j = 0; for (int k = 0; k < A.length; k++) { // To handle left becoming empty if (i == left.length && j < right.length) { A[k] = right[j]; j++; continue; } // To handle right becoming empty if (j == right.length && i < left.length) { A[k] = left[i]; i++; continue; } if (left[i] <= right[j]) { A[k] = left[i]; i++; } else { A[k] = right[j]; j++; } } return A; } static long[] MergeSort(long[] A) { if (A.length > 1) { int q = A.length / 2; long[] left = new long[q]; long[] right = new long[A.length - q]; System.arraycopy(A, 0, left, 0, q); System.arraycopy(A, q, right, 0, A.length - q); long[] left_sorted = MergeSort(left); long[] right_sorted = MergeSort(right); return Merge(left_sorted, right_sorted); } else { return A; } } static long[] Merge(long[] left, long[] right) { long[] A = new long[left.length + right.length]; int i = 0; int j = 0; for (int k = 0; k < A.length; k++) { // To handle left becoming empty if (i == left.length && j < right.length) { A[k] = right[j]; j++; continue; } // To handle right becoming empty if (j == right.length && i < left.length) { A[k] = left[i]; i++; continue; } if (left[i] <= right[j]) { A[k] = left[i]; i++; } else { A[k] = right[j]; j++; } } return A; } static int upper_bound(int[] data, int num, int start) { int low = start; int high = data.length - 1; int mid = 0; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (data[mid] < num) { low = mid + 1; } else if (data[mid] >= num) { high = mid - 1; ans = mid; } } if (ans == -1) { return 100000000; } return data[ans]; } static int lower_bound(int[] data, int num, int start) { int low = start; int high = data.length - 1; int mid = 0; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (data[mid] <= num) { low = mid + 1; ans = mid; } else if (data[mid] > num) { high = mid - 1; } } if (ans == -1) { return 100000000; } return data[ans]; } } static boolean[] primeSieve(int n) { boolean[] primes = new boolean[n + 1]; Arrays.fill(primes, true); primes[0] = false; primes[1] = false; for (int i = 2; i <= Math.sqrt(n); i++) { if (primes[i]) { for (int j = i * i; j <= n; j += i) { primes[j] = false; } } } return primes; } // Iterative Version static HashMap<Integer, Boolean> subsets_sum_iter(int[] data) { HashMap<Integer, Boolean> temp = new HashMap<Integer, Boolean>(); temp.put(data[0], true); for (int i = 1; i < data.length; i++) { HashMap<Integer, Boolean> t1 = new HashMap<Integer, Boolean>(temp); t1.put(data[i], true); for (int j : temp.keySet()) { t1.put(j + data[i], true); } temp = t1; } return temp; } static HashMap<Integer, Integer> subsets_sum_count(int[] data) { HashMap<Integer, Integer> temp = new HashMap<>(); temp.put(data[0], 1); for (int i = 1; i < data.length; i++) { HashMap<Integer, Integer> t1 = new HashMap<>(temp); t1.merge(data[i], 1, ADD); for (int j : temp.keySet()) { t1.merge(j + data[i], temp.get(j) + 1, ADD); } temp = t1; } return temp; } static class Graph { static ArrayList<Integer>[] g; static boolean[] visited; static ArrayList<Integer>[] graph(int n) { ArrayList<Integer>[] g = new ArrayList[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } return g; } static void BFS(int s) { Queue<Integer> Q = new ArrayDeque<>(); visited[s] = true; Q.add(s); while (!Q.isEmpty()) { int v = Q.poll(); for (int a : g[v]) { if (!visited[a]) { visited[a] = true; Q.add(a); } } } } } static class SparseTable { int[] log; int[][] st; public SparseTable(int n, int k, int[] data, BiFunction<Integer, Integer, Integer> f) { log = new int[n + 1]; st = new int[n][k + 1]; log[1] = 0; for (int i = 2; i <= n; i++) { log[i] = log[i / 2] + 1; } for (int i = 0; i < data.length; i++) { st[i][0] = data[i]; } for (int j = 1; j <= k; j++) for (int i = 0; i + (1 << j) <= data.length; i++) st[i][j] = f.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } public int query(int L, int R, BiFunction<Integer, Integer, Integer> f) { int j = log[R - L + 1]; return f.apply(st[L][j], st[R - (1 << j) + 1][j]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 2048); 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public int[] readAllInts(int n) { int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); } return p; } public int[] readAllInts(int n, int s) { int[] p = new int[n + s]; for (int i = s; i < n + s; i++) { p[i] = in.nextInt(); } return p; } public long[] readAllLongs(int n) { long[] p = new long[n]; for (int i = 0; i < n; i++) { p[i] = in.nextLong(); } return p; } public long[] readAllLongs(int n, int s) { long[] p = new long[n + s]; for (int i = s; i < n + s; i++) { p[i] = in.nextLong(); } return p; } public double nextDouble() { return Double.parseDouble(next()); } } static void exit(int a) { out.close(); err.close(); System.exit(a); } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static OutputStream errStream = System.err; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); static PrintWriter err = new PrintWriter(errStream); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
e86a3fd8ee01955289582522cabb3063
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(int y = 0; y < t; y++) { int answer = 0; StringTokenizer jo = new StringTokenizer(f.readLine()); int n = Integer.parseInt(jo.nextToken()); int k = Integer.parseInt(jo.nextToken()); int[] ar = new int[n]; StringTokenizer job = new StringTokenizer(f.readLine()); for(int i = 0; i < n; i++) { int temp = Integer.parseInt(job.nextToken()); ar[i] = temp; } int back = 0; a:for(int i = 0; i < n-1; i++) { answer=-2; if(ar[i]>=ar[i+1]){continue;} int p=0;p=i; while(ar[i]<ar[i+1]) { if(i!=0) { while(ar[p]==ar[p-1]) {p--;if(p==0){break;}} } if(p>0){back = Math.min(ar[p-1],ar[i+1]);}else{back=ar[i+1];} int dif = back-ar[i]; int vals = i-p+1; if(dif*vals<k){for(int ii = i; ii > p-1; ii--){ar[ii]+=dif;}k-=(dif*vals);} else {if(k%vals==0){answer=p;}else{answer=i+1-(k%vals);}break a;} } } if(n==1){answer=-2;} System.out.println(answer+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
4ca007e77d6f6c21e38599ac4c629394
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package Practise; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class NewColony { public static void main(String args[]) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t>0) { int n=fs.nextInt(); int k=fs.nextInt(); int []h=new int[n]; h=fs.readArray(n); int index=-1; while(k>0) { int i=0; boolean flag=false; for(;i<n-1;i++) { if(h[i]<h[i+1]) { h[i]++; k--; index=i; flag=true; break; } } if(!flag) { index=-1; break; } if(k==0) break; } System.out.println(index==-1?index:index+1); t--; } } private static int[] readArray(int n) { // TODO Auto-generated method stub return null; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d9ce98041a30eabeef41d7c5827ce09b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package com.mycompany.newcolony; import java.util.Scanner; public class NewColony { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = Integer.valueOf(scan.nextLine()); for (int f = 0; f < t; f++) { String[] fst = scan.nextLine().split(" "); int n = Integer.valueOf(fst[0]); long k = Long.valueOf(fst[1]); String[] scn = scan.nextLine().split(" "); long[] mts = new long[n]; for (int i = 0; i < n; i++) { mts[i] = Long.valueOf(scn[i]); } long x = -2; for (long i = 0; i < k; i++) { x = -2; for (int c = 0; c < n - 1; c++) { if (mts[c] < mts[c + 1]) { mts[c]++; x = c; break; } } if (x == -2) { break; } } System.out.println(x + 1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3d3e3963e219ac0352784cabe5d15375
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Practice{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); int n = s.nextInt(); while (n-- > 0) { int x = s.nextInt(); int y = s.nextInt(); int height[]=new int[x]; for(int i=0;i<x;i++) { height[i]=s.nextInt(); } int index=0; boolean reached=false; for(int i=0;i<y-1;i++) { index=0; while(index<height.length-1) { if(height[index]>=height[index+1]) { index++; reached=(index+1)==height.length; if(reached) { break; } } else { height[index]++; break; } } if(reached) { break; } } index=0; while(index<height.length-1) { if(height[index]>=height[index+1]) { index++; } else { height[index]++; break; } } if(index==height.length-1) { System.out.println(-1);} else { System.out.println(index+1);} } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
5099d0ac78f08f647dfd018e0c4a92d0
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class codeforce { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t= sc.nextInt(); while(t--!=0) { int n = sc.nextInt(); int sum=0; int ans =0; int k = sc.nextInt(); int a[] =new int [n]; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); } while(sum<k) { int i=1; for( i=1;i<n;i++) { if(a[i-1]<a[i]) { a[i-1]++; ans= i-1; sum++; break; } } if(i==n) break; } if(sum<k)System.out.println("-1"); else System.out.println(ans+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c18911d01f6100dd48af6819df5fc00f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class code{ public static void main (String[] args) throws java.lang.Exception { Scanner obj = new Scanner(System.in); int t = obj.nextInt(); for(int j=0;j<t;j++){ int n = obj.nextInt(); int k = obj.nextInt(); int count = 0; int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = obj.nextInt(); } int fin = 0; for(int i=0;i<n-1;i++){ //System.out.println(count); if(arr[i]<arr[i+1]){ arr[i] = arr[i]+1; count += 1; if(count==k){ fin = i; break; } if(i>=1) i = i-2; else i = -1; } if(i==n-2) fin = n-1; } if(fin<n-1) System.out.println(fin+1); else System.out.println(-1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c5e7ffa5a9a5caa9a4731b9b3b51c6bf
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// package com.company; import java.io.File; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { try{ File file = new File("in.rtf"); Scanner input = null; if(file.exists()) input = new Scanner(file); else input = new Scanner(System.in); //---------------------INPUT------------ int t = input.nextInt(); int n, k; int arr[]; for(int i =0;i<t;i++){ n=input.nextInt(); k=input.nextInt(); arr = new int[n]; for(int j=0;j<n;j++) arr[j] = input.nextInt(); for(int j=0;j<n;j++){ if(j+1==n) { System.out.println(-1); break; } if(arr[j]<arr[j+1]){ if(j==0) { if(arr[j+1]-arr[j]>=k){ System.out.println(1); break; } else { k-=arr[j+1]-arr[j]; arr[j] += arr[j+1]-arr[j]; j=-1; } } else{ if(arr[j+1]>arr[j-1]){ if(arr[j-1]==arr[j]){ if(k-1==0){ System.out.println(j+1); break; } k--; arr[j]++; j=-1; } else { if(arr[j-1]-arr[j] >= k){ System.out.println(j+1); break; } k -= arr[j-1]-arr[j]; arr[j] += arr[j-1]-arr[j]; j=-1; } } else{ if(arr[j+1]-arr[j] >= k){ System.out.println(j+1); break; } k -= arr[j+1]-arr[j]; arr[j] += arr[j+1]-arr[j]; j=-1; } } } } // for(int j=0;j<n;j++){ // System.out.print(arr[j]+" "); // } // System.out.println(); // System.out.println(k); } input.close(); } catch (Exception e) { e.printStackTrace(); } } } /* */
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
35ccca3353b8effbdd6b9cc96691f2ad
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class second { private static final Scanner scanner = new Scanner(System.in); public static void main(String [] args) { int t = scanner.nextInt(); for(int i=0; i<t; i++) { int n = scanner.nextInt(); int k = scanner.nextInt(); int [] a= new int[n]; for(int j=0; j<n;j++) { a[j] = scanner.nextInt(); } if(n==1) { System.out.println(-1); }else { boolean found = false; boolean end = false; int loc = 0; while(k>0 && found == false && end == false) { for (int j = 0; j < n; j++) { if (j<n-1 && a[j] < a[j + 1]) { k --; a[j]++; if (k <= 0) { found = true; loc = j + 1; } break; } if(j == n-1 && a[j]- a[j-1] >=k){ found= true; loc = 1; break; } if(j == n-1 && a[j] <= a[j-1]){ end= true; break; } } } if(found) { System.out.println(loc); }else { System.out.println(-1); } } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
62be5ad9364c0aa1fcff30d59181e4ad
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// package codeforces; import java.util.Scanner; public class boulder { static int sol(int arr[], int n, int k){ int ans=0; for(int i=0; i<n; i++){ if(k==0){ return ans; } if(i==n-1){ return -1; } if(arr[i]<arr[i+1]){ while(arr[i]<arr[i+1]&&k!=0){ arr[i]+=1; ans=i; k--; if(k==0){ return ans; } if(i==n-1){ return -1; } } i=0; } } return ans; } static int sol2(int[] a, int n, int k){ int ans=0; while(k>0){ k--; int i=0; while(i<n-1){ if(a[i]<a[i+1]){ a[i]++; break; } i++; } if(i==n-1){ return -1; }else{ ans=i; } } return ans; } public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t!=0){ int n=s.nextInt(); int k=s.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++){ arr[i]=s.nextInt(); } // int[] arr= {1,8}; // int n=2; // int k=7; int x=(sol2(arr, n, k)); if(x==-1){ System.out.println(x); }else{ System.out.println(x+1); } t--;} s.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6e344574bbea80790b4c1ef5340e02e0
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class TP { public static PrintWriter out = new PrintWriter(System.out); public static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = 1; t = ni(); while (t-- > 0) solve(); out.flush(); } private static void solve() { int n = ni(), k = ni(); int[] h = new int[n + 2]; for (int i = 1; i <= n; i++) { h[i] = ni(); } h[n + 1] = -1; while (k > 0) { k--; int i = 1; while (i <= n) { if (h[i] < h[i + 1]) { h[i]++; break; } i++; } if (i <= n) { if (k == 0) { System.out.println(i); } } else { System.out.println(-1); return; } } } private static int ni() { return in.nextInt(); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static long nl() { return in.nextLong(); } private float nf() { return in.nextFloat(); } private static double nd() { return in.nextDouble(); } public static int[] facs(int n, int[] primes) { int[] ret = new int[9]; int rp = 0; for (int p : primes) { if (p * p > n) break; int i; i = 0; while (n % p == 0) { n /= p; i++; } if (i > 0) ret[rp++] = p; } if (n != 1) ret[rp++] = n; return Arrays.copyOf(ret, rp); } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 }; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
013b28d823ebc050889762477caba69f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- > 0) { int n = scn.nextInt(); int k = scn.nextInt(); int[]mountainHeights = new int[n]; for(int i=0;i<n;i++) { mountainHeights[i]=scn.nextInt(); } int pos = -1; int f = 0; while(k-->0){ pos = -1; for(int i=0;i<n-1;i++) { if(mountainHeights[i]<mountainHeights[i+1]) { pos = i+1; mountainHeights[i] = mountainHeights[i]+1; break; } } if(pos==-1) { break; } } System.out.println(pos); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
0149ca6a155607d49c1e2f493e9020ca
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/* I FOLLOW THE WAY OF THE RIGHT HONOURABLE JIM KWIK, GOD BLESS YOU SIR 1. I AM A 'NEWBIE' IN RATING 2. I WILL FAIL MANY TIMES, BUT I WILL NOT FAIL TO LEARN FROM FAILURE 3. I WILL NEVER SURRENDER MY GOAL, NO MATTER HOW MUCH I FAIL 4. I WILL PROVE MY SKILLS WHEN THE TIME COMES 5. I WILL ALWAYS KEEP TRYING */ import java.util.*; import java.io.*; public class Main{ static boolean tests = true; static boolean rev_sort(int[] h, int j){ for (int i = 0; i <= j; ++i) if (h[i] < h[i+1]){ return false; } return true; } static void solve(){ int n = fs.nextInt(); int k = fs.nextInt(); int[] h = fs.readIntArray(n); if (n == 1){ out.println(-1); return; } if (h[0] < h[1]){ k -= (h[1]-h[0]); h[0] = h[1]; if (k <= 0){ out.println(1); return; } } for (int i = 1; i+1 < n; ++i){ while (!rev_sort(h, i)){ ++h[i]; --k; if (k == 0 && !rev_sort(h, i)){ out.println(i+1); return; } for (int j = i-1; j >= 0; --j){ if (h[j] >= h[j+1]) break; ++h[j]; --k; if (k == 0){ out.println(j+1); return; } } } } out.println(-1); } static FastScanner fs; static PrintWriter out; static int int_max = (int)1e9, mod = int_max+7; public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); int t = tests ? fs.nextInt() : 1; while (t-- > 0) solve(); 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[] readIntArray(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; } long nextLong() { return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
685e84b97bd9cc20724437ebcd7f12ac
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Main { public static void main (String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int k=scan.nextInt(); int[] arr=new int[n+1]; for(int i=1;i<=n;i++) arr[i]=scan.nextInt(); int i=0; for(int j=0;j<k;j++){ for(i=1;i<n;i++){ if(arr[i]<arr[i+1]){ arr[i]+=1; break; } } if(i==n) { i=-1; break; } } if(i==-1) System.out.println("-1"); else System.out.println(i); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
2c6e4a34ee1b48545063f9855a381125
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main{ private static final int[] h = new int[101]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0 ; i < t ; ++i) solve(scanner, stringBuilder); System.out.println(stringBuilder); } public static void solve(Scanner scanner , StringBuilder stringBuilder) { int n = scanner.nextInt(); int k = scanner.nextInt(); for (int i = 0 ; i < n ; ++i) { h[i] = scanner.nextInt(); } int index = -2; while (k > 0) { index = -2; for (int i = 1 ; i < n ; ++i) { if (h[i - 1] < h[i]) { h[i - 1]++; k--; index = i - 1; break; } } if (index == -2) break; } stringBuilder.append(index + 1).append('\n'); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
cedde5e0ea03c8e25132640e352f44b5
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Codeforces{ 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(); int k = sc.nextInt(); int[] h = new int[n]; for(int j=0;j<n;j++) { h[j]=sc.nextInt(); } int index = 0; boolean temp = false; if(n==1) { index=-1; } else { while(true) { temp = false; index = 0; for(int j=0;j<n-1;j++) { if(h[j]<h[j+1]) { k-=1; h[j]+=1; if(k==0) { index = j+1; temp =true; break; } else { break; } } else { if(j==n-2) { index = -1; temp =true; break; } } } if(temp) { break; } } } System.out.println(index); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
54c7f800b4c14c3915326ee7f102f2d8
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class Codeforces{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int flag=0; int i = 1; int bd=0; while(i<n&&k>0){ if(arr[i-1]<arr[i]){ arr[i-1]++; bd=i; k--; i=1; continue; } else if(i==n-1 && arr[i-1]>=arr[i]){ flag=1; break; } i++; } /* while(k-->0) { for(i = 1 ;i < n ; i++) { if(arr[i-1]<arr[i]) { arr[i-1]++; break; } else if(i==n-1&&arr[i-1]>=arr[i]) { flag=1; break; } } } */ if(flag!=1 && bd!=0) System.out.println(bd); else System.out.println(-1); } sc.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a78036617261dd1914c6b57b137121d7
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class B { private static PrintWriter out; 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 int[] h = new int[100]; public static void main(String[] args) { FS sc = new FS(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), k = sc.nextInt(); for (int i = 0; i < n; ++i) h[i] = sc.nextInt(); if (n == 1) {out.println(-1);continue;} int diff = 0; for (int i = n - 2, max = h[n-1]; i >= 0; --i) { if (max > h[i]) diff += max - h[i]; else max = h[i]; } // out.print(diff+"\t"); if (k > diff) {out.println(-1); continue;} int p = 0; for (int b = 0, i = 0; b < k; ++b) { while(h[i] >= h[i+1]) ++i; ++h[i]; p = i+1; if (i > 0) --i; } out.println(p); } out.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b36bf7ef5c00a2ce0742add21dd163a6
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class B { private static PrintWriter out; 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());} } public static void main(String[] args) { FS sc = new FS(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), k = sc.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; ++i) h[i] = sc.nextInt(); if (n == 1) {out.println(-1); continue;} int diff = 0; for (int i = n - 2, max = h[n-1]; i >= 0; --i) { if (max > h[i]) diff += max - h[i]; else max = h[i]; } // out.print(diff+"\t"); if (k > diff) {out.println(-1); continue;} int p = 0; for (int b = 0, i = 0; b < k; ++b) { while(h[i] >= h[i+1]) ++i; ++h[i]; p = i+1; if (i > 0) --i; } out.println(p); } out.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9d6a949454bb41a3338aceaf3583d3e9
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args)throws IOException { InputReader in = new InputReader(); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); outer: while (t-- > 0) { int n = in.nextInt(); long k = in.nextLong(); int []a = in.readArray(n); int i = 0; while (i < n - 1 && k > 0) { if (a[i] < a[i + 1]) { a[i]++; k--; if (k == 0) { pw.println(i + 1); continue outer; } if (i > 0 && a[i - 1] < a[i]) i--; } else i++; } pw.println(-1); } pw.close(); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } ArrayList<Integer> lst = new ArrayList<>(); for (int i : a) lst.add(i); Collections.sort(lst); for (int i = 0; i < n; i++) a[i] = lst.get(i); } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int []a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long []a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int [][] read2dArray(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; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
8a560a66189d176a2a13fdff4c570e3c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class B { public static void main(String args[]) { Scanner sv = new Scanner(System.in); int n = sv.nextInt(); for (int i = 0; i < n; i++) { int a = sv.nextInt(); long b = sv.nextInt(); int[] array3=new int[a]; int[] array = new int[a]; for (int j = 0; j < array.length; j++) { array[j] = sv.nextInt(); array3[j]=array[j]; } // Arrays.sort(array3); if (array3[array3.length-1]*array.length-1<=b) { System.out.println("-1"); } else { int[] array2 = new int[(int) b]; int ll = (int) b; int lenth = 0; while (b > 0) { int count4 = 0; for (int j = 0; j < array.length - 1; j++) { if (array[j] >= array[j + 1]) { count4++; } else { if (b > 0) { array[j]++; b--; array2[lenth] = j + 1; lenth++; break; } } } if (count4 == array.length - 1) { break; } } long k = array2[ll - 1]; if (k != 0) { System.out.println(k); } else { System.out.println("-1"); } } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
4de99022e8f115beb8670159ae5db559
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class colony{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); while(a-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int ar[]=new int[n]; for(int i=0;i<n;i++) ar[i]=sc.nextInt(); int b=k; int h=0; while(b>0){ h=0; boolean flag=false; while(h<n-1){ if(ar[h]>=ar[h+1]) h++; else{ ar[h]+=1; break; } } if(h==n-1){ break; } b--; } if(h==n-1) System.out.println(-1); else System.out.println(h+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a12803e08dde192c672843220e7d3401
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.math.*; import java.util.*; import java.lang.*; import java.io.*; public class cf699_2b { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t=1; t=sc.nextInt(); for (int i = 0; i < t; i++) { int n=sc.nextInt(); long k=sc.nextLong(); int[] arr=new int[n]; for(int j=0;j<n;j++) { arr[j]=sc.nextInt(); } /*long[] max=new long[n]; long tot=0L; max[n-1]=arr[n-1]; for(int j=n-2;j>-1;--j) { max[j]=Math.max(arr[j],max[j+1]); tot+=max[j]-arr[j]; } if(k>tot) System.out.println("-1"); else { /*long x=0; int p=0; while(x<k) { if(arr[p]<arr[p+1]) { x+=arr[p+1]-arr[p]; arr[p]=arr[p+1]; } if(x>=k) break; if(p>0&&p<n-1) { if(arr[p-1]<arr[p]) { p=p-1; } else { p=p+1; } } else if(p==0) ++p; else if(p==n-1) --p; */ /* int cur=0; for(long j=0L;j<k;) { for(int p=0;p<n;p++) { if(p==n-1) { cur=-2; break; } if(arr[p]<arr[p+1]) { j+=arr[p+1]-arr[p]; arr[p]=arr[p+1]; cur=p; break; } } if(cur==-2) break; } System.out.println(cur+1); */ int j=0,cn=0,flag=0; while(j<n-1) { if(arr[j]<arr[j+1]) { ++cn; ++arr[j]; if(cn==k) { System.out.println(j+1); flag=1; break; } j=-1; } ++j; } if(flag==0) System.out.println("-1"); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c9336877a90f9ff6334cf6ec931d2e1a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.*; public class abc { static PrintWriter pw; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static boolean isPrime(long n) { if (n == 2) return true; int i = 2; while (i * i <= n) { if (n % i == 0) return false; i++; } return true; } public static long[] remove(long n) { long res[] = new long[1000000000]; long rese = 0; int i = 0; while (n > 0) { long dig = n % 10; n = n / 10; if (dig > 0) { rese = dig; res[i++] = rese; } } return res; } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static class pair { int x; int y; public pair(int a, int b) { x = a; y = b; } } public static class Comp implements Comparator<pair> { public int compare(pair a, pair b) { if (a.x != b.x) { return a.x - b.x; } else { return a.y - b.y; } } } public static void extract(ArrayList<Integer> ar, int k, int d) { int c = 0; for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) { long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm) ar.add(i); } } public static void dfs(int index, boolean vis[], int a[], int b[], int n) { vis[index] = true; for (int i = 0; i < n; i++) { if (!vis[i] && (a[i] == a[index] || b[i] == b[index])) dfs(i, vis, a, b, n); } } public static int countSetBitsUtil(int x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2); } public static void main(String[] args) { FastReader ob = new FastReader(); pw = new PrintWriter(System.out); int t = ob.nextInt(); while (t-- > 0) { int n = ob.nextInt(); int k = ob.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = ob.nextInt(); int b = 0,key=0; int c = 0; while(true) { for (int i = 0; i < n; i++) { if(i==n-1) { c=-1; break; } if(a[i] < a[i+1]) { a[i]++; b=i; break; } } key++; if(c==-1 || key==k) break; } if(c==-1) pw.println("-1"); else pw.println(b+1); } pw.flush(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b3404066661d83e7a1e90690d5a6d446
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package recursion; import java.util.*; import java.util.ArrayList; import java.util.List; public class EqualSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- != 0) { int n = sc.nextInt(); int k = sc.nextInt(); int h[] = new int[n]; for(int i = 0;i<n;i++) { h[i] = sc.nextInt(); } if(n==1) { System.out.println(-1); continue; } int c = 0; int j = 0; for(int i = 0;i<k;i++) { j = 0; while(j<n-1 && h[j]>=h[j+1]) { j++; } if(j==n-1) break; h[j]+=1; } if(j == n-1) System.out.println(-1); else System.out.println(j+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
df0752cd0cec658bd62c46b9fd244fa7
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Stack; import java.util.StringTokenizer; public class contest { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); int[] nums = new int[N]; st = new StringTokenizer(br.readLine()); for (int j = 0; j < N; j++) { nums[j] = Integer.parseInt(st.nextToken()); } pw.println(solve(nums, K)); } pw.close(); br.close(); } static int solve(int[] nums, int k) { Stack<int[]> heights = new Stack(); List<Integer> order = new ArrayList(); for (int i = 0; i < nums.length; i++) { int cur = 0; if (!heights.isEmpty() && heights.peek()[0] < nums[i]) { int[] last = heights.pop(); cur += last[1]; for (int j = 0; j < nums[i] - last[0]; j++) { while(!heights.isEmpty() && heights.peek()[0] == last[0] + j){ // System.out.println("YES"); cur += heights.pop()[1]; } int t = 0; for (int o = i - 1; t < cur; t++, o--) { // System.out.println(i + " " + o + " " + cur); order.add(o); } } } heights.push(new int[]{nums[i], cur+1}); // System.out.println(Arrays.toString(heights.peek())); } return (k > order.size()) ? -1 : (order.get(k - 1) + 1); } } /* 1 8 10 5 4 3 4 3 2 4 9 */
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b26ca3c2ea4e7e61e518bdf0e04a0955
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); int t=f.nextInt(); // int t=1; PrintWriter out=new PrintWriter(System.out); while(t>0) { t--; int n=f.nextInt(); int k=f.nextInt(); int[] l=f.readArray(n); int ans=-1; if(k>10001) { System.out.println(-1); } else { for(int j=0;j<k;j++) { for(int i=0;i<n-1;i++) { if(l[i+1]>l[i] && j==k-1 ) { ans=i+1; } if(l[i+1]>l[i]) { l[i]++; break; } } } System.out.println(ans); } } out.close(); } 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 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()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
679a1243e454c7f3853a731a251128f8
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int t = 0; t < T; t++) { int N = sc.nextInt(); int K = sc.nextInt(); int[] mountains = new int[N]; for(int i = 0; i < N; i++) mountains[i] = sc.nextInt(); int idx = 0; boolean f = false; while(K > 0) { f = false; end : for(int i = 1; i < N; i++) { if(mountains[i - 1] - mountains[i] < 0) { mountains[i - 1]++; K--; f = true; idx = i; break end; } } if(!f) break; } if(!f) System.out.println(-1); else System.out.println(idx); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c316df8f4e2adee3d0660600c70603f3
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author MahmoudJobeel */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BNewColony solver = new BNewColony(); solver.solve(1, in, out); out.close(); } static class BNewColony { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = sc.nextIntArray(n); int pos = -1; out: while (true) { for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { arr[i - 1]++; if (k > 0) pos = i; k--; continue out; } } break; } pw.println(k <= 0 ? pos : -1); } } } static class Scanner { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public Scanner(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } public int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
f00c1fba29983f5cfb76d52dc3a58737
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=1; t=sc.nextInt(); //int t=Integer.parseInt(br.readLine()); while(--t>=0){ int n=sc.nextInt(); int k=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); int z=-1,count=0; while(true){ for(int i=0;i<n-1;i++){ if(a[i]<a[i+1]){ a[i]++; count++; z=i; break; } } if(count==k||z==-1){ break; } z=-1; } if(z==-1||n==1){ System.out.println(-1); } else{ System.out.println(z+1); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
5c130830c1c2857fa780d5ffcf3a6263
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(); int k=ni(); int[]A=nai(n); while(true) { int flag=0; for(int i=0;i<n-1;i++) { if(A[i]<A[i+1]) { flag=1; if(k==1) {pn(i+1);return;} k--; A[i]++; break; } } //pn(Arrays.toString(A)); if(flag==0) {pn(-1);return;} } } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3e80b71f4a1786c4b5fc648c74bd5d1d
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static long M = (long) (1e9 + 7); 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 { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void heapify(int arr[], int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify(arr, n, largest); } } public static void swap(long[] a, int i, int j) { int temp = (int) a[i]; a[i] = a[j]; a[j] = temp; } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b / gcd(a, b)); } public static String sortString(String inputString) { // Converting input string to character array char[] tempArray = inputString.toCharArray(); // Sorting temp array using Arrays.sort(tempArray); // Returning new sorted string return new String(tempArray); } static boolean isSquare(int n) { int v = (int) Math.sqrt(n); return v * v == n; } static boolean PowerOfTwo(int n) { if (n == 0) return false; return (int) (Math.ceil((Math.log(n) / Math.log(2)))) == (int) (Math.floor(((Math.log(n) / Math.log(2))))); } static int power(long a, long b) { long res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a) % M; } a = ((a * a) % M); b = b / 2; } return (int) res; } public static boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) { return false; } return true; } static long computeXOR(long n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader();int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i <n ; i++) arr[i] = sc.nextInt(); int x = 0; while(k--!=0) { x = 0; while (x < n - 1) { if (arr[x] >=arr[x+1]) x++; else{ arr[x] = arr[x]+1; break; } } if(x==n-1) break; } if(x==n-1) System.out.println(-1); else System.out.println(x+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
faa2ad464d23ffe05bee790dd8446595
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class newColony { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t != 0) { int n = s.nextInt(); int k = s.nextInt(); ArrayList<Integer> heights = new ArrayList<>(); for(int i = 0; i < n; i++) { heights.add(s.nextInt()); } int count = 0; while(true) { count++; if(count >= k) { break; } boolean stopped = false; for(int i = 0; i < heights.size() - 1; i++) { if(heights.get(i) < heights.get(i + 1)) { heights.set(i, heights.get(i) + 1); stopped = true; break; } } if(!stopped) { break; } //System.out.println(heights); } boolean stopped = false; for(int i = 0; i < heights.size() - 1; i++) { if(heights.get(i) < heights.get(i + 1)) { System.out.println(i + 1); stopped = true; break; } } if(!stopped) { System.out.println(-1); } t--; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
21df8c6b5e8b7c138ac1a83b48a2cd47
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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 k = fs.nextInt(); int[] a = fs.readArray(n); int index = 0; int flag1 = 0; for (int j = 0; j < k; j++) { int f = 0; for (int i = 0; i < n - 1; i++) { if (a[i] < a[i + 1]) { a[i]++; f = 1; index = i; break; } } if (f == 0) { flag1 = 1; System.out.println(-1); continue outer; } } if (flag1 == 0) System.out.println(index + 1); } } 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
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
71609ad7f411dbcc42c3e0a8848e508d
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.PrintStream; public class Solution { public static boolean Local(){ try{ return System.getenv("LOCAL_SYS")!=null; }catch(Exception e){ return false; } } public static boolean LOCAL; static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try{ br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); }catch(FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() throws IOException{ return br.readLine(); } } static class Pair<T,X> { T first; X second; Pair(T first,X second){ this.first = first; this.second = second; } @Override public int hashCode(){ return Objects.hash(first,second); } @Override public boolean equals(Object obj){ return obj.hashCode() == this.hashCode(); } } static PrintStream debug = null; static long mod = (long)(Math.pow(10,9) + 7); public static void main(String[] args) throws Exception { FastScanner s = new FastScanner(); LOCAL = Local(); if(LOCAL){ s = new FastScanner("src/input.txt"); PrintStream o = new PrintStream("src/sampleout.txt"); debug = new PrintStream("src/debug.txt"); System.setOut(o); } long mod = 1000000007; int tcr = s.nextInt(); StringBuilder sb = new StringBuilder(); for(int tc=0;tc<tcr;tc++){ int n = s.nextInt(); int k = s.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); } Stack<Integer> st = new Stack<>(); st.push(0); for(int i=1;i<n;i++){ if(arr[i-1] >= arr[i]){ st.push(i); }else{ break; } } int next_index = st.size(); boolean done = false; int ans = -1; for(int i=0;i<k;i++){ dbg(debug,arr); if(next_index == n){ done = true;break; } if(i == k-1){ ans = st.peek(); break; } arr[st.peek()]++; if(st.size() > 1){ int polled = st.pop(); if(arr[st.peek()] >= arr[polled]){ st.push(polled); }else{ next_index--; } } int curr_index = st.peek(); while((next_index < n) && (arr[next_index] <= arr[st.peek()])){ st.push(next_index); next_index++; } } if(done){ sb.append(-1); sb.append('\n'); }else{ sb.append((ans+1)+"\n"); } } print(sb.toString()); } public static boolean inRange(int r1,int r2,int val){ return ((val >= r1) && (val <= r2)); } static int len(long num){ return Long.toString(num).length(); } static long mulmod(long a, long b,long mod) { long ans = 0l; while(b > 0){ long curr = (b & 1l); if(curr == 1l){ ans = ((ans % mod) + a) % mod; } a = (a + a) % mod; b = b >> 1; } return ans; } public static void dbg(PrintStream ps,Object... o) throws Exception{ if(ps == null){ return; } Debug.dbg(ps,o); } public static long modpow(long num,long pow,long mod){ long val = num; long ans = 1l; while(pow > 0l){ long bit = pow & 1l; if(bit == 1){ ans = (ans * (val%mod))%mod; } val = (val * val) % mod; pow = pow >> 1; } return ans; } public static char get(int n){ return (char)('a' + n); } public static long[] sort(long arr[]){ List<Long> list = new ArrayList<>(); for(long n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } public static int[] sort(int arr[]){ List<Integer> list = new ArrayList<>(); for(int n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } // return the (index + 1) // where index is the pos of just smaller element // i.e count of elemets strictly less than num public static int justSmaller(long arr[],long num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } public static int justSmaller(int arr[],int num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } //return (index of just greater element) //count of elements smaller than or equal to num public static int justGreater(long arr[],long num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static int justGreater(int arr[],int num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static void println(Object obj){ System.out.println(obj.toString()); } public static void print(Object obj){ System.out.print(obj.toString()); } public static int gcd(int a,int b){ if(b == 0){return a;} return gcd(b,a%b); } public static long gcd(long a,long b){ if(b == 0l){ return a; } return gcd(b,a%b); } public static int find(int parent[],int v){ if(parent[v] == v){ return v; } return parent[v] = find(parent, parent[v]); } public static List<Integer> sieve(){ List<Integer> prime = new ArrayList<>(); int arr[] = new int[100001]; Arrays.fill(arr,1); arr[1] = 0; arr[2] = 1; for(int i=2;i<=100000;i++){ if(arr[i] == 1){ prime.add(i); for(long j = (i*1l*i);j<100001;j+=i){ arr[(int)j] = 0; } } } return prime; } static boolean isPower(long n,long a){ long log = (long)(Math.log(n)/Math.log(a)); long power = (long)Math.pow(a,log); if(power == n){return true;} return false; } private static int mergeAndCount(int[] arr, int l,int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static int mergeSortAndCount(int[] arr, int l,int r) { // Keeps track of the inversion count at a // particular node of the recursion tree int count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static class Debug{ //change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; private static <T> String ts(T t) { if(t==null) { return "null"; } try { return ts((Iterable) t); }catch(ClassCastException e) { if(t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; } try { return ts((Object[]) t); }catch(ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: arr) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: iter) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(PrintStream ps,Object... o) throws Exception { if(LOCAL) { System.setErr(ps); System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": ["); for(int i = 0; i<o.length; i++) { if(i!=0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3a8b0e2523b0e9dc78ec60612218087b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class codeforces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int ans = -1; boolean flag = false; int n = sc.nextInt(); int k = sc.nextInt(); int[] list = new int[n+1]; list[n] = 0; for(int i = 0 ; i< n ; i++){ list[i] = sc.nextInt(); } int o =0 ; while ( o < n ) { if(list[o+1] > list[o]) { k -= 1; list[o] = list[o] + 1; if(k <= 0) {ans = o + 1; break;} else { o = 0; continue; } } else o+=1; } System.out.println(ans); } }}
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
0450495971a10f250263b5acf3a0b775
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codeforces { static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here int t=I(); while(t-->0) { int n=I(); long k=L(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=I(); } int q=0,minin=-1; while(k-->0) { int min=Integer.MAX_VALUE; minin=-1; boolean flag=false; for(int i=0;i<n;i++) { if(min>=a[i] && !flag) { min=a[i]; minin=i; } else flag=true; } if(minin==n-1) { q=1; break; } else a[minin]++; } if(q==1) System.out.println("-1"); else System.out.println(minin+1); } } public static ArrayList<Integer> prime(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } public static class DSU { static int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public static int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public static void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static class pair { long a; int b; public pair(long val,int index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return 1; else return -1; } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static long countDigit(long n) { long sum=0; while(n!=0) { sum++; n=n/10; } return sum; } public static long digitSum(long n) { long sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } return sum; } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } 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 I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c4e51c6780e26324983f8fb1daaf3853
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BNewColony solver = new BNewColony(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BNewColony { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); int[] problem = new int[n]; for (int j = 0; j < n; j++) { problem[j] = in.nextInt(); } boulders(n, k, problem, out); } public static void boulders(int n, int k, int[] problem, PrintWriter out) { //greedy choice find the largest value in array other than the 0th index itself. //continuously find it until we run out of k or the boulder just rolls to it's death. int maxIndex = 0; int maxIndexCopy = -1; for (int i = 1; i < n; i++) { if (problem[i] > problem[i - 1]) { maxIndex = i; maxIndexCopy = i; break; } } if (maxIndexCopy == -1) { out.println(-1); return; } else { while (problem[maxIndex] > problem[maxIndex - 1]) { for (int i = maxIndex - 1; i >= 0; i--) { if (k == 0) { out.println(i + 1); return; } if (problem[i] < problem[i + 1]) { problem[i] += 1; k -= 1; } else if (problem[i] >= problem[maxIndex]) { break; } if (k == 0) { out.println(i + 1); return; } } if (problem[maxIndex - 1] == problem[maxIndex]) { int maxIndexCopy2 = maxIndex + 1; maxIndexCopy = -1; for (int i = maxIndexCopy2; i < n; i++) { if (problem[i] > problem[i - 1]) { maxIndex = i; maxIndexCopy = i; break; } } if (maxIndexCopy == -1) { out.println(-1); return; } } } } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
f5ade0d317c0d3708d902aee2832ce05
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int z=sc.nextInt(); for(int z1=0;z1<z;z1++) { int n,k; n=sc.nextInt(); k=sc.nextInt(); int ar[] =new int[n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } int t=0; int j; for(j=0;j<k;j++) { int d=0; t=0; int min=ar[t]; while(d<n) { if(min>=ar[d]&&(t<n-1?(ar[t+1]<=min):true)) {min=ar[d]; t=d;} d++; } if(t!=n-1) ar[t]=ar[t]+1; else break; } /*if(j!=k-1) System.out.println("-1"); else*/ if(t!=n-1) System.out.println(t+1); else System.out.println("-1"); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9665fdd281ec927cecf172b1966b01b2
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import static java.lang.Math.*; import java.util.*; public class Main{ public static void main(String[]args){ Scanner x = new Scanner (System.in); int y = x.nextInt(); List<Integer> z = new ArrayList<>(); List<Integer> s = new ArrayList<>(); while (y-->0){ int r = x.nextInt() , k = x.nextInt(),i=-1,l=1000; s.add(l); while (r-->0){ int v = x.nextInt(); s.add(v); for(int t=s.size()-1;t>1&&k>0;t--){ if(s.get(t)>s.get(t-1)){ int o = min(s.get(t-2)+1,s.get(t)); k = k - (o-s.get(t-1)); s.set(t-1,o); if(k<=0){ i=t-1; break; } } if(t==2&&v>s.get(s.size()-2))t=s.size(); } } z.add(i); s.clear(); } for(int j : z) System.out.println(j); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
4cabb052b8f3eaad44fa0481b69306f9
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int k=s.nextInt(); int[] arr= new int[n+1]; for(int i=1;i<=n;i++) { arr[i]=s.nextInt(); } int lastp=1; for(int i=0;i<k;i++) { if(lastp==-1) { break; } int idx=1; boolean placed=false; while(!placed) { if(idx==n) { lastp=-1; break; } if(arr[idx]>=arr[idx+1]) { idx++; }else { arr[idx]=arr[idx]+1; lastp=idx; placed=true; } if(idx==n) { lastp=-1; break; } } } System.out.println(lastp); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
1862aa70e2fd34741e6c1f5c19f3eea6
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/*package whatever //do not write package name here */ import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; public class praccc { public static void main (String[] args) { //code Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); for(int kk=0;kk<tt;kk++) { int n = sc.nextInt(); long k = sc.nextLong(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); } if(n==1) { System.out.println(-1); continue; } int flag = 0, ind = 0; for(int i=0;i<k;i++) { a[0] = a[0] + 1; for(int j=0;j<n-1;j++) { if(a[j]>a[j+1]) { a[j] = a[j] - 1; a[j+1] = a[j+1] + 1; ind = j; if(j+1==n-1) { flag = 1; break; } } else { ind = j; break; } } if(flag == 1) break; } if(flag == 1) { System.out.println(-1); } else { System.out.println(ind+1); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b1ede3bc214e0051fd0353a0685382f5
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class B{ static int[] dx={-1,1,0,0}; static int[] dy={0,0,1,-1}; static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static ArrayList<Pair>es; static LinkedList<Integer>edges[]; static LinkedList<Integer>edges2[]; static boolean prime[]; static void sieve(int n) { prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } public static int lowerBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } static boolean isPowerOfTwo(long n) { if (n == 0) return false; while (n != 1) { if (n % 2 != 0) return false; n = n / 2; } return true; } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int dist(int x1,int y1,int x2,int y2){ return Math.abs(x1-x2)+Math.abs(y1-y2); } static long cuberoot(long x) { long lo = 0, hi = 1000005; while(lo<hi) { long m = (lo+hi+1)/2; if(m*m*m>x) hi = m-1; else lo = m; } return lo; } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } public static class comp1 implements Comparator<ArrayList<Integer>>{ public int compare(ArrayList<Integer> o1,ArrayList<Integer> o2){ if(o1.size()>0&&o2.size()>0&&o1.get(o1.size()-1)>o2.get(o2.size()-1)) return 1; return -1; } } public static class comp2 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return (o2.ab-o1.ab)>0?1:-1; } } static boolean can(int m,int s) { return (s>=0&&s<=m*9); } static boolean collinear(long x1, long y1, long x2, long y2, long x3, long y3) { long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if(a==0) return true; return false; } static boolean vis[]; static int res=0; static boolean is=false; static void dfs(int x) { if(edges[x].size()!=2) is=true; vis[x]=true; for(int v:edges[x]) { if(!vis[v]) dfs(v); } } static int n; static int arr[]; static boolean ok(int x) { int idx=0; // int tmp[]=new int[n]; int orig=x; int idx1=0,idx2=n-(x+1); ArrayList<Integer>tmp=new ArrayList<Integer>(); for(int i=0;i<2*x+1;i++) { if(i%2==0){ tmp.add(arr[idx2++]); } else tmp.add(arr[idx1++]); } // for(int i=0;i<tmp.size();i++) // out.println(tmp.get(i)); int yes=0; for(int i=1;i<tmp.size()-1;i++) { if(tmp.get(i)<tmp.get(i-1)&&tmp.get(i)<tmp.get(i+1)) yes++; } if(yes>=x) return true; return false; } public static void main(String[] args) throws Exception { /*int xx=253; for(int i=1;i*i<=xx;i++) { if(xx%i==0) { System.out.println(i); System.out.println(xx/i); } }*/ //java.util.Scanner scan=new java.util.Scanner(new File("mootube.in")); // PrintWriter out = new PrintWriter (new FileWriter("mootube.out")); //scan=new FastReader("moocast.in"); //out = new PrintWriter ("moocast.out"); //System.out.println(3^2); //System.out.println(19%4); //StringBuilder news=new StringBuilder("ab"); //news.deleteCharAt(1); //news.insert(0,'c'); //news.deleteCharAt(0); //System.out.println(news); //System.out.println(can(2,15)); //System.out.println(LCM(2,2)); // System.out.println(31^15); //System.out.println(""); //System.out.println(824924296372176000L>(long)1e16); int tt=1; //rec(2020); tt=scan.nextInt(); //int st=scan.nextInt(); //System.out.println(calc(91)); //sieve(21000); //SNWNSENSNNSWNNW // System.out.println(set.remove(new Pair(1,1))); //System.out.println(count("cccccccccccccccccccccccccooooooooooooooooooooooooodddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffforrrrrrrrrrrrrcesssssssssssss","codeforces")); //System.out.println(isPowerOfTwo(446265625L)); //System.out.println("daaa".compareTo("bccc")); //System.out.println(2999000999L>1999982505L); int T=1; outer:while(tt-->0) { int n=scan.nextInt(),k=scan.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=scan.nextInt(); if(k>=10000) { out.println(-1); continue outer; } //int arr[]=new int[n]; that: for(int i=0;i<k;i++) { for(int j=0;j<n-1;j++) { if(arr[j]<arr[j+1]){ if(i==k-1) { out.println(j+1); continue outer; } arr[j]++; continue that; } } } for(int i=0;i<n-1;i++) { if(arr[i]<arr[i+1]) { out.println(i+1); continue outer; } } out.println(-1); } out.close(); } static class dsu{ static int id[]=new int[101]; dsu() { for(int i=0;i<101;i++) id[i]=i; } static int find(int x) { if(x==id[x]) return x; return find(id[x]); } static void connect(int i,int j) { i=find(i); j=find(j); id[i]=j; } static boolean is(int i,int j) { return find(i)==find(j); } } static long binexp(long a,long n,long mod) { if(n==0) return 1; long res=binexp(a,n/2,mod)%mod; res=res*res; if(n%2==1) return (res*a)%mod; else return res%mod; } static class special{ Pair x; Pair y; special(Pair x,Pair y) { this.x=new Pair(x.x,x.y); this.y=new Pair(y.x,y.y); } @Override public int hashCode() { return (int)(x.x + 31 * y.y); } public boolean equals(Object other) { if(other instanceof special) { special e =(special)other; return this.x.x==e.x.x && this.x.y==e.x.y && this.y.x==e.y.x && this.y.y==e.y.y; } else return false; } } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base % mod; long R = powMod(base, exp/2, mod) % mod; R *= R; R %= mod; if ((exp & 1) == 1) { return base * R % mod; } else return R % mod; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } public static class Pair implements Comparable<Pair>{ long x; long y; long ab; long z; public Pair(){} public Pair(long x1, long y1,long z) { x=x1; y=y1; this.z=z; } public Pair(long x1, long y1) { x=x1; y=y1; this.ab=x+y; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { return (int)(x-o.x); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a93d840e42061e35643da67be8739ca8
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String [] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i = 0; i < t; i++) { int n = input.nextInt(); int k = input.nextInt(); int [] arr = new int [n]; for(int j = 0; j < n; j++) arr [j] = input.nextInt(); int answer = solve(arr, k); System.out.println(answer == n ? -1 : answer); } } private static int solve(int [] arr, int k) { int j = 0; for(int i = 0; i < k; i++) { for(j = 0; j < arr.length; j++) { if(j != arr.length - 1 && arr [j] >= arr [j + 1]) continue; else if(j == arr.length - 1) return arr.length; else { arr [j] += 1; break; } } } return j + 1; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
cb3e0c8fdd29e5cfb465ccd01fc1df59
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; import java.util.Collections; public class div2_699_B implements Runnable { public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-- > 0) { int n=sc.nextInt(),k=sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int flag=0,flag1=0; ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<k;i++) { flag=0;flag1=0; for(int j=1;j<n;j++) { if(a[j-1]<a[j]) { a[j-1]++; al.add(j); flag=1; break; } } if(flag==0) { flag1=1; break; } } if(flag1==1) { System.out.println("-1"); } else { System.out.println(al.get(al.size()-1)); } } } static class sortintarray { public static void sort(int[] arr) { int n = arr.length, mid, h, s, l, i, j, k; int[] res = new int[n]; n--; for (s = 1; s <= n; s <<= 1) { for (l = 0; l < n; l += (s << 1)) { h = Math.min(l + (s << 1) - 1, n); mid = Math.min(l + s - 1, n); i = l; j = mid + 1; k = l; while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]); while (i <= mid) res[k++] = arr[i++]; while (j <= h) res[k++] = arr[j++]; for (k = l; k <= h; k++) arr[k] = res[k]; } } } } static class sortchararray { public static void sort(char[] arr) { int n = arr.length, mid, h, s, l, i, j, k; char[] res = new char[n]; n--; for (s = 1; s <= n; s <<= 1) { for (l = 0; l < n; l += (s << 1)) { h = Math.min(l + (s << 1) - 1, n); mid = Math.min(l + s - 1, n); i = l; j = mid + 1; k = l; while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]); while (i <= mid) res[k++] = arr[i++]; while (j <= h) res[k++] = arr[j++]; for (k = l; k <= h; k++) arr[k] = res[k]; } } } } static long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} static long lcm(long a, long b){return (a*b)/gcd(a,b);} static int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} static int lcm(int a, int b){return (a*b)/gcd(a,b);} long factorial(long n){ if (n == 0) return 1; else return(n * factorial(n-1)); } static long myceil(long a, long b){ return (a+b-1)/b; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new div2_699_B(),"Main",1<<27).start(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
4097e0ad03efe634a4f3a72cc0c12373
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), k = sc.nextInt(), flag = -2; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); for (int i = 1; i < n; i++) { int val = arr[i - 1]; while (val < arr[i]) { int j = i - 2; while (j >= 0 && arr[j] == val) j--; int k1; if (j >= 0) k1 = (i - 1 - j) * (Math.min(arr[i], arr[j]) - val); else k1 = (i - 1 - j) * (arr[i] - val); if (k > k1) { k -= k1; for (int in = j + 1; in < i; in++) arr[in] += k1 / (i - 1 - j); } else { if (k % (i - 1 - j) != 0) flag = i - ((k) % (i - 1 - j)); else flag = j + 1; break; } val = arr[i - 1]; } if (flag != -2) break; } System.out.println(flag + 1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
e1d9d9a9fadc2c7c8f74104da6561d1c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class P1481B{ public static void main (String arr[]){ Scanner ip = new Scanner(System .in); int a[]; int n,k,t=ip.nextInt(); int i , j; int ans; boolean b; while (t-->0){ n = ip.nextInt(); k = ip.nextInt(); a = new int [n]; ans = -1; b = false; for (i = 0 ; i < n;i++){ a[i] = ip.nextInt(); } for (i = 0 ; i < k;i++){ for(j = 1 ; j < n ; j++){ if (a[j-1]<a[j]){ a[j-1]++; ans = j; break; } else if (j == n-1){ ans = -1; b = true; break; } } if(b) break; } System.out.println(ans); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
0ac4f1ad11cb489eff381af78f040d6b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); byte t = input.nextByte(); for(byte i = 0; i < t; i++) { byte n = input.nextByte(); int k = input.nextInt(); byte[] h = new byte[n]; for(byte j = 0; j < h.length; j++) { h[j] = input.nextByte(); } System.out.println(kthPosition(h, k)); } input.close(); } public static int kthPosition(byte[] h, int k) { for(int i = 0, count = 1; i < h.length; i++) { if(i < h.length - 1 && h[i] < h[i + 1]) { h[i]++; if(count == k) return i + 1; else if(i > 0) i -= 2; else i -= 1; count++; } } return -1; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6516986451d7a4526bd58b68b48f33f4
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/*input 2 20 645651756 11 21 31 41 51 56 60 70 80 90 98 98 99 99 100 100 100 100 100 100 20 465601593 11 21 31 41 51 61 65 66 76 83 87 92 100 100 100 100 100 100 100 100 */ import java.util.Scanner; import java.util.Arrays; public class CF2{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0){ t--; int n=sc.nextInt(); int k=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int z=0; int index=-1; int max = Arrays.stream(arr).max().getAsInt(); if(max*n<k){ System.out.println(index); continue; } while(k-->0){ index=-1; for(int i=0;i<n-1;i++){ if(arr[i]<arr[i+1]){ arr[i]++; index=i+1; break; } } } System.out.println(index); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
2052823e6e934d8371ee5ed603c26631
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { int i, j; StringBuilder sb = new StringBuilder(); FastReader in = new FastReader(System.in); int t = in.nextInt(); start:while(t-->0) { int n=in.nextInt(), k=in.nextInt(), max=-1; int a[]=new int[n+2]; for(i=1;i<=n;i++) { a[i]=in.nextInt(); max=Math.max(max, a[i]); } if(max*n<k) { sb.append("-1\n"); continue start; } int pos=-1; while(k-->0) { pos=-1; for(i=1;i<=n;i++) { if(a[i]<a[i+1]) { pos=i; break; } } if(pos==-1) break; a[pos]++; } sb.append(pos+"\n"); } System.out.print(sb); } } 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; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b619b5345ad1bebd2ee290774c4f14d6
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); sc.nextLine(); for(int t = 0; t < T; t++){ int n = sc.nextInt(); long k = sc.nextLong(); int[] heights = new int[n]; for(int i = 0; i < n; i++){ heights[i] = sc.nextInt(); } boolean reach = false; int stop = 0; while(k > 0){ stop = 0; while(stop < n - 1 && heights[stop] >= heights[stop+1]) stop++; if (stop == n -1) { reach = true; break; } heights[stop]++; k--; } if (reach) { System.out.println("-1"); } else { System.out.println(stop + 1); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
79c57141cc6153ca8260134cb856562f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int[] arr=new int[n]; int max=0; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); max=Math.max(max,arr[i]); } if(n*max<k){ System.out.println(-1); continue; } int ans=0; for(int b=0;b<k;b++){ int to=-2; for(int i=0;i<n-1;i++){ if(arr[i]<arr[i+1]){ to=i; break; } } ans=to+1; if(to==-2){ break; } arr[to]++; } System.out.println(ans); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
27905b57fda3233b19ad336683f2cc94
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int[] arr= new int[n]; if(n==0){ System.out.println(-1); continue; } for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int ans=-2,flag=0; while(k>0){ // System.out.print("x"+" "); int i=0; for(i=0;i<n-1;i++){ if(arr[i]<arr[i+1]){ k=k-1; arr[i]=arr[i]+1; ans=i; break; } } if(i==n-1){ ans=-2; break; } } System.out.println(ans+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d840764530fa1ee88db059c16e2003bb
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class scratch{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); while (t-->0){ solve(); } } static void solve() throws IOException { String[] s1 = br.readLine().split(" "); int n = Integer.parseInt(s1[0]); int k = Integer.parseInt(s1[1]); String[] s = br.readLine().split(" "); int[] arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(s[i]); } int c = k; int flag = 0; int index = -1; while(c-->0){ flag =0; for(int i = 0;i<n-1;i++){ if(arr[i] < arr[i+1]){ arr[i]++; flag=1; index = i; break; } } if(flag == 0)break; } if(flag == 0){ System.out.println(-1); }else{ System.out.println(index+1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
7dac2004b2faaf319cfaf5c392e76388
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; public class Round699_2 { public static void main(String[] args) { Scanner o=new Scanner(System.in); int t=o.nextInt(); while(t>0) { int mts=o.nextInt(); int bldr=o.nextInt(); int h[]=new int[mts]; for(int i=0;i<mts;i++) { h[i]=o.nextInt(); } int pos=-1; boolean out=false; while(true) { boolean change=false; for(int i=0;i<mts-1;i++) { if(bldr<=0) { out=true; break; } int v=h[i+1]-h[i]; if(h[i+1]-h[i]>0) { bldr=bldr-1; h[i]=h[i]+1; pos=i+1; change=true; break; } } if(out) break; if(!change) { pos=-1; break; } } System.out.println(pos); t--; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
99839611de5de9b0ceebaf0e5c13ae93
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub FastReader s = new FastReader(); int tc = s.nextInt(); for(int t = 0;t < tc;t++) { int n = s.nextInt() , k = s.nextInt(); int[] input = new int[n]; int max = Integer.MIN_VALUE; for(int i = 0;i < n;i++) { input[i] = s.nextInt(); max = Math.max(max, input[i]); } if(n == 1) { System.out.println("-1"); continue; } boolean flag = false; int pos = 0; for(int i = 1;i <= k;i++) { for(int j = 1;j < n;j++) { if(input[j - 1] < input[j]) { input[j - 1]++; pos = j - 1; break; } if(j == n - 1) { flag = true; break; } } if(flag) break; } if(flag) System.out.println("-1"); else System.out.println(pos + 1); // int count = 0; // for(int i = 0;i < n;i++) { // count += max - input[i]; // } // if(max == input[0]) { // count = 0; // // } // if(count < k) { // System.out.println("-1"); // continue; // } // // count = 0; // boolean flag = false; // int pos = 0; // for(int i = 1;i <= k;i++) { // for(int j = 1;j < n;j++) { // if(input[j] > input[j - 1]) { // input[j - 1]++; // count++; // if(i == k) pos = (j - 1); // } // } // } // // System.out.println(pos); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
336deb02c00621116e862b0121969f26
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class Main { private static final Scanner scanner = new Scanner(System.in); private static final int OO = (int) 1e9 + 1; private static final double PI = (double) 22 / 7; private static final int[] dx4 = {0, 1, 0, -1}, dx = {0, 0, 1, -1, 1, -1, 1, -1}; private static final int[] dy4 = {1, 0, -1, 0}, dy = {1, -1, 0, 0, 1, -1, -1, 1}; public static void main(String[] args) { int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(), k = scanner.nextInt(), r = 0; int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } int a = -1; while (k > 0) { int j = 0; for (j = 0; j < n - 1; j++) { if (arr[j] < arr[j + 1]) { arr[j] += 1; a = j; break; } } if (j == n - 1) { a = -1; break; } k -= 1; } /*for (int val : arr) { System.out.print(val + " "); } System.out.println("" + k + " " + (a == -1 ? a : (a + 1)));*/ System.out.println(a == -1 ? a : (a + 1)); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3f207435f903b23074f09a14c07e1240
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] heights = new int[n]; for(int i=0; i<n; i++) { heights[i] = sc.nextInt(); } List<Integer> pos = new ArrayList<>(); for(int i=0; i<k; i++) { int posn = 0; int index = 0; int flag = 0; for(int j=0; j<n-1; j++) { if(heights[j] < heights[j+1]) { pos.add(posn+1); index = j; break; } else posn++; } if((posn + 1) == n) break; heights[index] = heights[index] + 1; } if(pos.size() == k) System.out.println(pos.get(k-1)); else System.out.println(-1); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
66ff92de79d8a342c55f9d70fbea1ba1
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public final class Codechef { public static void main(String[] args){ // Scanner sc=new Scanner(System.in); FastReader sc=new FastReader(); PrintWriter writer=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextLong(); int ans=0,tmp=0; int[] ar=new int[n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } for(int i=0;i<k;i++) { int flag=0; for(int j=0;j<n-1;j++) { tmp=j; if(ar[j]<ar[j+1]) { ar[j]++; flag=1; break; } } if(flag==0) { ans=-1; break; } } if(ans!=-1) { ans=tmp+1; } System.out.println(ans); } writer.flush(); writer.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
0a25ac61b01fb6322c37cc059e770b51
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
//package com.company; import java.math.*; import java.util.*; import java.lang.*; import java.io.*; //import javafx.util; public final class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here FastReader s=new FastReader(); // for taking innput of array of length "len" // int len = s.nextInt(); // int [] arr = new int[len]; // String [] strs = s.nextLine().trim().split("\\s+"); // for (int i = 0; i < len; i++) // arr[i] = Integer.parseInt(strs[i]); int t = s.nextInt(); // int k = 1; while(t-->0) { int len = s.nextInt(); // int k = s.nextInt(); long k = s.nextLong(); int [] arr = new int[len]; //int [] rr = new int[len]; String [] strs = s.nextLine().trim().split("\\s+"); for (int i = 0; i < len; i++) arr[i] = Integer.parseInt(strs[i]); long ans = -1; while (k > 0) { boolean xx = false; for(int i = 0; i<len-1; i++) { if (arr[i] < arr[i + 1]) { ans = i + 1; arr[i]++; break; } if (i == len - 2) { xx = true; } } if(xx) break; k--; } if(k!=0) ans = -1; System.out.println(ans); } // for printing ar array of size "len" // StringBuffer sb = new StringBuffer(); // for (int i = 0; i < len; i++) // sb.append(arr[i] + " "); // // System.out.println(sb); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
a3e57fc6b645628eebac8d809f46419a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.util.Random; import java.util.StringTokenizer; import java.lang.*; public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter pwr = new PrintWriter(System.out,true); int t=fs.nextInt(); for(int i=0;i<t;i++) { int n=fs.nextInt(); long k=fs.nextLong(); int a[]=new int [n],maxi=0,count=0; int b[]=new int [n]; Arrays.fill(b,0); for(int j=0;j<n;j++) { a[j]=fs.nextInt(); } maxi=a[n-1]; int ans=-1; for(int j=n-2;j>=0;j--) { if(a[j]>j+1) { maxi=a[j]; } else { b[j]=maxi-a[j]; if(b[j]<0) { b[j]=0; } count+=b[j]; } } int fl=0; if(false) { ans=-1; } else { while(k-->0) { fl=0; for(int j=1;j<n;j++) { if(a[j]>a[j-1]) { a[j-1]++; ans=j; fl=1; break; } } if(fl==0) { break; } } } if(fl==0)ans=-1; pwr.println(ans); } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
be40ebeca83fab961b6c038c5ab9afae
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; // Arrays.sort(); //char[] a=fs.next().toCharArray(); public class B_New_Colony { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = fs.nextInt(); outer: while (T-- > 0) { int n = fs.nextInt(), k = fs.nextInt(); int arr[] = fs.readArray(n); c: while (k > 0) { for (int i = 0; i < n - 1; i++) { if (arr[i] < arr[i + 1]) { if (i == 0) { k -= (arr[i + 1] - arr[i]); arr[i] = arr[i + 1]; } else if (arr[i] == arr[i - 1]) { k--; arr[i] += 1; } else { k -= (Math.min(arr[i - 1], arr[i + 1]) - arr[i]); arr[i] = Math.min(arr[i - 1], arr[i + 1]); } if (k <= 0) { System.out.println(i + 1); continue outer; } continue c; } } System.out.println(-1); break; } } } static boolean isSorted(int arr[]) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] < arr[i + 1]) { return false; } } return true; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder nextsb() { StringBuilder sb = new StringBuilder(next()); return sb; } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
c09418bb13ad15506592463b966124c9
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); // Input Set size int testCases = scan.nextInt(); StringBuilder ans = new StringBuilder(); while (testCases-- > 0) { int mount = scan.nextInt(); int boulders = scan.nextInt(); int[] mountHeights = new int[mount+1]; mountHeights[0] = Integer.MAX_VALUE; for (int i = 1; i <= mount; i++) { mountHeights[i] = scan.nextInt(); } int stopped = -1; for (int i = 1; i <= mount; i++) { int backs = 1; while (mountHeights[i-backs] < mountHeights[i]) { if (mountHeights[i-backs-1] < mountHeights[i]) { boulders -= (-mountHeights[i-backs] + mountHeights[i-backs-1]) * backs; } else { boulders -= (-mountHeights[i-backs] + mountHeights[i]) * backs; } if (boulders <= 0) { boulders = Math.abs(boulders) % backs; stopped = boulders + i-backs; break; } backs++; } if (stopped != -1) { break; } backs = 1; while (mountHeights[i-backs] < mountHeights[i]) { mountHeights[i-backs] = mountHeights[i]; backs++; } } ans.append(stopped).append('\n'); } System.out.println(ans); } } class Scanner { BufferedReader reader; StringTokenizer tokenizer; Scanner(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
9f674cbbc1b489cbbc459492f3e81b76
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// Author : RegalBeast import java.io.*; import java.util.*; public class Main { static final FastReader FR = new FastReader(); static final PrintWriter PW = new PrintWriter(new OutputStreamWriter(System.out)); static int mountainCount, boulderCount; static int[] mountainHeights; public static void main(String[] args) { StringBuilder solution = new StringBuilder(); int tests = 1; tests = FR.nextInt(); for (int t = 0; t < tests; ++t) { mountainCount = FR.nextInt(); boulderCount = FR.nextInt(); mountainHeights = new int[mountainCount]; for (int m = 0; m < mountainCount; m += 1) { mountainHeights[m] = FR.nextInt(); } int lastBoulderIdx = getLastBoulderIdx(); solution.append(lastBoulderIdx + "\n"); } PW.print(solution.toString()); PW.close(); } static int getLastBoulderIdx() { for (int i = 0; i < mountainCount; i += 1) { if (i == mountainCount-1) { return -1; } else { int diff = mountainHeights[i+1] - mountainHeights[i]; if (diff > 0) { mountainHeights[i] += 1; boulderCount -= 1; if (boulderCount == 0) { return i+1; } else { return getLastBoulderIdx(); } // if (diff >= boulderCount) { // return i+1; // } else { // boulderCount -= diff; // mountainHeights[i] = mountainHeights[i+1]; // return getLastBoulderIdx(); // } } } } return -1; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
efef7d3995052550ca373c4ca1bc407f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { testNumber = in.nextInt(); while (testNumber-- > 0) { int n = in.nextInt(); long k = in.nextLong(); int[] a = in.nextIntArray(n); int pos = -1; while (k-- > 0) { int i = 0; boolean check = true; while (i < n - 1) { if (a[i] < a[i + 1]) { a[i] += 1; pos = i + 1; check = false; break; } i++; } if (check) { pos = -1; break; } } out.println(pos); } } } 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 close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
71bee448dcc19c7023bf5d31f30307d8
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class File { public 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()); } } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int test=sc.nextInt(); while(test>0) { int n=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=sc.nextInt(); int ans=-1; int i=0; while(i!=n-1&&k>0) { if(arr[i]>=arr[i+1]){ i++; continue; } if(i==0||arr[i-1]>=arr[i+1]) { k-=arr[i+1]-arr[i]; arr[i]=arr[i+1]; if(k<=0){ ans=i+1; break; } i++; } else { k-=arr[i-1]+1-arr[i]; arr[i]=arr[i-1]+1; if(k<=0){ ans=i+1; break; } i--; } } if(i==n-1)ans=-1; else ans=i+1; out.println(ans); test--; } out.close(); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
58a28dcfdfe84b5bbb9174075192543d
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class CF699_2 { public static void main(String args[])throws IOException{ InputStreamReader xyz=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(xyz); int t=Integer.parseInt(in.readLine()); for(int ii=1;ii<=t;ii++) { String str=in.readLine(); String[] st = str.split(" "); int n=Integer.parseInt(st[0]); int k=Integer.parseInt(st[1]); String s=in.readLine(); String[] num = s.split(" "); int ar[]=new int[n]; for(int j=0;j<n;j++) { ar[j]=Integer.parseInt(num[j]); } int sum=0; for(int i=0;i<n;i++) { int h=-1; int pos=-1; for(int j=i;j<n;j++) { if(ar[j]>h) { h=ar[j]; pos=j; } } for(int j=i;j<pos;j++) { sum=sum+ar[pos]-ar[j]; } i=pos; } if(k<=sum) { for(int i=1;i<k;i++) { for(int j=0;j<n-1;j++) { if(ar[j]<ar[j+1]) { ar[j]=ar[j]+1; break; } } } int pos=-1; for(int i=0;i<n-1;i++) { if(ar[i]<ar[i+1]) { pos=i+1; break; } } System.out.println(pos); } else { System.out.println("-1"); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6de07448e57f07c10f68490a21926561
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
// package eround101; import java.util.*; import java.io.*; import java.lang.*; import java.util.StringTokenizer; public class C101 { static HritikScanner sc = new HritikScanner(); static PrintWriter pw = new PrintWriter(System.out,true); final static int MOD = (int) (1e9 + 7); static int[] count = new int[1000001]; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) solve(); } static void solve(){ int n = ni(); int k = ni(); long[] arr = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nl(); } List<Integer> list = new ArrayList<>(); if(n == 1) { pl(-1); return; } for(int i = 0; i < k; i++) { int j = 0; while(j < n) { if(arr[j] >= arr[j+1] ) { j++; if(j == n-1) { pl(-1); return; } continue; } if(arr[j] < arr[j+1] && j < n - 1) { arr[j]++; list.add(j+1); break; } } } pl(list.get(list.size()-1)); } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for(Object o : arr) p(o); pl(); } static void pa(int arr[]) { for(int o : arr) p(o); pl(); } static void pa(long arr[]) { for(long o : arr) p(o); pl(); } static void pa(double arr[]) { for(double o : arr) p(o); pl(); } static void pa( char arr[]) { for(char o : arr) p(o); pl(); } static void pa(List list) { for(Object o : list) p(o); pl(); } static void pa(Object[][] arr) { for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(int[][] arr) { for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(long[][] arr) { for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(char[][] arr) { for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(double[][] arr) { for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } ////////////////////////////////////////////////////////////////// public static class Pair implements Comparable<Pair> { int h, w, index; Pair(int h, int w) { this.h = h; this.w = w; } public int compareTo(Pair A) { if (A.h == this.h) { return this.w - A.w; } return this.h - A.h; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } ////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } 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))))); } // static int nCr(int n, int r) //{ // return fact(n) / (fact(r) * // fact(n - r)); //} // //// Returns factorial of n //static int fact(int n) //{ // int res = 1; // for (int i = 2; i <= n; i++) // res = res * i; // return res; //} public static long getFact(int n){ long ans = 1; while (n > 0){ ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow){ if (pow == 0) return 1; long temp = pow(n, pow/2)%MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) temp *= n; temp %= MOD; return temp; } public static long nCr(int n, int r){ long ans = 1; int temp = n-r; while (n > temp){ ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r)%MOD, MOD-2)%MOD; ans %= MOD; return ans; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 11
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
35124d090cc2ad9faffa709886c5e18e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.IOException;import java.io.InputStream;import java.io.PrintStream; import java.io.PrintWriter;import java.security.AccessControlException;import java.util.Arrays; public class _p001481B {static public void main(final String[] args) throws IOException {p001481B._main(args);} static private class p001481B extends Solver{public p001481B(){}@Override public void solve()throws IOException{int n=sc.nextInt();int k=sc.nextInt();sc.nextLine(); int[]h=Arrays.stream(sc.nextLine().trim().split("\\s+")).mapToInt(Integer::parseInt).toArray(); int res=0;while(true){if(res==n-1){break;}if(h[res+1]>h[res]){int d=Math.max(1,res ==0?h[res+1]-h[res]:Math.min(h[res+1]-h[res],h[res-1]-h[res]));if(d>=k){k=0;break; }k-=d;h[res]+=d;if(res>0){res--;}}else{res++;}}if(k>0){res=-2;}pw.println(res+1); }static public void _main(String[]args)throws IOException{new p001481B().run();}} static private class MyScanner{private StringBuilder cache=new StringBuilder();int cache_pos=0;private int first_linebreak=-1;private int second_linebreak=-1;private StringBuilder sb=new StringBuilder();private InputStream is=null;public MyScanner(final InputStream is){this.is=is;}private String charToString(final int c){return String.format("'%s'", c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c)));}public int get(){int res =-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos);cache_pos++;if(cache_pos ==cache.length()){cache.delete(0,cache_pos);cache_pos=0;}}else{try{res=is.read(); }catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void unget(final int c){if(cache_pos==0){cache.insert(0,(char)c);}else{cache_pos--;}} public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true;if(c==first_linebreak) {if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!= -1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak) {cache.append((char)c);break;}if(!done){sb.append((char)c);}}return sb.toString(); }private boolean check_linebreak(int c){if(c=='\n'|| c=='\r'){if(first_linebreak ==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak==-1){second_linebreak =c;}return true;}return false;}public int nextInt(){return Integer.parseInt(next()); }public long nextLong(){return Long.parseLong(next());}public boolean hasNext(){ boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c)&& c!=' '&& c !='\t'){res=true;unget(c);break;}}return res;}public String next(){sb.delete(0,sb.length()); boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c)|| c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c);}}return sb.toString();}public int nextChar(){return get();}public boolean eof(){int c=get(); boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public double nextDouble() {return Double.parseDouble(next());}}static private abstract class Solver{protected String nameIn=null;protected String nameOut=null;protected boolean singleTest=false; protected boolean preprocessDebug=false;protected boolean doNotPreprocess=false; protected PrintStream debugPrintStream=null;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test=0;private int count_tests=0;protected int currentTest(){return current_test;}protected int countTests(){return count_tests; }private void process()throws IOException{if(!singleTest){count_tests=sc.nextInt(); sc.nextLine();for(current_test=1;current_test<=count_tests;current_test++){solve(); pw.flush();}}else{count_tests=1;current_test=1;solve();pw.flush();}}abstract protected void solve()throws IOException;protected void run()throws IOException{boolean done =false;try{if(nameIn!=null && new File(nameIn).exists()){try(FileInputStream fis =new FileInputStream(nameIn);PrintWriter pw0=select_output();){done=true;sc=new MyScanner(fis);pw=pw0;process();}}}catch(IOException ex){}catch(AccessControlException ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in); pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException {if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out); }}}
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
2a78b316798a022204bcdf75ad6061c0
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class MainB { static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { String string = reader.readLine(); if (string == null) { return false; } tokenizer = new StringTokenizer(string); return tokenizer.hasMoreTokens(); } catch (IOException e) { return false; } } } static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) { int t = in.nextInt(); while (t -- > 0) { solve(); } out.close(); } static void solve() { int n = in.nextInt(); int k = in.nextInt(); int[] height = new int[n]; boolean isDec = true; for (int i = 0; i < n; i++) { height[i] = in.nextInt(); if (isDec && i > 0 && height[i] > height[i - 1]) { isDec = false; } } if (isDec) { out.println(-1); } else if (n == 2) { if (height[1] - height[0] >= k) { out.println(1); } else { out.println(-1); } } else { int answer = 0; boolean isStop; while (k -- > 0) { answer = -1; isStop = false; for (int i = 0; i < n - 1; i++) { if (height[i] < height[i + 1]) { isStop = true; answer = i + 1; height[i] ++; break; } } if (!isStop) { break; } } out.println(answer); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
692519c74422163e7e1d9f9a256b8f58
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int[] h = new int[n]; int max = Integer.MIN_VALUE; for(int i = 0; i<n; i++) { h[i] = sc.nextInt(); max = Math.max(h[i], max); } if(n*max<k) { System.out.println("-1"); continue; } int to = n; int ans = 0; for(int i = 0; i<k; i++){ to = -2; for(int j = 0 ; j<n-1; j++){ if(h[j]<h[j+1]){ to = j; break; } } ans = to + 1; if(ans==-1) break; h[to]++; } System.out.println(ans); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
dde94fc97e5de223564044975e00d06e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws Exception { int Q = sc.nextInt(); for(int i = 0; i < Q; i++) pw.println(solve()); pw.flush(); } static int solve(){ int N = sc.nextInt(); int K = sc.nextInt(); long[] H = sc.nextLongArray(N); if(K >= 10000) return -1; for(int i = 0; i < K; i++){ for(int j = 0; j < N-1; j++){ if(H[j] < H[j+1]){ if(i == K-1) return j+1; else H[j]++; break; } } } return -1; } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d16f994a206b3bbe28854f1aac99f79f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { int a = nextInt(); for (int i = 0; i < a; i++) { int n = nextInt(); int k = nextInt(); long[] b = new long[n]; for (int j = 0; j < n; j++) { b[j] = nextLong(); } boolean e=false; for (int j = 0; j < k-1; j++) { for (int l = 0; l < n - 1; l++) { if (b[l] < b[l + 1]) { b[l]++; break; } else if (l==n-2&&b[l]>=b[l+1]){ e=true;break; } } if (e)break; } long ans=-1; for (int j = 0; j < n-1; j++) { if (b[j]<b[j+1]){ ans=j+1; break; } } out.println(ans); } 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
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
0d250a4b813ee91349672665b978d2be
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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 Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int h[]=new int[n]; for(int i =0;i<n;i++) h[i]=sc.nextInt(); int d = k; int x = 1; for(int i =0; i<k;i++){ boolean f = true; int st=-1; for(int j=x;j<n;j++){ if(h[j-1]>=h[j]) continue; else{ f = false; h[j-1]++; st=j; break; } } d--; if(f){ System.out.println(-1); break; } else if(d==0 && !f){ System.out.println(st); } else if(!f){ x=st-1; if(x==0) x++; } else x=n-1; } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
e53c44b8cb69665c9a6e62cbeef9f0cf
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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 Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int h[]=new int[n]; for(int i =0;i<n;i++) h[i]=sc.nextInt(); int d = k; for(int i =0; i<k;i++){ boolean f = true; int st=-1; for(int j=1;j<n;j++){ if(h[j-1]>=h[j]) continue; else{ f = false; h[j-1]++; st=j; break; } } d--; if(f){ System.out.println(-1); break; } else if(d==0 && !f){ System.out.println(st); } } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
896d345d8a0e9a47e71babf0d47b923f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
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 Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int h[]=new int[n]; for(int i =0;i<n;i++) h[i]=sc.nextInt(); if(k>100000 || n==1) System.out.println(-1); else{ int d = k; int x = 1; for(int i =0; i<k;i++){ boolean f = true; int st=-1; for(int j=x;j<n;j++){ if(h[j-1]>=h[j]) continue; else{ f = false; h[j-1]++; st=j; break; } } d--; if(d==0 && f){ System.out.println(-1); } else if(d==0 && !f){ System.out.println(st); } else if(!f){ x=st-1; if(x==0) x++; } else x=n-1; } } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
f85401c927040024cd797b540a138c66
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] nums = new int[105], stk = new int[105]; for(int T = sc.nextInt(); T > 0; --T) { int n = sc.nextInt(), k = sc.nextInt(); for(int i = 1; i <= n; ++i) { nums[i] = sc.nextInt(); } int res = 0, i; boolean tag = false; while(k > 0) { --k; for(i = 1; i <= n; ++i) { if(i==n) { tag = true; break; } if(nums[i] >= nums[i+1]) continue; res = i; ++nums[i]; break; } if(tag) break; } System.out.println(tag||k>0? -1: res); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
396359a5b929329256791a5438f5d62e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class TT { static StringBuilder out; static void solve() throws IOException{ int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = fs.nextInt(); if(n == 1){ pw.println(-1); return; } int d=1; while(k>0){ for(d=1;d<n;d++){ if(arr[d] > arr[d-1]){ k--; arr[d-1] += 1; break; } } if(d == n){ pw.println(-1); return; } } pw.println(d); } public static void main (String[] args) throws java.lang.Exception { fs = new FastReader(); out = new StringBuilder(); int t = fs.nextInt(); while (t-- > 0) { solve(); } pw.flush(); pw.close(); } //------------------------------------------------- static long[] arrayInput(int n) throws IOException{ long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i] = fs.nextLong(); return arr; } static final int MOD = (int) 1e9+7; static final int INT_MAX = Integer.MAX_VALUE; static final int INT_MIN = Integer.MIN_VALUE; static FastReader fs; static PrintWriter pw = new PrintWriter(System.out); static void swap(long[]arr, int a, int b){ long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } static void ruffleSort(long arr[]){ for(int i = 0; i<arr.length; i++){ int t = (int) Math.random() * arr.length; long x = arr[t]; arr[t] = arr[i]; arr[i] = x; } Arrays.sort(arr); } static boolean isPrimeProbable(BigInteger bi){ return bi.isProbablePrime(10); } static BigInteger nextProbablePrime(BigInteger bi){ return bi.nextProbablePrime(); } //--------reader section----------------- static class FastReader{ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; BufferedReader br; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); 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 nextInt()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 long nextLong()throws IOException{ long ll=0l; 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'){ ll*=10; ll+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*ll; } public double nextDouble()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; } String nextLine(){ String str = ""; try{str = br.readLine();} catch (IOException e){e.printStackTrace();} return str; } public String nextString()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; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
eb7cdce93aadcfb66356e0285774b33b
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class TT { static StringBuilder out; static void solve() throws IOException{ int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = fs.nextInt(); if(n == 1){ pw.println(-1); return; } int d=1; while(k>0){ for(d=1;d<n;d++){ if(arr[d] > arr[d-1]){ k--; arr[d-1] += 1; break; } } if(d == n){ pw.println(-1); return; } } pw.println(d); } public static void main (String[] args) throws java.lang.Exception { fs = new FastReader(); out = new StringBuilder(); int t = fs.nextInt(); while (t-- > 0) { solve(); } pw.flush(); pw.close(); } //------------------------------------------------- static long[] arrayInput(int n) throws IOException{ long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i] = fs.nextLong(); return arr; } static final int MOD = (int) 1e9+7; static final int INT_MAX = Integer.MAX_VALUE; static final int INT_MIN = Integer.MIN_VALUE; static FastReader fs; static PrintWriter pw = new PrintWriter(System.out); static void swap(long[]arr, int a, int b){ long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } static void ruffleSort(long arr[]){ for(int i = 0; i<arr.length; i++){ int t = (int) Math.random() * arr.length; long x = arr[t]; arr[t] = arr[i]; arr[i] = x; } Arrays.sort(arr); } static boolean isPrimeProbable(BigInteger bi){ return bi.isProbablePrime(10); } static BigInteger nextProbablePrime(BigInteger bi){ return bi.nextProbablePrime(); } //--------reader section----------------- static class FastReader{ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; BufferedReader br; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); 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 nextInt()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 long nextLong()throws IOException{ long ll=0l; 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'){ ll*=10; ll+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*ll; } public double nextDouble()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; } String nextLine(){ String str = ""; try{str = br.readLine();} catch (IOException e){e.printStackTrace();} return str; } public String nextString()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; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
7a500701c43c729f7e7aea745b3b9437
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class boulders{ public static int calculate(int n, int k, int[] heights){ if(n==1)return -1; int count=0; boolean good=true; int position=0; while(good){ int current=0; while(current<n-1&&heights[current]>=heights[current+1]){ current++; } if(current==n-1){ break; } heights[current]++; count++; if(count==k){ position=current; break; } } //while() if(k>count)return -1; return position+1; } public static void main(String[] args){ Scanner scanner= new Scanner(System.in); int t=scanner.nextInt(); for(int i=0;i<t;i++){ int n=scanner.nextInt(); int k=scanner.nextInt(); int[] heights= new int[n]; for(int j=0;j<n;j++){ heights[j]=scanner.nextInt(); } System.out.println(calculate(n,k,heights)); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
2e41f90786ad32c0b81bc33981835601
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedInputStream; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(), k = in.nextInt(); int[] mou = new int[n]; for (int i = 0; i < n; ++i) { mou[i] = in.nextInt(); } int thr = 1; for (int i = 0; i < k; ++i) { int j = 0; while (j < n -1 && mou[j] >= mou[j + 1]) { ++j; } if (j == n - 1) { thr = -1; break; } else { ++mou[j]; thr = j + 1; } } System.out.println(thr); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
267be2c6c94d9c67cffb804e5fb20ac5
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader in = new Reader(); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(), k = in.nextInt(); int[] mou = new int[n]; for (int i = 0; i < n; ++i) { mou[i] = in.nextInt(); } if (k > 1e5){ System.out.println(-1); } else { int thr = 1; for (int i = 0; i < k; ++i) { int j = 0; while (j < n - 1 && mou[j] >= mou[j + 1]) { ++j; } if (j == n - 1) { thr = -1; break; } else { ++mou[j]; thr = j + 1; } } System.out.println(thr); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
8fb14e058b4246074fe92dea996ef85c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader in = new Reader(); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(), k = in.nextInt(); int[] mou = new int[n]; for (int i = 0; i < n; ++i) { mou[i] = in.nextInt(); } if (k > 1e5){ System.out.println(-1); } else { int thr = 1; for (int i = 0; i < k; ++i) { int j = 0; while (j < n - 1 && mou[j] >= mou[j + 1]) { ++j; } if (j == n - 1) { thr = -1; } else { ++mou[j]; thr = j + 1; } } System.out.println(thr); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
d1bd4f9a49f92fdfc797da326863a6f0
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedInputStream; import java.util.*; public class Mainn { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(), k = in.nextInt(); int[] mou = new int[n]; for (int i = 0; i < n; ++i) { mou[i] = in.nextInt(); } if (k > 1e5){ System.out.println(-1); } else { int thr = 1; for (int i = 0; i < k; ++i) { int j = 0; while (j < n - 1 && mou[j] >= mou[j + 1]) { ++j; } if (j == n - 1) { thr = -1; } else { ++mou[j]; thr = j + 1; } } System.out.println(thr); } } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
6d2cbb7d253b4dfe238dbefdbd4d4d0e
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.time.LocalTime; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; /* 1 5 1000000000 1 1 1 1 100 */ public class B { public static void main(String[] args) { FastScanner scan = new FastScanner(); int t = scan.nextInt(); scan: for(int tt=0; tt<t; tt++) { int n = scan.nextInt(), k = scan.nextInt(); int [] arr = scan.readArray(n); for(int i=1; i<k; i++) { int pos = -1; for(int j=0; j+1<n; j++) { if(arr[j] < arr[j+1]) { pos = j; break; } } if(pos == -1) break; arr[pos]++; } int pos = -1; for(int j=0; j+1<n; j++) { if(arr[j] < arr[j+1]) { pos = j+1; break; } } System.out.println(pos); } } public static void sort(int [] a) { ArrayList<Integer> b = new ArrayList<>(); for(int i: a) b.add(i); Collections.sort(b); for(int i=0; i<a.length; i++) a[i]= b.get(i); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int [] readArray(int n) { int [] a = new int[n]; for(int i=0; i<n ; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
dc64b65f2be1587118be7938a94c3252
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int iter = scan.nextInt(); for (int i = 0; i < iter; i++) { int numMount = scan.nextInt(); int boulders = scan.nextInt(); int[] arr = new int[numMount]; for (int j = 0; j < numMount; j++) { arr[j] = scan.nextInt(); } /* if(boulders > 10000) pw.println(-1);*/ // else{ label1: for (int j = 1; j < numMount; j++) { if(arr[j] > arr[j-1]){ // System.out.println(boulders); boulders --; arr[j-1] ++; if(boulders <= 0){ pw.println(j); break label1; } j = 0; } } if(boulders > 0) pw.println(-1); } // } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3f667c9e150e5f8949b3e9e0af3d756d
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int iter = scan.nextInt(); for (int i = 0; i < iter; i++) { int numMount = scan.nextInt(); int boulders = scan.nextInt(); int[] arr = new int[numMount]; for (int j = 0; j < numMount; j++) { arr[j] = scan.nextInt(); } if(boulders > 10000) pw.println(-1); else{ label1: for (int j = 1; j < numMount; j++) { if(arr[j] > arr[j-1]){ // System.out.println(boulders); boulders --; arr[j-1] ++; if(boulders <= 0){ pw.println(j); break label1; } j = 0; } } if(boulders > 0) pw.println(-1); } } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
20e73a125cc2acbb8cf69fd46a5c6779
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; public class Main { private static final int MOD = (int) (1E9 + 7); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here int t = 1; t = parseInt(scanner.nextLine()); while (t-- > 0) { solve(); } } private static void solve() throws IOException { int n = scanner.nextInt(); int k = scanner.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = scanner.nextInt(); } int i = 0; while (i < n - 1) { if (h[i] >= h[i + 1]) { i++; continue; } int d = h[i + 1] - h[i]; if (k == 1) { System.out.println(i + 1); return; } h[i]++; k--; i = Math.max(i - 1, 0); } System.out.println("-1"); } private static int add(int a, int b) { long res = ((long) (a + MOD) % MOD + (b + MOD)) % MOD; return (int) res; } private static int mul(int a, int b) { long res = ((long) a % MOD * b % MOD) % MOD; return (int) res; } private static int pow(int a, int b) { a %= MOD; int res = 1; while (b > 0) { if ((b & 1) != 0) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return parseDouble(next()); } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
88a9b1ee6c9a8198478838a816a9a61f
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; public class B699 { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); //t=1; while(t>0) { int n=s.nextInt(); int k=s.nextInt(); int ans=0; boolean bool=true; int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } for(int i=0;i<100*100;i++) { if(bool) { for(int j=0;j<n-1;j++) { if(a[j]<a[j+1]) { k--; a[j]++; if(k<1) { ans=j+1; bool=false; } break; } } } else break; } // for(int i=0;i<n;i++) // System.out.print(a[i]+" "); //System.out.println(); if(ans!=0) System.out.println(ans); else System.out.println("-1"); t--; } } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
1aec53f55b89050f58047ba4877f569c
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static int p = -1, k = 0; public static void main(String arg[]){ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t > 0) { int i = 0, n = scan.nextInt(); k = scan.nextInt(); p = -1; int [] arr = new int[n]; for (i = 0; i < n; ++i) { arr[i] = scan.nextInt(); } while (find(arr, 0) && k > 0){ } System.out.println(p); t--; } } public static boolean find(int [] arr, int i) { if (i < arr.length - 1) { if (arr[i] < arr[i + 1]) { if (k > 1) { k--; arr[i]++; return true; } else { k = 0; if (p == -1) { p = i + 1; } return true; } } else { boolean a = find(arr, i + 1); if (arr[i] < arr[i + 1]) { if (k > 1) { k--; arr[i]++; return true; } else { k = 0; if (p == -1) { p = i + 1; } return true; } } return a; } } return false; } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
b186294772a551a4823867018fe1ba9a
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(), k = in.nextInt(); int a[] = in.readArray(n); int pos = -1; outer : for(int i=0;i<n-1;i++){ if(a[i+1]>a[i]){ boolean allInc = false; while(!allInc){ for(int j=i;j>=0;j--){ if(a[j]<a[j+1]){ k--; a[j]++; if(k==0){ pos = j+1; break outer; } } } allInc = true; for(int j=0;j<=i;j++) if(a[j]<a[j+1]) allInc = false; } } } //for(int i : a) out.print(i+ " "); out.println(); out.println(pos); } out.flush(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output
PASSED
3ab45ed2821f560843874e3097d2112d
train_107.jsonl
1612535700
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i &lt; h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Div2 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] s = reader.readLine().split(" "); int ops = Integer.parseInt(s[0]); StringBuilder sb = new StringBuilder(); Div2 div2 = new Div2(); while (ops-- > 0) { s = reader.readLine().split(" "); int n = Integer.parseInt(s[0]); int k = Integer.parseInt(s[1]); s = reader.readLine().split(" "); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(s[i]); } int lastPos = -1; while (k > 0) { boolean isChanged = false; for (int i = 0; i < n - 1; i++) { if (nums[i] < nums[i + 1]) { k--; nums[i]++; lastPos = i; isChanged = true; break; } } if (!isChanged || k == 0) { break; } } if (k > 0) { sb.append("-1").append("\n"); } else { sb.append(lastPos + 1).append("\n"); } } System.out.print(sb.toString()); } }
Java
["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"]
2 seconds
["2\n1\n-1\n-1"]
NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 &lt; h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 &lt; h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system.
Java 8
standard input
[ "brute force", "greedy", "implementation" ]
32855bb8ba33973178fde7c3d0beb2ce
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
1,100
For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.
standard output