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
ebec56c99094d906abd154a24efc701d
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.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class pcarp{ static int max_value; 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[] arr = fs.readArray(n); int ans=0; for(int j=0;j<k;j++){ int flag=0; for(int i=0;i<n-1;i++){ if(arr[i]<arr[i+1]){ arr[i]+=1; flag=1; ans=(i+1); break; } } if(flag==0){ ans=-1; break; } } System.out.println(ans); } } public static String function(String s){ if(check(s)){ return s; } if(s.length()==2 && s.charAt(0)==s.charAt(1)){ return ""; } if(s.charAt(0)==s.charAt(1)){ return function(s.substring(2,s.length())); } else{ return function(s.charAt(0)+function(s.substring(1,s.length()))); } } static boolean isPerfectSquare(double x) { if (x >= 0) { double sr = Math.sqrt(x); return ((sr * sr) == x); } return false; } public static boolean isPerfect(int n){ int a = (int)Math.sqrt(n); if(a*a==n){ return true; } return false; } public static boolean check(String s){ if(s.length()==1){ return true; } for(int i=1;i<s.length();i++){ if(s.charAt(i)==s.charAt(i-1)){ return false; } } return true; } public static boolean isPrime(int n){ boolean flag=true; while(n%2==0){ n=n/2; flag=false; } for(int i=3;i<=Math.sqrt(n);i+=2){ if(n%i==0){ flag=false; while(n%i==0){ n=n/i; } } } return flag; } public static void dfst(ArrayList<ArrayList<Integer>> graph,int src, int deg,boolean ef, boolean of, boolean[] vis, boolean[] flip, int[] init, int[] goal){ if(vis[src]){ return; } vis[src]=true; if((deg%2==0 && ef) || (deg%2==1 && of)){ init[src]=1-init[src]; } if(init[src]!=goal[src]){ flip[src]=true; if(deg%2==0){ ef=!ef; } else{ of=!of; } } for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ dfst(graph,graph.get(src).get(i),deg+1,ef,of,vis,flip,init,goal); } } } public static void dfs(ArrayList<ArrayList<Integer>> graph, int src, int val, boolean[] vis){ vis[src]=true; int cur_val =0; for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ cur_val=val+1; dfs(graph,graph.get(src).get(i),cur_val,vis); } if(max_value<cur_val){ max_value=cur_val; } cur_val=0; } } public static ArrayList<Integer> pf(int n){ ArrayList<Integer> arr = new ArrayList<>(); boolean flag=false; while(n%2==0){ flag=true; n/=2; } if(flag){ arr.add(2); } for(int i=3;i<=Math.sqrt(n);i++){ if(n%i==0){ arr.add(i); while(n%i==0){ n/=i; } } } if(n>1){ arr.add(n); } return arr; } static int gcd(int a, int b){ if(b==0){ return a; } else{ return gcd(b,a%b); } } static boolean function(int n, int i, int[] arr, int sum){ if(sum==0){ return true; } if(i==n && sum!=0){ return false; } if(sum<arr[i]){ return function(n,i+1,arr,sum); } else{ return function(n,i+1,arr,sum-arr[i]) || function(n,i+1,arr,sum); } } public static long fact( long n, long mod){ long res =1; for(int i=1;i<=n;i++){ res%=mod; i%=mod; res=(res*i)%mod; } return res; } public static long nCk(long n,long k, long mod){ return (fact(n,mod)%mod*modular(fact(k,mod),mod-2,mod)%mod*modular(fact(n-k,mod),mod-2,mod)%mod)%mod; } public static long modular(long n, long e, long mod){ long res = 1; n%=mod; if (n == 0) return 0; while(e>0){ if((e&1)==1){ res=(res*n)%mod; } e=e>>1; n=(n*n)%mod; } return res; } 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
77e4f7692be13ab92dc26dd620c4fb51
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 in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); int arr[] = new int[n]; int max = 0; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); max = Math.max(arr[i], max); } if(max * n < k) { System.out.println(-1); continue; } int lastState = 0; boolean flag = false; for (int i = 0; i < k; i++) { boolean added = false; for (int j = 0; j < n - 1; j++) { if (arr[j] < arr[j + 1]) { added = true; arr[j]++; lastState = j; break; } } if (!added) flag = true; } if (flag) System.out.println(-1); else System.out.println(lastState + 1 == n ? -1 : lastState + 1); } in.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 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
bc53adf35f11fc7f23826e3d09599e85
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 Codechef{ public static void main(String []args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int tc=Integer.parseInt(br.readLine()); while(tc-->0){ String s1[] = br.readLine().trim().split(" "); int n = Integer.parseInt(s1[0]); long k = Long.parseLong(s1[1]); String s[] = br.readLine().trim().split(" "); int a[] = new int[n+1]; for(int i=1;i<=n;i++){ a[i] = Integer.parseInt(s[i-1]); } int ind = -1; for(int i=0;i<k;i++) { ind = -1; for(int j=1;j<n;j++){ if(a[j]<a[j+1]){ a[j]++; ind = j; break; } } if(ind==-1) break; } System.out.println(ind); } } }
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
5e4b8773a4cce0a3c15c75a8ba29718e
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.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay2516 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(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, FastReader in, PrintWriter out) { int n = in.nextInt(); long k = in.nextInt(); int[] h = in.readArray(n); while (k > 0) { int j = 0; boolean f = true; for (; j < n - 1; ++j) { if (h[j + 1] > h[j]) { h[j]++; k--; f = false; if (k == 0) { out.println(j + 1); return; } break; } } if (f) { out.println(-1); return; } } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; 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 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
434d8ddb729efee2151977a9ea55cf1f
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 New_Colony_B { public static void main(String[] args) { // TODO Auto-generated method stub 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 c = 0; if(n==1) { System.out.println(-1); } else { boolean check = false; int ans = -1; for(int i = 0; i<n-1; i++) { int a = arr[i]; int b = arr[i+1]; if(a>=b) { continue; } else { c++; arr[i]++; if(c==k){ ans = i+1; check = true; break; } else { i = -1; } } } if(check==true) { System.out.println(ans); } else { System.out.println(-1); } } // for(int i = 0; i<n; i++) { // System.out.print(arr[i]+" "); // } // System.out.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 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
63a0867d36a2c3e0a61c310d9c86a77b
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; /** * * @author Acer */ public class NewColony { 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(); } if(n == 1){ System.out.println(-1); continue; } boolean flag = false; int pos = -1; while(k-- > 0){ flag = true; for (int i = 0; i < n-1; i++) { if(arr[i] < arr[i+1]){ flag = false; arr[i]++; pos = i+1; break; } } if(flag){ break; } } //System.out.println("k:"+k); if(flag){ System.out.println(-1); } else{ 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 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
02b9fbcfe1fd27077b281a0381491fb0
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
//NOT done import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class P1481B { static FastReaderP1481A in=new FastReaderP1481A(); public static void main(String...x){ int test=i(); while(test-->0){ int n=i(); int k=i(); int ar[]=inputIntgerArray(n); int i,ans=0; for(i=0;i<n-1 && k>0;i++){ if(ar[i]<ar[i+1]){ k-=1; ar[i]+=1; ans=i; if(i > 0 && ar[i]<ar[i-1]){ int mn=Math.min(ar[i-1]-ar[i],ar[i+1]-ar[i]); k-=mn; ar[i]+=mn; } i=-1; } } if(k<=0) System.out.println(ans+1); else System.out.println(-1); } } static int i(){return in.nextInt();} static long l(){return in.nextLong();} static int[] inputIntgerArray(int N){ int A[]=new int[N]; for(int i=0; i<N; i++)A[i]=in.nextInt(); return A; } static long[] inputLongArray(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } class FastReaderP1481A{ BufferedReader br; StringTokenizer st; public FastReaderP1481A() { 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 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
fe7504ce91485aa0d8da738aaf99c387
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.*; public class NewColony { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int runs = sc.nextInt(); while(runs-->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(); } if(n==1) System.out.println(-1); else { int index = 0; outer:for(int i = 0;i<k;i++) { runner:for(int j = 0;j<n-1;j++) { if(arr[j]<arr[j+1]) { arr[j]++; index = j+1; break runner; } if(j+1 == n-1) { index = -1; break outer; } } } 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 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
0923c4a96bbe888833948f6774b3e94b
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
//NEVER GIVE UP ON YOUR DREAMS import java.io.*; import java.util.*; public class New_Colony { private static final int MOD = (int) 1e9 + 7; private static final int INF = (int) 1e8; private static int MAXN = (int) 1e5 + 6; private static FastReader fr = new FastReader(); public static void main(String[] args) { StringBuilder sb = new StringBuilder(); //Code here int tt=fr.ni(); while(tt-->0){ int n=fr.ni(); int k=fr.ni(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=fr.ni(); } int max=a[n-1]; int Ksum=0; for(int i=n-2;i>=0;i--){ if(a[i]<max){ Ksum+=(max-a[i]); } else{ max = Math.max(max,a[i]); } } if(k>Ksum){ sb.append("-1\n"); } else{ int ans=-1; for(int i=1;i<k;i++){ for(int j=0;j<n-1;j++){ if(a[j]>=a[j+1]){ continue; }else{ a[j]+=1; break; } } } for(int i=0;i<n-1;i++){ if(a[i]>=a[i+1]){ continue; } else{ ans = i+1; break; } } sb.append(ans+"\n"); } } System.out.println(sb); } //FAST IO here static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String ns() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException ie) { ie.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(ns()); } long nl() { return Long.parseLong(ns()); } double nd() { return Double.parseDouble(ns()); } String nsl() { 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 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
b8b2ef8e7bdb88ce6d4c710b6afeb1db
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
/* "Why do we fall? So we can learn to pick ourselves up." */ import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); //static long dp[][]; //static int mod=1000000007; public static void main(String[] args) { PrintWriter out=new PrintWriter(System.out); //StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int k=i(); int A[]=input(n); long res=0; while(true) { k--; int f=-1; for(int i=0;i<n-1;) { if(A[i]>=A[i+1]) { i++; continue; } f=1; A[i]++; if(k==0) { System.out.println(i+1); continue outer; } break; } if(f==-1) { System.out.println("-1"); break; } } } //out.close(); //System.out.println(sb.toString()); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(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]=i(); } } return A; } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long mod(long x) { int mod=1000000007; return ((x%mod + mod)%mod); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Pair //implements Comparable<Pair> { int x; int y; Pair(int x,int y){ this.x=x; this.y=y; } } 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 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
50189b20bc5cf8c611a90949207b7513
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) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int t1 = 0; t1 < t; t1++) { int n = in.nextInt(); long k = in.nextLong(); long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = in.nextLong(); pw.println(solve(arr, n, k)); } pw.close(); } static long solve(long[] arr, int n, long k) { int ans = 0; for (int i = 0; i < k; i++) { int res = -2; for (int j = 0; j < n - 1; j++) { if (arr[j] < arr[j + 1]) { res = j; break; } } ans = res + 1; if (res == -2) break; arr[res]++; } return ans; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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
10de72c03b511ef7b26611d6eccd5405
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.util.Scanner; public class Codeforces { private static int sport(int[] h, int k) { while (k>0) { boolean flag=false; for (int i = 0; i < h.length - 1; i++) { if (h[i] < h[i + 1]) { flag=true; k--; if (k < 1) { return i + 1; } h[i]++; break; } } if (!flag){ return -1; } } return -1; } 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(); } System.out.println(sport(h, k)); } } }
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
d0c6a889689e8481b7af01f165857f29
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.*; public class Main { static int i,j,k,n,m,t,y,x,sum; static long mod = 998244353; static FastScanner fs = new FastScanner(); static int[] arr = new int[105]; public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); t = fs.nextInt(); while(t-- >0){ n = fs.nextInt(); long k = fs.nextInt(); long sum =0; int[] arr = new int[n]; for(i=0;i<n;i++){ arr[i]=fs.nextInt(); sum+=arr[i]; } long ans = 0; long min = arr[n-1]; for(i=n-1;i>=0;i--){ if(arr[i]<min){ ans+=(min-arr[i]); } else{ min=arr[i]; } } if(k>ans){ out.println(-1); } else{ ans=-1; i=0; while(i<k){ for(j=0;j<n-1;j++){ if(arr[j+1]>arr[j]){ arr[j]++; i++; if(i==k) { ans = j + 1; } break; } } if(i==k) break; } out.println(ans); } } 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()); } long nextLong() { return Long.parseLong(next()); } } static long exp(long base, long pow) { if (pow==0) return 1; long half=exp(base, pow/2); if (pow%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return a*b%mod; } static long add(long a, long b) { return (a+b)%mod; } static long modInv(long x) { return exp(x, mod-2); } }
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
74b2eb4fb00ff3c74953f88d098fd7ae
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 HelloWorld{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); while(T-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] h = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) h[i] = Integer.parseInt(st.nextToken()); int ans = 0; while(k-- > 0) { int temp = -1; for(int i = 0; i < n-1; i++) { if(h[i] >= h[i+1]) continue; else { h[i]++; temp = i+1; break; } } ans = temp; if(ans == -1) break; } writer.println(ans); } writer.close(); br.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 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
0fabe464c0acc42f0d8371d2d00e76e8
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 static java.lang.Math.max; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); while (t-->0) { int n = input.nextInt(); int k = input.nextInt(); int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = input.nextInt(); } int result = -1; while (k-->0) { int i = 1; while (i < n && a[i] >= a[i + 1]) { i++; } if (i == n) { break; } if (a[i] < a[i+1]) { a[i]++; } if (k == 0) { result = i; } } System.out.println(result); } } }
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
71c435f417a777aba84ffd745df9c4ba
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.lang.*; import java.util.*; import java.io.*; public class Codeforces { public void solve()throws IOException { FastScanner fs = new FastScanner(); StringBuilder print = new StringBuilder(); int t = fs.nextInt(); while (t-- > 0 ) { int n = fs.nextInt(), k = fs.nextInt(); int[]ar = new int[n]; for(int i=0;i<n;i++)ar[i] = fs.nextInt(); int ind = -1; for(int i=0;i<n-1;i++){ if(ar[i+1] > ar[i])ind = i; } if(ind == -1){ print.append("-1\n"); continue; } boolean ok = false; while (k > 0 && !ok){ k--; int last = -1; for(int i=0;i<n-1;i++){ if(ar[i+1] > ar[i]){ ar[i]++; last = i; if(k == 0){ ok = true; print.append(i+1).append("\n"); } break; } } if(!ok && last == -1){ ok= true; print.append("-1\n"); } } if(!ok)print.append("-1\n"); } System.out.println(print); } public static void main(String[]args)throws IOException{ try { new Codeforces().solve(); }catch (Exception e){ // return; e.printStackTrace(); } } 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(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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
991df90fe47de5ed69beb22a22fb0b60
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 CF102 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 1; i <= t; i++) { int n = sc.nextInt(); int k = sc.nextInt(); int[] ar =new int[n]; for(int j=0;j<n;j++){ ar[j]=sc.nextInt(); } int b1=0; process: for(int j=1;j<=k;j++){ for(int p=0;p<n;p++){ if(p<(n-1)&&ar[p]<ar[p+1] ){ //b1=p+1; //ar[p+1]+=1; b1=p; ar[p]+=1; continue process; } b1=p; if(b1==n-1){ break process; } } } if((b1+1)==n){ System.out.println(-1); }else { System.out.println((b1+1)); } } } public static long sweetness(long a) { long c = 0; for (long i = 1; i <= a; i++) { if (gcd(i, a) == 1) { c++; } } return c; } public static long power(long a, long b, long c) { long ans = 1; while (b != 0) { if (b % 2 == 1) { ans = ans * a; ans %= c; } a = a * a; a %= c; b /= 2; } return ans; } public static long power1(long a, long b, long c) { long ans = 1; while (b != 0) { if (b % 2 == 1) { ans = multiply(ans, a, c); } a = multiply(a, a, c); b /= 2; } return ans; } public static long multiply(long a, long b, long c) { long res = 0; a %= c; while (b > 0) { if (b % 2 == 1) { res = (res + a) % c; } a = (a + a) % c; b /= 2; } return res % c; } public static long totient(long n) { long result = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { //sum=sum+2*i; while (n % i == 0) { n /= i; // sum=sum+n; } result -= result / i; } } if (n > 1) { result -= result / n; } return result; } public static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } public static boolean[] primes(int n) { boolean[] p = new boolean[n + 1]; p[0] = false; p[1] = false; for (int i = 2; i <= n; i++) { p[i] = true; } for (int i = 2; i * i <= n; i++) { if (p[i]) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } }
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
aac578b19b11d0488b126deb90752c3c
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.*; /* author @ Aditya Aggarwal LinkedIn : https://www.linkedin.com/in/aditya7861/ ' The Dawn does not come twice to awaken the man ' Never Give Up ... Failure is the biggest success */ public class Main { static void run(){ boolean tc = true; //AdityaFastIO r = new AdityaFastIO(); FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { int testcases = tc ? r.ni() : 1; // Hold Here Sparky------------------->>> // Solution Starts Here while (testcases --> 0){ int n = r.ni(); int k = r.ni(); int[] h = new int[n+1]; for (int i=0;i<n;i++) h[i] = r.ni(); int ans = -1; for(int i=0; i<k; i++){ int j = 0; while(h[j] >= h[j+1] && j<n-1){ j++; } if(j == n-1){ ans = -1; break; } h[j] += 1; ans = j + 1; } out.write((ans + " ").getBytes()); out.write(("\n").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO{ final private int BUFFER_SIZE = 1<<16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nl() throws IOException { long ret = 0;byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret;return ret; } public double nd() throws IOException { double ret = 0, div = 1;byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret;return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws java.lang.Exception {run();} static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e>0) { if ((e & 1)>0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2;x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b;else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) < k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static int upper_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) <= k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2);graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first;int second; public Pair(int first, int second) { this.first = first;this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.first!=o.first) return (int) (this.first - o.first); else return(int)(this.second-o.second); } } static boolean isCollectionsSorted(List<Integer> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) < list.get(i - 1)) return false; return true; } }
Java
["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
630b915807a3a41844b5ec3d14d8dc35
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, InterruptedException { int tc = sc.nextInt(); while(tc --> 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] h = sc.nextIntArr(n); int i = 0; while(k > 0 && i < n-1){ if(h[i] >= h[i+1]) i++; else { if(i > 0){ if(h[i-1]-h[i]+1 <= h[i+1]-h[i]) { k -= (h[i - 1] - h[i] + 1); if (k <= 0) break; h[i] = h[i - 1] + 1; i--; } else{ k -= (h[i+1] - h[i]); if(k <= 0) break; h[i] = h[i+1]; i--; } } else{ k -= (h[i+1] - h[i]); if(k <= 0) break; h[i] = h[i+1]; } } } if(i+1 >= n) pw.println(-1); else pw.println(i+1); } pw.close(); } public static void pairSort(Pair[] arr) { ArrayList<Pair> l = new ArrayList<>(); Collections.addAll(l, arr); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } public static void longSort(long[] arr) { ArrayList<Long> l = new ArrayList<>(); for (long i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } public static void intSort(int[] arr) { ArrayList<Integer> l = new ArrayList<>(); for (int i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } public long[] nextLongArr(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(next()); } return arr; } } static class Pair implements Comparable<Pair>{ int first; int second; int length; public Pair(int first, int second){ this.first = first; this.second = second; this.length = second-first+1; } @Override public int compareTo(Pair p2) { if(first == p2.first) return second - p2.second; else return first - p2.first; } @Override public String toString() { return "("+ first + "," + second + ')'; } } static class Triple implements Comparable<Triple> { double x, y, z, sum; Triple(double a, double b, double c) { x = a; y = b; z = c; sum = x + y + z; } public int compareTo(Triple t) { if(Math.abs(sum - t.sum) < 1e-9) return x > t.x ? 1 : -1; return sum > t.sum ? 1 : -1; } public String toString() { return x + " " + y + " " + z; } } static PrintWriter pw = new PrintWriter(System.out); static Scanner sc = new Scanner(System.in); static Random random = new Random(); static final int MOD = (int) 1e9 + 7; }
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
488377db5edcdd5c7729f16efa693427
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 Solution{ public static int[] search(int[] h,int ind,int a){ int k = a; int[] used = new int[2]; int j = ind-1; int mf = h[j]; int f = j; int cnt = 1; int d = 0; boolean ok = true; while(ok){ if(f==0||h[f-1]>=h[ind]){ //finally we need to make f<-->j good with ind d = h[ind]-mf; if(k<=d*cnt){ //finally we find the answear of the problem used[0] = a; used[1] = j-(k-1)%cnt; return used; } mf += d; k -= d*cnt; for(int i=f;i<=j;i++){ h[i] = mf; } used[0] = a-k; used[1] = ind; return used; } d = h[f-1]-mf; if(k<=d*cnt){ //finally we find the answear of the problem used[0] = a; used[1] = j-(k-1)%cnt; return used; } mf += d; k -= d*cnt; while(f-1>=0&&h[f-1]==mf){ f--; cnt++; } } return used; } public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for(int z=0;z<t;z++){ int res = -1; st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] h = new int[n]; for(int i=0;i<n;i++) h[i] = Integer.parseInt(st.nextToken()); for(int i=1;i<n;i++){ if(h[i-1]<h[i]){ int[] used = search(h,i,k);//return [how many k used,indice of stoping if all used] k -= used[0]; if(k<=0) res = used[1]+1; } if(res >= 0) break; } out.println(res); } out.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 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
ee41104dc51e87082e802030d912b696
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.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; // graph, dfs,bfs, get connected components,iscycle, isbipartite, dfs on trees public class scratch_25 { static class Graph{ public static class Vertex{ HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex } public static HashMap<Integer,Vertex> vt; // for vertices(all) public Graph(){ vt= new HashMap<>(); } public static int numVer(){ return vt.size(); } public static boolean contVer(int ver){ return vt.containsKey(ver); } public static void addVer(int ver){ Vertex v= new Vertex(); vt.put(ver,v); } public static void addEdge(int ver1, int ver2, int weight){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } Vertex v1= vt.get(ver1); Vertex v2= vt.get(ver2); v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge v2.nb.put(ver1,weight); } public static void delEdge(int ver1, int ver2){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } vt.get(ver1).nb.remove(ver2); vt.get(ver2).nb.remove(ver1); } public static void delVer(int ver){ if(!vt.containsKey(ver)){ return; } Vertex v1= vt.get(ver); ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); vt.get(s).nb.remove(ver); } vt.remove(ver); } static boolean done[]; static int parent[]; static ArrayList<Integer>vals= new ArrayList<>(); public static boolean isCycle(int i){ Stack<Integer>stk= new Stack<>(); stk.push(i); while(!stk.isEmpty()){ int x= stk.pop(); vals.add(x); // System.out.print("current="+x+" stackinit="+stk); if(!done[x]){ done[x]=true; } else if(done[x] ){ return true; } ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet()); for (int j = 0; j <ar.size() ; j++) { if(parent[x]!=ar.get(j)){ parent[ar.get(j)]=x; stk.push(ar.get(j)); } } // System.out.println(" stackfin="+stk); } return false; } static int[]level; static int[] curr; static int[] fin; public static void dfs(int src){ done[src]=true; level[src]=0; Queue<Integer>q= new LinkedList<>(); q.add(src); while(!q.isEmpty()){ int x= q.poll(); ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ level[v]=level[x]+1; done[v]=true; q.offer(v); } } } } static int oc[]; static int ec[]; public static void dfs1(int src){ Queue<Integer>q= new LinkedList<>(); q.add(src); done[src]= true; // int count=0; while(!q.isEmpty()){ int x= q.poll(); // System.out.println("x="+x); int even= ec[x]; int odd= oc[x]; if(level[x]%2==0){ int val= (curr[x]+even)%2; if(val!=fin[x]){ // System.out.println("bc"); even++; vals.add(x); } } else{ int val= (curr[x]+odd)%2; if(val!=fin[x]){ // System.out.println("bc"); odd++; vals.add(x); } } ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); // System.out.println(arr); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ done[v]=true; oc[v]=odd; ec[v]=even; q.add(v); } } } } } // int count=0; //static long count=0; static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { double x; double y; public Pair(double x, double y) { this.x = x; this.y = y;} @Override public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { if(this.x >o.x){ return 1; } else if(this.x==o.x){ return 0; } else{ return -1; } } } // After writing solution, quick scan for: // array out of bounds // special cases e.g. n=1? // // Big numbers arithmetic bugs: // int overflow // sorting, or taking max, or negative after MOD public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t= Reader.nextInt(); for (int tt = 0; tt <t ; tt++) { int n= Reader.nextInt(); long k= Reader.nextLong(); int arr[]= new int[n]; for (int i = 0; i <n ; i++) { arr[i]= Reader.nextInt(); } int ans=-5; while(k>0){ boolean b=false; for (int i = 0; i <n-1 ; i++) { if(arr[i]<arr[i+1]){ b=true; arr[i]++; k--; if(k==0){ ans=i+1; } break; } } // System.out.println(Arrays.toString(arr)+" "+k); if(!b){ ans=-1; break; } } if(ans==-5 ){ for (int i = 0; i <n-1 ; i++) { if(arr[i]<arr[i+1]){ ans=i+1; break; } } } out.append(ans+"\n"); } out.flush(); out.close(); } public static String convert(String s,int n){ if(s.length()==n){ return s; } else{ int x= s.length(); int v=n-x; String str=""; for (int i = 0; i <v ; i++) { str+='0'; } str+=s; return str; } } public static int lower(int arr[],int key){ int low = 0; int high = arr.length-1; while(low < high){ int mid = low + (high - low)/2; if(arr[mid] >= key){ high = mid; } else{ low = mid+1; } } return low; } public static int upper(int arr[],int key){ int low = 0; int high = arr.length-1; while(low < high){ int mid = low + (high - low+1)/2; if(arr[mid] <= key){ low = mid; } else{ high = mid-1; } } return low; } static long modExp(long a, long b, long mod) { //System.out.println("a is " + a + " and b is " + b); if (a==1) return 1; long ans = 1; while (b!=0) { if (b%2==1) { ans = (ans*a)%mod; } a = (a*a)%mod; b/=2; } return ans; } public static long modmul(long a, long b, long mod) { return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod; } static long sum(long n){ // System.out.println("lol="+ (n*(n-1))/2); return (n*(n+1))/2; } public static ArrayList<Integer> Sieve(int n) { boolean arr[]= new boolean [n+1]; Arrays.fill(arr,true); arr[0]=false; arr[1]=false; for (int i = 2; i*i <=n ; i++) { if(arr[i]){ for (int j = 2; j <=n/i ; j++) { int u= i*j; arr[u]=false; }} } ArrayList<Integer> ans= new ArrayList<>(); for (int i = 0; i <n+1 ; i++) { if(arr[i]){ ans.add(i); } } return ans; } static long power( long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static long ceil_div(long a, long b){ return (a+b-1)/b; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } }
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
e2d1250137f20aab76c695c6482de600
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 helloworld { static long fact[]; static long max; static int ans; static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static long getSum(int index , long BITree[]) { long sum = 0; // Iniialize result // index in BITree[] is 1 more than // the index in arr[] // Traverse ancestors of BITree[index] while(index>0) { // Add current element of BITree // to sum sum += BITree[index]; // Move index to parent node in // getSum View index -= index & (-index); } return sum; } static int getMid(int s, int e) { return s + (e - s) / 2; } /* * A recursive function to get the sum of values in given range of the array. * The following are parameters for this function. * * st -> Pointer to segment tree * node -> Index of current node in * the segment tree. * ss & se -> Starting and ending indexes * of the segment represented * by current node, i.e., st[node] * l & r -> Starting and ending indexes * of range query */ static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) { // If segment of this node is completely // part of given range, then return // the max of segment if (l <= ss && r >= se) return st[node]; // If segment of this node does not // belong to given range if (se < l || ss > r) return -1; // If segment of this node is partially // the part of given range int mid = getMid(ss, se); return Math.max(MaxUtil(st, ss, mid, l, r, 2 * node + 1), MaxUtil(st, mid + 1, se, l, r, 2 * node + 2)); } /* * A recursive function to update the nodes which have the given index in their * range. The following are parameters st, ss and se are same as defined above * index -> index of the element to be updated. */ static void updateValue(int arr[], int[] st, int ss, int se, int index, int value, int node) { if (index < ss || index > se) { System.out.println("Invalid Input"); return; } if (ss == se) { // update value in array and in segment tree arr[index] = value; st[node] = value; } else { int mid = getMid(ss, se); if (index >= ss && index <= mid) updateValue(arr, st, ss, mid, index, value, 2 * node + 1); else updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2); st[node] = Math.max(st[2 * node + 1], st[2 * node + 2]); } return; } // Return max of elements in range from // index l (query start) to r (query end). static int getMax(int[] st, int n, int l, int r) { // Check for erroneous input values if (l < 0 || r > n - 1 || l > r) { System.out.printf("Invalid Input\n"); return -1; } return MaxUtil(st, 0, n - 1, l, r, 0); } // A recursive function that constructs Segment // Tree for array[ss..se]. si is index of // current node in segment tree st static int constructSTUtil(int arr[], int ss, int se, int[] st, int si) { // If there is one element in array, store // it in current node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then // recur for left and right subtrees and // store the max of values in this node int mid = getMid(ss, se); st[si] = Math.max(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); return st[si]; } /* * Function to construct segment tree from given array. This function allocates * memory for segment tree. */ static int[] constructST(int arr[], int n) { // Height of segment tree int x = (int) Math.ceil(Math.log(n) / Math.log(2)); // Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; // Allocate memory int[] st = new int[max_size]; // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, st, 0); // Return the constructed segment tree return st; } public static void updateBIT(int n, int index, long val , long BITree[] ) { // index in BITree[] is 1 more than // the index in arr[] // Traverse all ancestors and add 'val' while(index <= n) { // Add 'val' to current node of BIT Tree BITree[index] += val; // Update index to that of parent // in update View index += index & (-index); } } static long mod = 1000000007; static long pow(long a , long b , long c) { if(b==0) return 1; long res = 1; while(b > 0) { if((b&1) == 1) res = res*a%c; b = b>>1; a = a*a%mod; } return res; } static long modInverse(long a , long b) { return pow(a,b-2,mod); } static double d(int x1 , int y1 , int x2 , int y2) { double d1 = x1-x2; double d2 = y1-y2; return Math.sqrt(d1*d1-d2*d2); } static int low[]; static int p[]; static int parent(int x , int k , int dp[][]) { if(k==0) return x; while(k > 0) { if(x == -1) break; int up = low[k]; k -= p[up]; x = dp[x][up]; } return x; } static void d(int n , LinkedList<Integer> arr[] , boolean visited[] , int dp[][] , int h[] , int p,int lev) { visited[n] = true; int lim = low[visited.length]; dp[n][0] = p; int temp = n; for(int j = 1 ; j <= lim ; j++) { if(dp[n][j-1] == -1) break; dp[n][j] = dp[dp[n][j-1]][j-1]; } h[n] = lev; for(Integer i : arr[n]) { if(!visited[i]) { d(i,arr,visited,dp,h,n,lev+1); } } } static int lca(int a , int b , int dp[][] , int h[]) { if(h[a] > h[b]) { a = parent(a,h[a]-h[b],dp); } else if(h[b] > h[a]) { b = parent(b,h[b]-h[a],dp); } if(a==b) return a; int l = 1 , r = h.length; while(l < r) { int mid = (l+r)/2; if(parent(a,mid,dp) == parent(b,mid,dp)) r = mid; else l = mid+1; } return parent(a,l,dp); } static void dfs(int n , LinkedList<Integer> arr[] , boolean visited[] , int dp[] , int idx[] , int ans[]) { visited[n] = true; for(Integer i : arr[n]) { if(!visited[i]) { dfs(i,arr,visited,dp,idx,ans); dp[n] += dp[i]; } } if(n != 1) ans[idx[n]] = dp[n]; //System.out.println(dp[n] + " " + idx[n]); } static boolean check(int x , int arr[][] , int k , int n , int m) { int i1 = 0 , i2 = k-1; //int tot = 0; int bigtot = 0; int sumrow[][] = new int[n][m]; int sumcol[][] = new int[n][m]; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < m ; j++) { if(j > 0) sumrow[i][j] = sumrow[i][j-1]; if(arr[i][j] <= x) sumrow[i][j]++; } } for(int i = 0 ; i < m ; i++) { for(int j = 0 ; j < n ; j++) { if(j > 0) sumcol[j][i] = sumcol[j-1][i]; if(arr[j][i] <= x) sumcol[j][i]++; } } for(int cnt1 = i1 ; cnt1 <= i2 ; cnt1++) { for(int cnt2 = 0 ; cnt2 < k ; cnt2++) { if(arr[cnt1][cnt2] <= x) bigtot++; } } while(i2 < n) { int j1 = 0 , j2 = k-1; int tot = bigtot; while(j2 < m) { if(tot == k*k) return true; if(j2+1 == m) break; if(i1 == 0) { tot += sumcol[i2][j2+1]; tot -= sumcol[i2][j1]; } else { tot += sumcol[i2][j2+1]-sumcol[i1-1][j2+1]; tot -= sumcol[i2][j1]-sumcol[i1-1][j1]; } j2++; j1++; } if(i2+1 == n) break; for(int pos = 0 ; pos < k ; pos++) { if(arr[i1][pos] <= x) bigtot--; if(arr[i2+1][pos] <= x) bigtot++; } i2++; i1++; } return false; } public static void main(String []args) throws IOException { Reader sc = new Reader(); 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(); } boolean found = false; if(n == 1) System.out.println("-1"); else { while(true) { int pos = -1; boolean change = false; for(int i = 0 ; i < n-1 ; i++) { if(arr[i+1] > arr[i]) { k--; pos = i+1; arr[i]++; change = true; break; } } if(!change) { break; } if(k == 0) { found = true; System.out.println(pos); break; } } if(!found) System.out.println("-1"); //System.out.println(found); } } } }
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
5d00af2fa8fabd262f3381f0b4314298
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
/* * Date Created : 5/2/2021 * Have A Good Day ! */ 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 * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(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, FastReader r, OutputWriter out) { int n = r.nextInt(), h = r.nextInt(); int[] arr = r.nextIntArray(n); int ans = -1; while (h-- > 0) { int id = 0; while (id + 1 < n && arr[id + 1] <= arr[id]) id++; arr[id]++; if (id == n - 1) { ans = -1; break; } else ans = id + 1; } out.println(ans); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.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 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
4abcc7f03c00f76d5ee52046d8959b6a
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 B { public static void main(String[] args) throws IOException { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = 1; int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = fs.readArray(n); int[] helper = new int[n]; helper[n-1]=arr[n-1]; for (int i=n-2; i>=0; i--) { helper[i]=Math.max(helper[i+1], arr[i]); } int max = 0; for (int i=n-1; i>=0; i--) { max+=helper[i]-arr[i]; } if (k>max) { out.println(-1); } else { int last=-1; while (k>0) { for (int i=0; i<n-1; i++) { if (arr[i]<arr[i+1]) { k--; arr[i]++; last = i; break; } } } out.println(last+1); } } out.close(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class 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
066bab897d64e0bc01abfaec76baa344
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.util.concurrent.TimeUnit; public class Mridul8 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int g = 0; int t = sc.nextInt(); while(g != t) { g = g + 1; int n = sc.nextInt(); long k = sc.nextLong(); int no = 0,yes = 0; int[] arr = new int[n]; int i = -1; while (i != n - 1) { i = i + 1; arr[i] = sc.nextInt(); } int j = 0; int u = -1; while (j != k) { j = j + 1; i = -1; no = 0; while (i != n - 2) { i = i + 1; if (arr[i] < arr[i + 1]) { no = 1; break; } } if (no == 0) { yes = 1; break; } else { i = -1; while (i != n - 2) { i = i + 1; if (arr[i] < arr[i + 1]) { arr[i] = arr[i] + 1; if (j == k) { u = i + 1; } break; } } } } if (yes == 1) { System.out.println(-1); } else { System.out.println(u); } } } }
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
0474f5737298aef5c558d0f9c0547002
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 Problem1481B { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int tc = scanner.nextInt(); while (tc-->0){ int size = scanner.nextInt(); int k = scanner.nextInt(); int [] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = scanner.nextInt(); } int ans=-1 ; while (k-->0) { ans = -1; for (int i = 0; i < array.length - 1; i++) { if (array[i] < array[i + 1]) { ans = i; break; } } if (ans==-1){ break; } array[ans]++; } if (ans==-1){ 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 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
e9a9720d562ec5231f4849484fbf39e8
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
//<———My cp———— import java.util.*; import java.io.*; public class B_New_Colony{ public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int k = fr.nextInt(); int[] vals = new int[n]; for(int i = 0;i<n;i++){ vals[i] = fr.nextInt(); } int needed = 0; int max = 0; for(int i = n-1;i>=0;i--){ if(vals[i]>max){ max = vals[i]; } if(vals[i]<max){ needed += (max-vals[i]); } } // println("N: "+needed); // println("K: "+k); if(k>needed){ println(-1); }else{ int endIndex = 0; while(k>0){ for(int i = 1;i<n;i++){ int prev = vals[i-1]; int curr = vals[i]; if(prev<curr){ int add = curr-prev; if(k==1){ endIndex = i-1; }else{ vals[i-1]++; } k--; break; } } } // println("--------------"); // for(int i =0;i<n;i++){ // print(vals[i]+" "); // } // println(""); println(endIndex+1); } } } public static void print(Object val){ System.out.print(val); } public static void println(Object val){ System.out.println(val); } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
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
3fb08eab9f58e754acef4b3f87fadc82
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
// MyPackage; import java.io.*; import java.util.*; public class MyClass { final static long mod = 1_000_000_007; final static int mini = Integer.MIN_VALUE; final static int maxi = Integer.MAX_VALUE; static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ncr(long n, long r) { if (r > n - r) r = n - r; long[] C = new long[(int) r + 1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (long j = Math.min(i, r); j > 0; j--) C[(int) j] = (C[(int) j] + C[(int) j - 1]) % mod; } return C[(int) r]; } static boolean isPrime(long 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 boolean isPalindrome(String s, int k) { int i = 0, j = s.length() - 1; while(i < k) { if(s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } static int ceil(int a, int b) { return a % b == 0 ? a / b : a / b + 1; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } static void reverse(int[] arr) { int l = 0; int h = arr.length - 1; while (l < h) { swap(arr, l, h); l++; h--; } } static String reverse(String s) { StringBuilder res = new StringBuilder(s); return res.reverse().toString(); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String n() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nint() { return Integer.parseInt(n()); } long nlong() { return Long.parseLong(n()); } double ndouble() { return Double.parseDouble(n()); } int[] narr(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nint(); return a; } int[][] narr(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] = nint(); return a; } String nline() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); // int t = 1; int t = sc.nint(); while(t-- > 0) { int n = sc.nint(); int k = sc.nint(); int[] arr = sc.narr(n); int res = -1; while(k-- > 0) { boolean flag = false; for(int i = 0; i < n - 1; i++) { if(arr[i + 1] > arr[i]) { arr[i]++; res = i + 1; flag = true; break; } else res = -1; } if(!flag) { res = -1; break; } } out.println(res); } out.flush(); 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 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
cdf29372271e299191669612c76afec0
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 _1481b { FastScanner scn; PrintWriter w; PrintStream fs; long MOD = 1000000007; int N=100000+10; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} boolean LOCAL; void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} void solve(){ int t=scn.nextInt(); while(t-->0) { int n=scn.nextInt(); long k=scn.nextLong(); int[] ar=new int[n]; for(int i=0;i<n;i++) { ar[i]=scn.nextInt(); } int j=0; for(int i=0;i<k;i++) { for(j=0;j<n-1;j++) { if(ar[j]<ar[j+1]){ ar[j]++; break; } } if(j==n-1) break; } if(j!=n-1) w.println(j+1); else w.println(-1); } } void run() { try { long ct = System.currentTimeMillis(); scn = new FastScanner(new File("input.txt")); w = new PrintWriter(new File("output.txt")); fs=new PrintStream("error.txt"); System.setErr(fs); LOCAL=true; solve(); w.close(); System.err.println(System.currentTimeMillis() - ct); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { scn = new FastScanner(System.in); w = new PrintWriter(System.out); LOCAL=false; solve(); w.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(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()); } } public static void main(String[] args) { new _1481b().runIO(); } }
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
40e2f55429f4ee273d77ef3180895f9a
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 _1481B { 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(); int pos = -1; outer: while (k > 0) { for (int i = 1; i < n; i++) { if (h[i] > h[i - 1]) { k--; h[i - 1]++; if (k == 0) { pos = i; break outer; } continue outer; } } 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 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
3972b7e6bb565a59c67c107cc6914d3e
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 t=sc.nextInt(); for(int q=0;q<t;q++) { 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 c=-1; for(int i=0;i<k;i++) { boolean x=false; for(int j=0;j<n-1;j++) { if(a[j]<a[j+1]) { x=true; c=j; a[j]=a[j]+1; break; } } if(x==false) { c=-1; break; } } if(c==-1) System.out.println(-1); else System.out.println(c+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
66fb588c290cfeed835e76f450db8fc1
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.*; /** * * @author alanl */ public class Solve { /** * @param args the command line arguments */ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException { int t = readInt(); while(t-->0){ int n = readInt(), k = readInt(), arr[] = new int[n]; for(int i = 0; i<n; i++){ arr[i] = readInt(); } int cur = 0, ans = 0, cnt = 0; while(cnt<k){ int min = arr[0], idx = 0; for(int i = 1; i<n; i++){ if(min>=arr[i]){ min = arr[i]; idx = i; } else break; } if(idx==n-1){ ans = -1; break; } arr[idx]++; ans = idx+1; cnt++; if(cnt>99*(n-1)){ ans = -1; break; } } println(ans); } } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine().trim()); return st.nextToken(); } static long readLong () throws IOException { return Long.parseLong(next()); } static int readInt () throws IOException { return Integer.parseInt(next()); } static double readDouble () throws IOException { return Double.parseDouble(next()); } static char readChar () throws IOException { return next().charAt(0); } static String readLine () throws IOException { return input.readLine().trim(); } static void print(Object b) { System.out.print(b); } static void println(Object b) { System.out.println(b); } static void println() { System.out.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 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
31b839795df96e71b1eeacc4efc8449a
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.io.*; import java.util.*; public class NewColony { public static void main(String[] args)throws Exception{ new NewColony().run();} long mod=1000000000+7; // int[][] ar; void solve() throws Exception { for(int t=ni();t>0;t--){ int n=ni(); int k=ni(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = ni(); } int cnt=0; while(true){ int s =0; while(s<n-1 && a[s]>=a[s+1]){ s++; } cnt++; a[s]++; if(s==n-1){ out.println(-1);break; } if(cnt==k){ out.println(s+1);break; } } } } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["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
070894b0b13b852eb8b88b7b02e32dcf
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.*; import java.awt.Point; public class Main { static final long MOD = 1000000007L; //static final long MOD2 = 1000000009L; //static final long MOD = 998244353L; //static final long INF = 500000000000L; static final int INF = 1000000005; static final int NINF = -1000000005; //static final long NINF = -1000000000000000000L; static FastScanner sc; static PrintWriter pw; static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int T = sc.ni(); for (int t = 0; t < T; t++) { int N = sc.ni(); int K = sc.ni(); int[] heights = sc.intArray(N,0); int ans = 64346; // the index where the K-th boulder stops while (K > 0) { //the index where the boulder stop int i = operation(heights); if (i==-1) { ans = -1; break; } else { //the height of the mountain will go up by 1 heights[i] += 1; ans = i; } K--; } if (ans==-1) pw.println(-1); else pw.println(ans+1); } pw.close(); } //return the index where the boulder stops (or -1 if it doesn't stop) public static int operation(int[] heights) { for (int i = 0; i < heights.length-1; i++) { if (heights[i] < heights[i+1]) { return i; } } return -1; } public static void sort(int[] arr) { Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int r = rgen.nextInt(arr.length); int temp = arr[i]; arr[i] = arr[r]; arr[r] = temp; } Arrays.sort(arr); } public static void sort(long[] arr) { Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int r = rgen.nextInt(arr.length); long temp = arr[i]; arr[i] = arr[r]; arr[r] = temp; } Arrays.sort(arr); } //Sort an array (immune to quicksort TLE) public static void sort(int[][] arr) { Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int r = rgen.nextInt(arr.length); int[] temp = arr[i]; arr[i] = arr[r]; arr[r] = temp; } Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { return a[0]-b[0]; //Ascending order. } }); } public static void sort(long[][] arr) { Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int r = rgen.nextInt(arr.length); long[] temp = arr[i]; arr[i] = arr[r]; arr[r] = temp; } Arrays.sort(arr, new Comparator<long[]>() { @Override public int compare(long[] a, long[] b) { if (a[0] > b[0]) return 1; else if (a[0] < b[0]) return -1; else return 0; //Ascending order. } }); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[][] graph(int N, int[][] edges) { int[][] graph = new int[N][]; int[] sz = new int[N]; for (int[] e: edges) { sz[e[0]] += 1; sz[e[1]] += 1; } for (int i = 0; i < N; i++) { graph[i] = new int[sz[i]]; } int[] cur = new int[N]; for (int[] e: edges) { graph[e[0]][cur[e[0]]] = e[1]; graph[e[1]][cur[e[1]]] = e[0]; cur[e[0]] += 1; cur[e[1]] += 1; } return graph; } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni()+mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl()+mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
ac609f6620784d4910700ab511b36338
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.*; import java.awt.Point; public class CFTemplate { static final long MOD = 1000000007L; //static final long MOD2 = 1000000009L; //static final long MOD = 998244353L; //static final long INF = 500000000000L; static final int INF = 100000000; static final int NINF = -100000; static FastScanner sc; static PrintWriter pw; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { int N = sc.ni(); int K = sc.ni(); int[] H = sc.intArray(N, 0); int ans = -2; while (K > 0) { int d = -2; for (int i = 0; i < N-1; i++) { if (H[i] < H[i+1]) { d = i; H[i] += 1; break; } } K--; if (K==0) ans = d; if (d==-2) break; } pw.println(ans+1); } pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni()+mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl()+mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
4a94d895a89f3ea43b7d3c01a1037af8
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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class New_Colony { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static int getPosition(int n,int k,int[] h) { int y=0,d=0; //depth_to_fill int max=h[n-1]; for(int i=n-2;i>-1;i--) { if(h[i]<max)y+=max-h[i]; else if(h[i]>max)max=h[i]; } if(y<k) {return -1;} for(int i=1;i<k;i++) { for(int j=0;j<n;j++) { if(j+1!=n && h[j]<h[j+1]) {h[j]++;break;} } } for(int j=0;j<n;j++) { if(j+1!=n && h[j]<h[j+1])return j+1; } return -1; } public static void main (String[] args) throws java.lang.Exception{ InputReader sc=new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int n,k,t=sc.nextInt(); String[] s; int[] height; while(t-->0) { n=sc.nextInt(); k=sc.nextInt(); s=sc.nextLine().split(" "); height=new int[n]; for(int i=0;i<n;i++) height[i]=Integer.parseInt(s[i]); out.println(getPosition(n,k,height)); } out.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 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
44d3cb93bc8e1ab9a15d9a1621a47a40
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.util.*; import java.io.InputStreamReader; public class Main { public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testCases = Integer.parseInt(br.readLine()); for (int testCase = 1; testCase <= testCases; testCase++) { String s[] = br.readLine().split(" "); int mountains = Integer.parseInt(s[0]); int boulders = Integer.parseInt(s[1]); s = br.readLine().split(" "); int heights[] = new int[s.length]; for (int i = 0; i < mountains; i ++) heights[i] = Integer.parseInt(s[i]); List<Integer> increasingSequence = new ArrayList<>(); for (int i = 0; i < mountains - 1; i ++) if (heights[i] < heights[i + 1]) increasingSequence.add(i); // System.out.println(increasingSequence); int ans = -1; int boulder = 0; for (boulder = 1; boulder <= boulders && increasingSequence.size() != 0; boulder ++) { int rollingStopIndex = increasingSequence.get(0); ans = rollingStopIndex; heights[rollingStopIndex] ++; int heightNextMountain = rollingStopIndex + 1 < mountains? heights[rollingStopIndex + 1]: 0; if (heights[rollingStopIndex] >= heightNextMountain) increasingSequence.remove(0); if (rollingStopIndex - 1 >= 0 && heights[rollingStopIndex] > heights[rollingStopIndex - 1]) increasingSequence.add(0, rollingStopIndex - 1); // System.out.println("boulder "+boulder+"; heights "+Arrays.toString(heights)+"; ans "+ans+"; sequence"+" "+increasingSequence); } if (boulder != boulders + 1)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 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
a203b1f235d0474bc3d3bf117b506b33
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.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Fedoresko */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Task1481b solver = new Task1481b(); solver.solve(1, in, out); out.close(); } static class Task1481b { public void solve(int testNumber, FastScanner in, PrintWriter out) { int t = in.nextInt(); for (int p = 0; p < t; p++) { int n = in.nextInt(); long k = in.nextLong(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); } ArrayList<Integer>[] otrs = new ArrayList[101]; for (int i = 0; i < 101; i++) { otrs[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { if (h[i] > h[i - 1]) { for (int y = h[i - 1]; y < h[i]; y++) { if (otrs[y].isEmpty()) { otrs[y].add(0); } otrs[y].add(i); } } else if (h[i] < h[i - 1]) { for (int y = h[i]; y < h[i - 1]; y++) { otrs[y].add(i); } } } int[] it = new int[101]; for (int i = 0; i < 101; i++) { it[i] = 0; } boolean ok = false; int lev = h[0]; while (true) { while (lev >= 0 && otrs[lev].size() <= it[lev] + 1) { lev--; } if (lev < 0) break; int b = otrs[lev].get(it[lev]); int e = otrs[lev].get(it[lev] + 1); if (lev > 0 && otrs[lev - 1].size() > it[lev - 1] + 1 && b <= otrs[lev - 1].get(it[lev - 1]) && e >= otrs[lev - 1].get(it[lev - 1] + 1)) { lev = lev - 1; } else { if (k > e - b) { k -= e - b; it[lev] += 2; lev = lev + 1; } else { out.println(e - k + 1); ok = true; break; } } } if (!ok) out.println(-1); } } } static class FastScanner { private final BufferedReader reader; private StringTokenizer tokenizer; public FastScanner(final InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String next() { pushForward(); return tokenizer != null ? tokenizer.nextToken() : null; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } private void pushForward() { if (tokenizer == null) { nextTokenizer(); } while (tokenizer != null && !tokenizer.hasMoreTokens()) { tokenizer = null; nextTokenizer(); } } private void nextTokenizer() { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException 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 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
4a43a9b7b160e09b16615781b78faf7d
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.lang.Math; public class sol { private static int MAX = 102; public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t; t = sc.nextInt(); while(t>0) { int n, k; n = sc.nextInt(); k = sc.nextInt(); int[] h = new int[MAX]; int i; h[0]=MAX; for(i=1;i<=n;i++){ h[i] = sc.nextInt(); } i=1; while(k>0 && i<=n){ while(h[i]>h[i-1]){ i--; } while(i<=n && h[i]>=h[i+1]){ i++; } while(i<=n && k>0 && h[i]<Math.min(h[i-1]+1, h[i+1])){ h[i]=h[i]+1; k--; } } if(i>n){ System.out.println(-1); } else{ System.out.println(i); } 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
fad29374cd21a9ee74574d347b5cd830
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 test { //static int[] arr; public static void main (String[] args) throws Exception { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int y=Integer.parseInt(bf.readLine()); for(int h=0;h<y;h++) { String[] xl=(bf.readLine()).split(" ");int i=0; long a,b,m;long ans=0l; a=Long.parseLong(xl[0]); b=Long.parseLong(xl[1]); //m=Integer.parseInt(xl[2]); //m2=new long[a]; long[] m1=new long[(int)(a)]; xl=(bf.readLine()).split(" "); for(int o=0;o<m1.length;o++) { m1[o] = Long.parseLong(xl[o]); } long prev= m1[m1.length-1];long su=0l; for(int o=m1.length-2;o>=0;o--) { if(m1[o] < prev) su+=(prev-m1[o]); else prev=m1[o]; } if(b>su) { System.out.println("-1");continue; } else { int lm=(int)(b); int lastPos=-1; for(int q=0;q<b;q++) { for(int w=0;w<m1.length;w++) { if((w+1)<m1.length && m1[w]<m1[w+1]) { m1[w]++;lastPos=w+1;break; } } } System.out.println(lastPos); } } } }
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
9effac4bf050c9567fec09ef16fe5efb
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 NewColony { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int j=0, i=0; while(t>0) { int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[n]; for(i=0;i<n;i++) { a[i] = sc.nextInt(); } for(i=0;i<k;i++) { for(j=0;j<n-1;j++) { if(a[j]<a[j+1]) { a[j]++; break; } } if(j == n-1) { j = -1; System.out.println(-1); break; } } if(j != -1) System.out.println(j+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
e1826337d035bfdb3c1f057d59e2f8ac
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 B { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(), b = fs.nextInt(); int a[] = fs.readArray(n); for (int i = 0; ; i++) { int curr = -1; for (int j = 0; j < n-1; j++) { if (a[j + 1] > a[j]) { a[j]++; curr = j;b--; break; } } if (curr == -1) { System.out.println(curr); break; } if (b==0) { System.out.println(curr+1); break; } } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } 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
ba550c2cd71da4f99139e2e6db2af4fd
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.*; public class CFB { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t--!=0) { String[] line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); long k = Long.parseLong(line[1]); int [] arr = new int[n]; line = br.readLine().split(" "); for(int i=0;i<n;i++){ arr[i] = Integer.parseInt(line[i]); } int ans = 0; while(k>0){ boolean isPoss = false; for(int i=0;i<n-1;i++){ if(arr[i+1]>arr[i]){ arr[i]++; k--; ans = i+1; isPoss = true; break; } } if(!isPoss){ ans = -1; 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 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
991ef79848d25f18926e4524ca906b6f
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 { public static void main(String args[]) throws FileNotFoundException{ Scanner in; if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file File input = new File("input.txt"); in = new Scanner(input); } else { // Input is System.in in = new Scanner(System.in); } int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int k = in.nextInt(); int h[] = new int[n]; for (int j = 0; j < n; j++) { h[j] = in.nextInt(); } solve(n, k, h); } } private static void solve(int n, int k, int h[]) { int lastMax = h[n - 1]; int sum = 0; for (int i = n - 2; i >= 0; i--) { if (h[i] > lastMax) { lastMax = h[i]; } else { sum = sum + (lastMax - h[i]); } } if (k > sum) { System.out.println(-1); } else { // System.out.println(sum); // find Position int position = 0; int currentSum = 0; boolean resultFound = false; while (!resultFound) { for (int i = 0; i < n - 1; i++) { if (h[i] < h[i + 1]) { position = i + 1; currentSum++; h[i]++; if (currentSum >= k) { resultFound = true; } break; } } // System.out.print(currentSum + " "); // for (int q = 0; q < n; q++) { // System.out.print(h[q] + " "); // } // System.out.println(); } System.out.println(position); } } } // 25 // 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 //
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
ed97ab73b64374f87c89a8b8d61f2d07
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 vivek{ 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(); } char nextChar() { return next().charAt(0); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int rows) { int arr[] = new int[rows]; for (int i = 0; i < rows; i++) arr[i] = nextInt(); return arr; } long[] nextLongArray(int rows) { long arr[] = new long[rows]; for (int i = 0; i < rows; i++) arr[i] = nextLong(); return arr; } char[] nextCharArray(int rows) { char arr[] = new char[rows]; for (int i = 0; i < rows; i++) arr[i] = next().charAt(0); return arr; } int[][] nextMatrix(int rows, int columns) { int mat[][] = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { mat[i][j] = nextInt(); } } return mat; } } static final Random random=new Random(); static void sort(int a[]){ int n=a.length; for(int i=0;i<n;i++){ int oi=random.nextInt(n) , temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static int gcd(int a,int b){ if(b==0) return 1; else return gcd(b,a%b); } static int mod(int a){ if(a<0) return a*-1; return a; } public static void main(String args[]){ FastReader sc=new FastReader(); PrintWriter p=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long k=sc.nextLong(); int a[]=new int [n+1]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); a[n]=-1; int f=0; while(k>0){ k--; for(int i=0;i<n;i++){ if(a[i]<a[i+1]){ a[i]++; f=i+1; //p.print(f+" "); break; } else if(i==n-1) f=-1; } if(f==-1 ) break; } if(f>0) p.println(f); else if(f==-1) p.println("-1"); } p.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 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
7361d64cc0db88a3025a9dd1387d59a3
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 contest.CodeforcesRound699; import java.io.*; import java.util.*; /*********************** * @oj: codeforces * @id: hitwanyang * @email: 296866643@qq.com * @date: 2021/2/6 13:44 * @url: https://codeforc.es/contest/1481/problem/B ***********************/ public class B { InputStream is; FastWriter out; String INPUT = ""; //提交时注意需要注释掉首行package //基础类型数组例如long[]使用Arrays排序容易TLE,可以替换成Long[] //int 最大值2**31-1,2147483647; //尽量使用long类型,避免int计算的数据溢出 void solve() { int t = ni(); for (; t > 0; t--) go(); } void go() { int n = ni(), k = ni(); int index = k; int[] a = na(n); List<Integer> pos = new ArrayList<>(); while (k > 0) { boolean f = true; for (int i = 0; i < n; i++) { if (i > 0 && a[i] > a[i - 1]) { a[i - 1]++; pos.add(i); k--; f = false; break; } } if (f) { break; } } // out.println(a); // out.println(pos.toString()); if (pos.size() == 0 || k > 0) { out.println(-1); } else { out.println(pos.get(index - 1)); } } void run() throws Exception { is = System.in; out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); //debug log //tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) map[i] = na(m); return map; } private int ni() { return (int) nl(); } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for (int i = 0; i < o.length; i++) if (o[i] != 0) System.out.print(i + ":" + o[i] + " "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for (int i = 0; i < o.length; i++) { for (long x = o[i]; x != 0; x &= x - 1) stands.add(i << 6 | Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } public void tf(boolean[]... b) { for (boolean[] r : b) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if (INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if (INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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
32197e2d0776d7365ed888be4784c1ed
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 colony { public static int MAX_BOULD = 9803; public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); for (int z = 0; z < t; z++) { StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); if (K >= MAX_BOULD) { pw.println("-1"); continue; } int[] mountains = new int[N]; for (int i = 0; i < N; i++) { mountains[i] = Integer.parseInt(st.nextToken()); } // System.out.println("MOUNTS: " + Arrays.toString(mountains)); boolean done = false; int curIndex = 0; for (int q = 0; q < K; q++) { curIndex = 0; while (curIndex < N - 1) { if (mountains[curIndex] >= mountains[curIndex+1]) { curIndex++; } else { mountains[curIndex]++; break; } } if (curIndex == N - 1) { pw.println("-1"); done = true; break; } } if (!done) { pw.println((curIndex + 1)); } } pw.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 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
c8b44712ed2771af23d57af485b2afe6
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 arr[] = new int [n]; for(int i = 0 ; i < n ; i++){ arr[i] = sc.nextInt(); } int posi = -1; for(int i = 0 ; i < k ; i++){ int c = 1; for(int j = 0 ; j < n-1 ; j++){ if(arr[j] >= arr[j+1]){ c++; }else{ arr[j] = arr[j]+1; break; } } if(c == n){ posi = -1; break; }else{ posi = c; } } System.out.println(posi); } } }
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
5d0909b37b47503709c074f4cea65d90
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 NewColony { public static void main(String[] args) { FastReader fastReader = new FastReader(); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int k = fastReader.nextInt(); int h[] = new int[n]; for (int i = 0; i < n; i++) { h[i] = fastReader.nextInt(); } int result = -1; while (k-- > 0) { for (int i = 0; i < n; i++) { if (i == n - 1) { result = -1; break; } if (h[i] < h[i + 1]) { h[i]++; result = i + 1; break; } } if (result == -1) { break; } } System.out.println(result); } } 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 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
e954a0632bf7c35bc73f3fee52954082
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.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; public class N699 { static PrintWriter out; static Scanner sc; static ArrayList<Integer>primes; static HashSet<Integer>primesH; static boolean prime[]; //static ArrayList<Integer>a; static HashSet<Long>h; static boolean[][]v; static int[]a,b,c,oa,oc; static HashSet<Integer>[]ob; static int[][]mp; static long[]l; public static void main(String[]args) throws IOException { sc=new Scanner(System.in); out=new PrintWriter(System.out); //A(); //sieveOfEratosthenes(1000000); B(); //C(); //minimum for subarrays of fixed length //D(); out.close(); } private static void A() throws IOException { int t=sc.nextInt(); while(t-->0) { int px=sc.nextInt(),py=sc.nextInt(); int r=0,u=0,d=0,l=0; String s=sc.next(); for(int i=0;i<s.length();i++) { switch(s.charAt(i)) { case 'U':u++;break; case'R':r++;break; case 'D':d++;break; case'L':l++;break; } } int x=r-l,y=u-d; boolean a=px-x<=l&&px>=x; boolean b=py-y<=d&&py>=y; boolean c=x-px<=r&&px<=x; boolean e=y-py<=u&&py<=y; if(px==x&&py==y||a&&b||a&&e||c&&b||c&&e) { out.println("YES"); }else { out.println("NO"); } } } static void B() throws IOException { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),k=sc.nextInt(); a=sc.nextArrInt(n); int pos=-1; int i=0; for(;i<n-1;i++) { if(a[i]>=a[i+1])continue; k--; a[i]++; if(k<=0)break; i-=2; if(i<-1) { i=-1; } } out.println(i==n-1?-1:i+1); } } static void C() throws IOException { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),m=sc.nextInt(); a=sc.nextArrInt(n); b=sc.nextArrInt(n); c=sc.nextArrInt(m); ob=new HashSet[n+1]; int[]ans=new int[m]; // ob=new int[n+1]; // oc=new int[n+1]; for(int i=0;i<=n;i++) { ob[i]=new HashSet<Integer>(); } for(int i=0;i<n;i++) { if(a[i]!=b[i]) ob[b[i]].add(i); } HashSet<Integer>last=ob[c[m-1]]; int pos=getElement(last); if(pos==-1) { int g=c[m-1]; for(int i=n-1;i>=0;i--) { if(b[i]==g) { pos=i; break; } } }else { ob[c[m-1]].remove(pos); } if(pos==-1) { //noooo out.println("NO"); }else { ans[m-1]=pos; for(int i=0;i<m-1;i++) { int p=getElement(ob[c[i]]); if(p==-1) { ans[i]=pos; }else { ans[i]=p; ob[c[i]].remove(p); } } boolean f=true; for(HashSet<Integer>j:ob) { if(j.size()!=0) { f=false; break; } } if(f) { out.println("YES"); for(int i=0;i<m;i++) { out.print((ans[i]+1)+" "); } out.println(); }else { out.println("NO"); } } } } static int getElement(HashSet<Integer>k) { for(int e:k) { return e; } return -1; } static void D() throws IOException { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),m=sc.nextInt(); mp=new int[n][n]; for(int i=0;i<n;i++) { String s=sc.next(); for(int j=0;j<n;j++) { mp[i][j]=i==j?-1:s.charAt(i)=='a'?1:0; } } if(m==1) { out.println("YES"); out.println("1 2"); }else { boolean f=false;int v1=-1,v2=-1; int h=(int) Math.floor((n-1)/2); for(int i=0;i<=h&&!f;i++) { for(int j=0;j<=h&&!f;j++) { if(j==i)continue; if(mp[i][j]==mp[j][i]) { f=true; v1=i;v2=j; } } } if(f) { //ok }else { if(m%2==0) { if(n==2) { //no }else { //maybe int max=Integer.MIN_VALUE; v=new boolean[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(!v[i][j]) { int cur=solve(i,j); if(cur==-1) { } max=Math.max(max, cur); } } } } }else { //ok } } } } // Math.floor((n-1)/2); } private static int solve(int i, int j) { int x=i,y=j; int max=0; for(int t=0;t<mp.length;t++) { } return 0; } static class Pair{ int a,min; Pair(int a,int min){ this.a=a; this.min=min; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static void sieveOfEratosthenes(int n) // O(n*log(log(n))) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. 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] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for(int i = 2; i <= n; i++) { if(prime[i] == true) { //System.out.print(i + " "); //primes.add(i); //primesH.add(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 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
5086bb291c0a21f854bfb280568035ca
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.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Stack; import java.util.StringTokenizer; import static java.lang.Math.*; public class B { static FastScanner sc; static PrintWriter pw; public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(System.out); int T = sc.nextInt(); while (T-- > 0) { solve(); } pw.close(); } private static void solve() { 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 ans = -1; while(k-- > 0) { ans = check(h); if(ans == -1) { pw.println(-1); return; } } pw.println(ans); } static int check(int[] h) { int n = h.length; for(int i = 0; i < n - 1; i++) { if(h[i] >= h[i+1]) { } else { h[i]++; return i+1; } } return -1; } static void debug(Object... O) { System.out.println(Arrays.deepToString(O)); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["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
bd2214175e66464d67f783a107b0323e
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.HashMap; import java.util.Map; import java.util.Scanner; public class A { 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 ar[] = new int[n]; for(int i = 0; i<n; i++) { ar[i] = sc.nextInt(); } int u= 0; if(n==1) { System.out.println(-1); } else { boolean check = false; int ans = -1; for(int i = 0; i<n-1; i++) { int a = ar[i]; int b = ar[i+1]; if(a>=b) { continue; } else { u++; ar[i]++; if(u==k){ ans = i+1; check = true; break; } else { i = -1; } } } if(check) { 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
a347571d7fd161acac14daf638ac4ad9
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.*; import java.lang.*; import java.util.LinkedList; import java.util.Queue; import static java.lang.Math.*; public class Cf294 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] nextArray(int n) { int arr[] = new int[n]; int i; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } 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 int[] nextIntArray(int n) { int i; int arr[] = new int[n]; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { int i; long arr[] = new long[n]; for(i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) { int i; double arr[] = new double[n]; for(i=0;i<n;i++) { arr[i] = nextDouble(); } return arr; } public Integer[] IntegerArray(int n) { int i; Integer arr[] = new Integer[n]; for(i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public Long[] LongArray(int n) { int i; Long arr[] = new Long[n]; for(i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public Double[] DoubleArray(int n) { int i; Double arr[] = new Double[n]; for(i=0;i<n;i++) { arr[i] = nextDouble(); } return arr; } public ArrayList<ArrayList<Integer>> getGraph(int n,int m) { ArrayList<ArrayList<Integer>> g =new ArrayList<>(); int i; for(i=0;i<=n;i++) { g.add(new ArrayList<>()); } for(i=0;i<m;i++) { int x = nextInt(); int y = nextInt(); g.get(x).add(y); g.get(y).add(x); } return g; } public ArrayList<ArrayList<Integer>> getTree(int n) { ArrayList<ArrayList<Integer>> g =new ArrayList<>(); int i; for(i=0;i<=n;i++) { g.add(new ArrayList<>()); } for(i=0;i<n-1;i++) { int x = nextInt(); int y = nextInt(); g.get(x).add(y); g.get(y).add(x); } return g; } } public static int maxInt(int arr[]) { int max = Integer.MIN_VALUE; for(int i : arr) { max = Math.max(i,max); } return max; } public static int minInt(int arr[]) { int min = Integer.MAX_VALUE; for(int i : arr) { min = Math.min(i,min); } return min; } public static long maxLong(long arr[]) { long max = Long.MIN_VALUE; for(long i : arr) { max = Math.max(i,max); } return max; } public static long minLong(long arr[]) { long min = Long.MAX_VALUE; for(long i : arr) { min = Math.min(i,min); } return min; } public static double maxDouble(double arr[]) { double max = Double.MIN_VALUE; for(double i : arr) { max = Math.max(i,max); } return max; } public static double minDouble(double arr[]) { double min = Double.MAX_VALUE; for(double i : arr) { min = Math.min(i,min); } return min; } void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static class Pair { int x,y; public Pair(int x,int y) { this.x = x; this.y = y; } public static Comparator<Pair> sortX = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.x==p2.x) { return (int)(p1.y-p2.y); } return (int)(p1.x-p2.x); } }; public static Comparator<Pair> sortY = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.y==p2.y) { return (int)(p1.x-p2.x); } return (int)(p1.y-p2.y); } }; public static Comparator<Pair> custom = new Comparator<Pair>(){ public int compare(Pair p1,Pair p2) { if(p1.x-p2.x==0) { return -1*(p1.y-p2.y); } return -1*(p1.x-p2.x); } }; public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof Pair)) return false; return (this.x == ((Pair) obj).x && this.y == ((Pair)obj).y); } public int hashCode() { if(x<0 || y<0) { return (int)(-5*x+-32*y); } return (int)(5*(x+y)); } } static class DPair { Pair s,d; public DPair(Pair s,Pair d) { this.s = s; this.d = d; } public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof DPair)) return false; return (this.s.x == ((DPair) obj).s.x && this.s.y == ((DPair) obj).s.y && this.d.x == ((DPair)obj).d.x && this.d.y == ((DPair) obj).d.y); } public int hashCode() { return 5*(s.x+s.y+d.x+d.y); } } static class DSU { public static int rank[],parent[]; int n; HashMap<Integer,Integer> map = new HashMap<>(); public DSU(int n) { rank = new int[n+1]; parent = new int[n+1]; this.n = n; makeSet(n); } public void makeSet(int n) { for(int i=1;i<=n;i++) { parent[i] = i; map.put(i,1); } } public static int find(int x) { if(parent[x]!=x) { parent[x] = find(parent[x]); } return parent[x]; } public void union(int x,int y) { int xRoot = find(x); int yRoot = find(y); if(xRoot==yRoot) { return; } if(rank[xRoot] < rank[yRoot]) { parent[xRoot] = yRoot; rank[yRoot] = rank[xRoot] + 1; map.put(yRoot,map.getOrDefault(yRoot,1)+map.getOrDefault(xRoot,1)); if(map.getOrDefault(xRoot,0)!=0) { map.remove(xRoot); } } else if(rank[yRoot] < rank[xRoot]) { parent[yRoot] = xRoot; rank[xRoot] = rank[yRoot] + 1; map.put(xRoot,map.getOrDefault(xRoot,1) + map.getOrDefault(yRoot,1)); if(map.getOrDefault(yRoot,0)!=0) { map.remove(yRoot); } } else { parent[yRoot] = xRoot; rank[xRoot] = rank[yRoot] + 1; map.put(xRoot,map.getOrDefault(xRoot,1)+map.getOrDefault(yRoot,1)); if(map.getOrDefault(yRoot,0)!=0) { map.remove(yRoot); } } } } static class SegmentTree { public static int tree[]; public SegmentTree(int arr[], int n) { int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int size = 2 * (int) Math.pow(2, x) - 1; tree = new int[size]; constructTree(arr, 0, n - 1, 0); } public int getMid(int s, int e) { return s + (e - s) / 2; } public int minVal(int x, int y) { return (x < y) ? x : y; } public int constructTree(int arr[], int s, int e, int ind) { if (s == e) { tree[ind] = arr[s]; return arr[s]; } int mid = getMid(s, e); tree[ind] = constructTree(arr, s, mid, ind * 2 + 1) + constructTree(arr, mid + 1, e, ind * 2 + 2); return tree[ind]; } public long getSum(int n, int l, int r) { if (l < 0 || r > n - 1 || l > r) { return -1; } return getSumR(0, n - 1, l, r, 0); } public long getSumR(int s, int e, int l, int r, int ind) { if (l <= s && r >= e) { return tree[ind]; } if (e < l || s > r) { return 0; } int mid = getMid(s, e); return getSumR(s, mid, l, r, 2 * ind + 1) + getSumR(mid + 1, e, l, r, 2 * ind + 2); } public void updateValue(int arr[], int n, int i, int val) { if (i < 0 || i > n - 1) { return; } int diff = val - arr[i]; arr[i] = val; updateValueR(0, n - 1, i, diff, 0); } public void updateValueR(int s, int e, int i, int diff, int ind) { if (i < s || i > e) { return; } tree[ind] = tree[ind] + diff; if (e != s) { int mid = getMid(s, e); updateValueR(s, mid, i, diff, 2 * ind + 1); updateValueR(mid + 1, e, i, diff, 2 * ind + 2); } } ////FOR MIN APLLICATION public int constructMinTree(int arr[], int s, int e, int ind) { if (s == e) { tree[ind] = arr[s]; return arr[s]; } int mid = getMid(s, e); tree[ind] = minVal(constructMinTree(arr, s, mid, ind * 2 + 1), constructMinTree(arr, mid + 1, e, ind * 2 + 2)); return tree[ind]; } public int getMin(int n,int l,int r) { if (l < 0 || r > n - 1 || l > r) { return -1; } return getMinR(0, n - 1, l, r, 0); } public void updateValueMin(int arr[],int s, int e, int index, int value, int node) { if (index < s || index > e) { return; } if (s == e) { arr[index] = value; tree[node] = value; } else { int mid = getMid(s, e); if (index >= s && index <= mid) { updateValueMin(arr,s, mid, index, value, 2 * node + 1); } else { updateValueMin(arr,mid + 1, e, index, value, 2 * node + 2); } tree[node] = Math.min(tree[2 * node + 1], tree[2 * node + 2]); } return; } public int getMinR(int s, int e, int l, int r, int index) { if (l <= s && r >= e) { return tree[index]; } if (e < l || s > r) { return Integer.MAX_VALUE; } int mid = getMid(s, e); return minVal(getMinR(s, mid, l, r, 2 * index + 1) , getMinR(mid + 1, e, l, r, 2 * index + 2)); } } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static void main(String args[]) throws Exception { new Thread(null, new Cf294(),"Main",1<<27).start(); } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int TESTCASES = in.nextInt(); while(TESTCASES-->0) { int n = in.nextInt(); int k = in.nextInt(); int arr[] = in.nextIntArray(n); int i,j; int res = -1; if(n==1) { w.println(-1); continue; } boolean flag = true; while(k-->0) { for(i=0;i<n-1;i++) { if(arr[i]<arr[i+1]) { arr[i]++; res = i; break; } else if(i==n-2) { flag = false; } } if(!flag) { break; } } if(flag) { w.println(res+1); } else { w.println(-1); } } w.flush(); w.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 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
3a648ea4e1febaa8de085dceec9bebfe
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 cfrcs { // // static boolean prime[] = new boolean[n+1]; // static void sieve(int n) // { // // 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 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(); } if(k>10000)System.out.println(-1); else { int a1[]=new int[n]; a1[n-1]=arr[n-1]; int count=0; for(int i=n-2;i>=0;i--) { if(arr[i]<a1[i+1]) { count=count+a1[i+1]-arr[i]; a1[i]=a1[i+1]; }else a1[i]=arr[i]; } // for(int i=0;i<n;i++) { // System.out.print(a1[i]+" "); // }System.out.println(); // if(count<k)System.out.println(-1); else { int flag=-1; for(int j=1;j<=10000;j++) { for(int i=0;i<n-1;i++) { if(arr[i]<arr[i+1]) { int a=Integer.MAX_VALUE; if(i>0) { a=arr[i-1]+1-arr[i]; } int b=arr[i+1]-arr[i]; int c=Math.min(a, b); k=k-c; if(k<=0) {flag=i+1;break;} arr[i]=arr[i]+c; break; } }if(flag!=-1)break; } // for(int i=0;i<n;i++) { // System.out.print(arr[i]+" "); // }System.out.println(); System.out.println(flag); } } } } }
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
b65bb25927a1c2bcc3d65192034f14a7
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.*; 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]; for(int i=0; i<n; i++){ arr[i]= sc.nextInt(); } int ans=0; for(int i=0; i<k; i++){ ans =0; for(int j=1; j<n; j++){ if(arr[j]>arr[j-1]){ arr[j-1]= arr[j-1]+1; ans= j; break; } } if(ans ==0){ break; } } if(ans==0){ System.out.println(-1); } else{ System.out.println(ans); } 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
590b77e8b6b69129742bec951c95d41b
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 practice; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { 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 void solve(int arr[],int k,int n) { int boulder=1; int index=0; boolean flag=false;; while(boulder<=k) { int pos=1; for(int i=1;i<n;i++) { if(arr[i]>=arr[i+1]) { pos++; } if(arr[i]<arr[i+1]) { arr[i]=arr[i]+1; flag=true; break; } } if(!flag) { System.out.println("-1"); return; } index=pos; boulder++; } if(!flag) { return; } if(index==n) { System.out.println("-1"); } else { System.out.println(index); } } public static void main(String[] args) throws IOException { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[n+1]; for(int i=0;i<n;i++) { arr[i+1]=sc.nextInt(); } // solve(arr,k,n); int boulder = 1; int index=0; boolean flag=false; while(boulder<=k) { flag = false; for(int i=1;i<n;i++) { index = i; if(arr[i]<arr[i+1]) { arr[i]=arr[i]+1; flag=true; break; } } if(flag == false) { System.out.println("-1"); break; } boulder++; } if(flag == true) { 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 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
73cc6226f3747da9ea80f20f3b235122
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 B { final static int MAXN = 100_005; final static long MOD = (long) 1e9 + 7; public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); outer: for(int tt =0 ;tt < t ; tt++) { int n = sc.nextInt() , k = sc.nextInt(); int a[] = sc.readArray(n); int max = a[n-1] ; int sum = 0; for(int i = n-2 ; i >= 0 ; i--) { if(max > a[i])sum += max - a[i]; else max = Math.max(max , a[i]); } if(k > sum)System.out.println(-1); else { int su = 0; while(su < k) { for(int i = 0 ; i < n - 1 ; i++) { if(a[i] < a[i+1]) { a[i]++; su++; if(su == k) { System.out.println(i+1); continue outer; } break; } } } } } } public 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); } public static long[] factorial; public static void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public static long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public static long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } public static long max(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public static int max(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public static long min(long... ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public static int min(int... ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public static long sum(long... ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public static long sum(int... ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } 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; } } }
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
dd1ccb288a6b224ded905e974b60be11
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 Solution1 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for (int i = 0; i<t; i ++){ int n = s.nextInt(); int k = s.nextInt(); int[] h = new int[n]; for (int j = 0; j<n; j++){ h[j] = s.nextInt(); } int k1 = 0; for (int j = 0; j<k; j++){ k1 = 0; while(k1 < n-1){ if (h[k1]<h[k1+1]){ h[k1]++; break; } k1++; if (k1 == n-1){ k1 = -2; break; } } if (k1==-2) break; } if (n==1) System.out.println(-1); else System.out.println(k1+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
15f5cd8cfc521ac40410480f2bb40154
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.awt.*; public class NewColony { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(f.readLine()); for (int z =0; z<t; z++) { StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); int[] h = new int[n+1]; int sum = 0; for (int i=1; i<=n; i++) { h[i] = Integer.parseInt(st.nextToken()); sum+=h[i]; } int ind = 0; while (k>0) { int index = 1; while (index<n) { if (h[index]<h[index+1]) { h[index]++; k--; ind = index; break; } index++; } if (index==n) { ind = -1; break; } } out.println(ind); } 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 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
f3bf77b247501bf78b78c3b22b16e02b
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 NewColony{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static boolean check(int arr[]){ int n= arr.length; for(int i = 0;i<n-1;i++){ if(arr[i+1]>arr[i]) return false; } return true; } static void solve() { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = sc.readIntArray(n); int mx[] = new int[n]; mx[n-1] = arr[n-1]; for(int i= n-2;i>=0;i--){ mx[i] = Math.max(arr[i],mx[i+1]); } int count = 0; for(int i = n-1;i>=0;i--){ count += (mx[i]-arr[i]); } if(count<k){ out.println(-1); return; } for(int j = 1;j<k;j++) { for (int i = 0; i < n - 1; i++) { if (arr[i + 1] > arr[i]) { arr[i]++; break; } } } for (int i = 0; i < n - 1; i++) { if (arr[i + 1] > arr[i]) { out.println(i+1); return; } } out.println(-1); } static class Pair implements Comparable<Pair>{ int num; int fre; Pair(int n,int f){ num = n; fre = f; } public int compareTo(Pair p){ return this.num - p.num; } } static void reverse(int arr[]){ int i = 0;int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ solve(); // solve2(); // solve3(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(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 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
402beac8ad8424839c9f5676d52ccec1
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 faltu; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.*; public class simple { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); long tt = s.nextLong(); while(tt-->0) { int n =s.nextInt(); long k=s.nextLong(); int[]a=new int[n]; for(int i=0;i<n;i++)a[i]=s.nextInt(); int idx=-1; while(k-->0) { boolean f=false; for(int i=0;i<n-1;i++) { if(a[i+1]>a[i]) { a[i]++; idx=i+1; f=true; break; } } if(!f) { idx=-1; break; } } 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 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
3f16eacd31f5df8ba49251820a58ccdc
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 practices.homework15; import java.io.*; import java.util.*; import static java.lang.Math.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! public class J { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(), k = in.nextInt(); int[] h = in.nextIntArrayPrimitive(n); if(n == 1 || k > (n - 1) * 99) out.println(-1); else { int ans = -1; for(int x = 1; x <= k; x++) { int i = 0; for(; i < n - 1; i++) { if(h[i] < h[i + 1]) { h[i]++; break; } } if(i == n - 1) { ans = -1; break; } if(x == k) ans = i + 1; } out.println(ans); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } } }
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
49b05fdda323dff5f833261ff5b4d531
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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long kk = sc.nextLong(); while(kk-->0) { int length = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[length]; for(int i = 0; i < length; i++) { arr[i] = sc.nextInt(); } int index = -1; while(k>0) { boolean flag = false; for(int i = 0; i < length-1; i++) { if(arr[i]<arr[i+1]) { index = i+1; k -= 1; arr[i] += 1; flag = true; break; } } if(!flag) { index=-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 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
87c7269c49fd62b26bf09cde8701573f
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 NewColony { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // System.in and System.out are input and output streams, respectively. InputStream inputStream = System.in; InputReader in = new InputReader(inputStream); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int k = in.nextInt(); int[] arr = in.readArray(n); boolean flag = false; int idx = 1; while (k > 0 && !flag) { flag = true; for (idx = 1; idx < arr.length; idx++) { if (arr[idx] > arr[idx - 1]) { k--; arr[idx - 1]++; flag = false; break; } } } if (k == 0) { out.write(idx + "\n"); } else { out.write(-1 + "\n"); } } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
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
eb19e70db87190a01f6e721934e37212
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; /** * @Author 王耀兴 * @Date 2021/5/2 17:29 * @Version 1.0 */ public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int m = sc.nextInt(); int k = sc.nextInt(); int[] nums = new int[m]; for (int j = 0; j < m; j++) { nums[j] = sc.nextInt(); } int d = 0; all: for (int j = 0; j < k; j++) { for (int l = 0; l < m; l++) { if (l == m - 1) { System.out.println(-1); break all; } if (nums[l] < nums[l + 1]) { nums[l]++; d = l + 1; break; } } if (j == k - 1) { System.out.println(d); } } } } }
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
1674bcaee614cecc063a55a669941892
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 E_NewColony { 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 i = 0; boolean isOver = false; while ( k > 0) { if (h[i] >= h[i+1]) { i++; if (i >= n-1) { isOver = true; break; } }else { h[i]++; k--; if (k!=0) { i = 0; } } } if (isOver) { System.out.println(-1); }else { 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 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
669946c2a5bafe9b4c50ab17474a7407
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 B1481 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); int K = in.nextInt(); int[] H = new int[N]; for (int n=0; n<N; n++) { H[n] = in.nextInt(); } int pos = -1; main: for (int k=0; k<K; k++) { pos = 0; while (true) { if (pos == N-1) { pos = -1; break main; // fall into waste } else if (H[pos] >= H[pos+1]) { pos++; } else { H[pos]++; break; } } } System.out.println((pos == -1) ? pos : (pos + 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
99b4f497d892d3431a25aa6a16915566
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.ref.SoftReference; import java.math.BigInteger; import java.util.*; //_________________________________________________________________ public class Main { static void multiply(int a[],int n,int num){ int carry=0; for (int i=0;i<n;i++){ int product=a[i]*num+carry; a[i]=product%10; carry=product/10; } while (carry>0){ a[n]=carry%10; carry/=10; n++; } } static void largeFactorial(int num){ int a[]= new int[1000]; a[0]=1; int n=1; for (int i=2;i<=num;i++){ multiply(a,n,i); } for (int i=n-1;i>=0;i--){ System.out.print(a[i]); } } public static void main(String[] args) throws IOException { // File file = new File("input.txt"); FastScanner sc = new FastScanner(); // Scanner sc= new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); outer:while (t-->=1) { int n = sc.nextInt(), k = sc.nextInt(); int a[] = sc.readArray(n); int x=-1; while (k>0){ k--; loop:for (int i=0;i<n;i++){ if (i==n-1){ System.out.println(-1); continue outer; } if (a[i]<a[i+1]){ a[i]++; x=i; break loop; } } } System.out.println(x+1); } out.flush(); } //------------------------------------if------------------------------------------------------------------------------------------------------------------------------------------------- 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 String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable<Pair> { long a; long b; public Pair(long a, long b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } // public int compareTo(Pair other) { // if (this.b == other.b) return 0; // if (this.b < other.b) return 1; // else return -1; // } //sort on the basis of first part only public int compareTo(Pair other) { if (this.a == other.a) return 0; else if (this.a > other.a) return 1; else return -1; } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i<s.length();i++){ fre[s.charAt(i)-'a']++; } return fre; } static int mod =(int)1e9; static long mod(long x) { return ((x % mod + mod) % mod); } static long add(long x, long y) { return mod(mod(x) + mod(y)); } static long mul(long x, long y) { return mod(mod(x) * mod(y)); } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); 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
5b775264d614087bd8a224bc9cc4ab02
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_NewColony { public static void main(String args[]) throws NumberFormatException, IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t =Integer.parseInt(br.readLine()); StringBuilder sb=new StringBuilder(); while(t-->0){ String[] str=br.readLine().split(" "); int n =Integer.parseInt(str[0]); int k =Integer.parseInt(str[1]); String[] harr=br.readLine().split(" "); int[] h=new int[n]; for(int i=0;i<n;i++) { h[i]=Integer.parseInt(harr[i]); } int i=0; while(k>0) { if(i==n-1) { break; } if(h[i]<h[i+1]) { k--;h[i]++; }else { i++; continue; } if(k<=0) { break; } if(i>0) { i--; } } i++; if(i==n) { sb.append(-1+"\n"); }else { sb.append(i+"\n"); } } System.out.println(sb); } }
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
0472c4c71b3cc9cb5a904d7af8127a5b
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.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(), "Main", 1 << 26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long k = sc.nextLong(); long h[] = new long[n]; for (int i = 0; i < n; i++) { h[i] = sc.nextLong(); } if (n == 1) { w.println(-1); continue; } int i = 0; for (long j = 1; j <= k; j++) { while (i < n && h[i] >= h[i + 1]) { i++; if (i == n - 1) break; } if (i == n - 1) break; h[i]++; if (j == k) break; if (i - 1 >= 0 && h[i - 1] < h[i]) i--; } if (i == n - 1) w.println(-1); else w.println(i + 1); } w.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 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
e814d78de7b3fb38898a9f97dc2c0e0f
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) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); 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(); } int l=-1; for(int i=0;i<k;i++) { l=-1; for(int j=1;j<n;j++) { if(a[j]<a[j+1]) { a[j]++; l=j; break; } } if(l==-1) break; } System.out.println(l); } } }
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
9089e06d60f661f5d1d4977593942653
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 { 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) throws java.lang.Exception { // try // { // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); // } // catch (Exception e) // { // System.err.println("Error"); // } FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-- > 0) { solve(sc); } } static void solve(FastReader sc) { int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } if(k > 10000) { System.out.println(-1); } else { int temp = -1; while(k-- > 0) { int j = 0; while(j < n - 1) { if(arr[j] <arr[j + 1]) break; j++; } if(j < n - 1) { arr[j]++; } temp = j; } if(temp == n - 1) { System.out.println(-1); } else { System.out.println(temp + 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 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
92395f1a0b4f6ebb51428246b4cbc793
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 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception{ int t = Integer.parseInt(in.readLine()); while(t--!=0) { String [] nk = in.readLine().split(" "); int n = Integer.parseInt(nk[0]), k = Integer.parseInt(nk[1]); int [] a = new int [n+1]; String [] s = in.readLine().split(" "); for(int i=1;i<=n;i++) a[i] = Integer.parseInt(s[i-1]); if(n==1) out.println(-1); else { boolean pd = true; //判断石头位置,出了n为false int kk = 0; for(int i=1;i<=k&&pd;i++) { for(int j=2;j<=n;j++) { if(a[j]>a[j-1]) { a[j-1]++; kk = j-1; break; } if(j==n) { kk = -1; pd = false; } } } out.println(kk); } } out.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 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
fdda24f5c7cb16f7f09608840f7c1bc7
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 ProblemB { public static void main(String[] argc) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); for(int i = 0; i < t; i++) { int n = sc.nextInt(); int k = sc.nextInt(); int[] H_Arr = new int[n]; int max_H = 0; for(int j = 0; j < n; j++) { H_Arr[j] = sc.nextInt(); max_H = Math.max(max_H, H_Arr[j]); } if(n*max_H < k) { sb.append(-1 + "\n"); continue; } // System.out.println(Arrays.toString(H_Arr)); int final_pos = -1; for(int idx = 0; idx < k; idx++) { int j = 1; for(j = 1; j < n; j++) { if(H_Arr[j-1] < H_Arr[j]) { H_Arr[j-1]++; break; } } if(idx == k -1) { if(j-1 == n -1) final_pos = -1; else final_pos = j; } // System.out.println(Arrays.toString(H_Arr)); } sb.append(final_pos + "\n"); } System.out.println(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
PASSED
a6b381a2ebb42d41b318bfba1bc0fb77
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 javax.tools.DocumentationTool.Location; // import jdk.internal.net.http.common.Pair; import java.lang.*; // import org.graalvm.compiler.graph.spi.Canonicalizable.Binary; import java.io.*; public class sol { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String giveMeFile) throws IOException { File file = new File("input.txt"); br = new BufferedReader(new FileReader(file)); } 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 boolean[] SES() { int n = 1000001; // SES Stands for SieveOfEratoSthenes boolean isPrime[] = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { for (int j = 2 * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } public static int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } public static long fastPower(int a, int b, int n) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = (res % n * a % n) % n; } a = (a % n * a % n) % n; b = b >> 1; // dividing by 2 } return res; } public static int first(int arr[], int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x) return mid; else if (x > arr[mid]) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(int arr[], int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr[mid + 1]) && arr[mid] == x) return mid; else if (x < arr[mid]) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static long sumPair(int arr[], int n) { long sum = 0; for (int i = n - 1; i >= 0; i--) sum += i * arr[i] - (n - 1 - i) * arr[i]; return sum; } static int SE(int N) { // Sieve of eratosthenes method int[] arr = new int[N + 1]; for (int i = 2; i * i <= N; i++) { if (arr[i] == 0) for (int j = 2 * i; j <= N; j += i) arr[j]++; arr[i] = 1; } int max = arr[0]; for (int i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; // Return maximum element in arr[] // return getMax(arr); } public static void main(String[] args) throws IOException { // ************** Providing input File // File file = new File("input.txt"); // BufferedReader br = new BufferedReader(new FileReader(file)); // *************** Input for Online Judges with Buffered Reader // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // ************* Input for Online Judges with Scanner class // Reading with Scanner class // Scanner sc = new Scanner(file); // sc.useDelimiter("\\Z"); // ***************** Printing Outout to output file. // PrintWriter printWriter = new PrintWriter(new BufferedWriter(new // FileWriter("output.txt"))); // printWriter.println("something here"); // printWriter.close(); --->> // only this close method will flush the output // from the buffer to the output file. FastReader read = new FastReader(); int t = read.nextInt(); while (t-- != 0) { int n = read.nextInt(); int k = read.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = read.nextInt(); int pos = 0; boolean out = false; while (k > 0) { for (int j = 0; j < n; j++) { if (j < n - 1) { if (a[j] >= a[j + 1]) { continue; } else { a[j] = a[j] + 1; pos = j; // System.out.println("this" + pos); break; } } else { // System.out.println("agya"); out = true; break; } } if (out) { break; } k--; } if (out) System.out.println("-1"); else System.out.println(pos + 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
2f4192192c5035177abfb0ab681cee16
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 java.util.TreeMap; import java.util.TreeSet; import java.util.function.IntBinaryOperator; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Set; import java.util.Stack; public class Test1 { final static int MOD = (int) 1e9+7; public static void main(String[] args) throws IOException { FastReader fr = new FastReader(1000000); int T = fr.nextInt(); while(T-->0) { solve(fr); } } static class Pair { int height, len; Pair(int height, int len){ this.height = height; this.len = len; } } static void solve(FastReader fr) throws IOException { int N = fr.nextInt(); int K = fr.nextInt(); int[] h = new int[N]; for(int i=0; i<N; i++) { h[i] = fr.nextInt(); } int j = 0; for(int i=0; i<K; i++) { for(; j<N; j++) { if(j==N-1) { System.out.println(-1); return; } if(h[j] < h[j+1]) { h[j]++; if(i!=K-1) j = 0; break; } } } System.out.println(j+1); } private static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private int lineLength = 1000; @SuppressWarnings("unused") public FastReader(int lineLength) { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; this.lineLength = lineLength; } @SuppressWarnings("unused") public FastReader(String file_name, int lineLength) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; this.lineLength = lineLength; } @SuppressWarnings("unused") public String nextLine() throws IOException { byte[] buf = new byte[lineLength]; // 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); } @SuppressWarnings("unused") 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; } @SuppressWarnings("unused") 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; } @SuppressWarnings("unused") 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++]; } @SuppressWarnings("unused") public void close() throws IOException { if (din == null) return; din.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 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
8370e4fc1f085d27aa3a281a5e235b2b
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 Party { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i<t; i++){ System.out.println(solve(sc)); } } public static int solve (Scanner sc){ int n = sc.nextInt(); int k = sc.nextInt(); int[] h = new int[n]; for (int i = 0; i<n; i++){ h[i] = sc.nextInt(); } for(int i = 0; i<k; i++){ for (int j = 0; j<n; j++){ if (j<n-1 && h[j]>=h[j+1]){ continue; } else if (j==n-1){ return -1; } else{ h[j]++; if (i==k-1) return j+1; break; } } } return -1; } public static int max(int a, int b, int c){ return Math.max(Math.max(a, b), c); } }
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
12b02dcc5361ef7b3bf0170610bce819
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(); StringBuilder sb=new StringBuilder(); for(int t=0;t<T;t++){ int n=sc.nextInt(); int k=sc.nextInt(); long [] h=new long [n]; for(int i=0;i<n;i++){ h[i]=sc.nextLong(); } int ans=-1; while(k>0){ int stop=-1; for(int i=0;i<n-1;i++){ if(h[i]<h[i+1]){ h[i]++; stop=i+1; break; } } ans=stop; if(stop==-1) break; k--; } sb.append(ans+"\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
PASSED
ff75111bd16def91fd9b553a8c61471a
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Round699Div2_2 { public static void main(String[] args) throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); int t= Integer.parseInt(br.readLine()); while (t-->0){ StringTokenizer st= new StringTokenizer(br.readLine()); int n= Integer.parseInt(st.nextToken()); int k= Integer.parseInt(st.nextToken()); st= new StringTokenizer(br.readLine()); int h[]= new int[n]; for (int i=0;i<h.length;i++){ h[i]=Integer.parseInt(st.nextToken()); } int smallestFound=-1; boolean isSmallFound=false; while (k>=0){ isSmallFound=false; for (int i=1;i<h.length;i++){ if (h[i]>h[i-1]){ k--; h[i-1]++; smallestFound=i-1; isSmallFound=true; break; } } if (k==0){ break; } if (k>0 && !isSmallFound){ break; } } if (k>0){ System.out.println(-1); }else if(k==0 && !isSmallFound){ System.out.println(1); }else { System.out.println(smallestFound+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
a4c46a1b9b4ebcf119e454b22dbbc40e
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 Solution { public static void main(String... args){ Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t-->0){ String[] line = sc.nextLine().trim().split(" "); int n = Integer.parseInt(line[0]); int p = Integer.parseInt(line[1]); int[] mnt = new int[n]; line = sc.nextLine().trim().split(" "); for(int i=0;i<n;i++){ mnt[i] = Integer.parseInt(line[i]); } int i=0; while(p>0){ i=0; while(i<mnt.length-1 && mnt[i]>=mnt[i+1]){ i++; } if(i==mnt.length-1){ break; } mnt[i]++; p--; } if(p>0) System.out.println(-1); else 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 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
576442dfa7a0ed02e90bdbc0ae879f6d
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.util.Scanner; import java.io.*; import javax.lang.model.util.ElementScanner6; import static java.lang.System.out; import java.util.Stack; import java.util.Queue; import java.util.LinkedList; public class B1481M2 { static int mod=(int)(1e9+7); static long MOD=(long)(1e9+7); public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int tc=1; tc=in.nextInt(); tcloop: while(tc-->0) { int n=in.nextInt(); int k=in.nextInt(); int arr[]=in.readIntArray(n); if(k>(int)1e4) { pr.println(-1); continue; } int last=0; for(int i=1;i<=k;i++) { for(int j=0;j<n-1;j++) { last=j; if(arr[j]<arr[j+1]) { arr[j]++; break; } last=j+1; } if(last==n-1) { pr.println(-1); continue tcloop; } } pr.println(last+1); } pr.flush(); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static 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 FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] 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; } 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 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
89631946572b485f72c78d1b18867d61
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 ProblemB { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int h=sc.nextInt(); int b=sc.nextInt(); int a[]=new int[h]; for(int i=0;i<h;i++) { a[i]=sc.nextInt(); } int z[]=new int[h]; int k=0,d=0; while(true) { k=-1; for(int i=0;i<h-1;i++) { if(a[i]<a[i+1]) { k=i; break; }} if(k==-1) { d=-1; break; } a[k]++; if(--b==0) { d=k+1; break; } } System.out.println(d); // while(b-->0) { // for(int i=0;i<h-1;i++) { // if(a[i]>=a[i+1]) { // k++; // }} // } }}
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
fdab266caca10337cdaea2bc06b6f82f
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.HashMap; import java.util.HashSet; import java.util.StringTokenizer; public class NewColony { public static int getFirstAfter(int[] arr){ for(int i=1;i<arr.length;i++){ if(arr[i] > arr[i-1]){ return i-1; } } return -1; } public static void main(String[] args){ MyScanner sc = new MyScanner(); 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 res = -1; for(int i=0;i<n;i++){ while(getFirstAfter(arr) != -1 && k > 0){ int idx = getFirstAfter(arr); arr[idx]++; k--; res = idx+1; } } if(k != 0){ System.out.println(-1); continue; } System.out.println(res); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\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
010c68972aa9e837aaeabd3f4818a6df
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
// Main Code at the Bottom import java.util.*; import java.io.*; public class Main{ //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; //env=true; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=(long)1e9+7; //debug static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Global variables and functions //Main function(The main code starts from here) public static void main (String[] args) throws java.lang.Exception { int test=1; test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(),k=sc.nextInt(),a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); int ans=-1; while(k>0) { int f=0; for(int i=0;i<n-1;i++) { if(a[i]<a[i+1]) { f=1; a[i]++; k--; if(k<=0) ans=i+1; break; } } if(f==0) k=0; } out.println(ans); } out.flush(); 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 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
1b6f95854711082b0c5102ded2a3529d
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 A1{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; static final long mod=1000000007; public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int k=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); if(n==1){ bw.write("-1\n"); return ; } int ans=0; for(int i=1;i<n;){ int j=i-1; if(ar[j]<ar[i]){ for(;j>=0;j--){ ar[j]++;k--; if(k==0){ bw.write((j+1)+"\n"); return ; } if(j>0 && ar[j]<=ar[j-1]){ break; } } } else i++; } bw.write("-1\n"); } /** Main Method**/ public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } /** Helpers**/ private static char[] getStr()throws IOException{ return br.readLine().toCharArray(); } private static int Gcd(int a,int b){ if(b==0) return a; return Gcd(b,a%b); } private static long Gcd(long a,long b){ if(b==0) return a; return Gcd(b,a%b); } private static int[] getArrIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static Integer[] getArrInP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Integer[] ar=new Integer[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static long[] getArrLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); long[] ar=new long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static List<Integer> getListIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken())); return al; } private static List<Long> getListLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken())); return al; } private static long pow_mod(long a,long b) { long result=1; while(b!=0){ if((b&1)!=0) result=(result*a)%mod; a=(a*a)%mod; b>>=1; } return result; } private static int pow_mod(int a,int b) { int result=1; int mod1=(int)mod; while(b!=0){ if((b&1)!=0) result=(result*a)%mod1; a=(a*a)%mod1; b>>=1; } return result; } private static int lower_bound(int a[],int x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r+1; } private static long lower_bound(long a[],long x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r+1; } private static int upper_bound(int a[],int x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static long upper_bound(long a[],long x){ int l=-1,r=a.length; while(l+1<r){ int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static boolean Sqrt(int x){ int a=(int)Math.sqrt(x); return a*a==x; } private static boolean Sqrt(long x){ long a=(long)Math.sqrt(x); return a*a==x; } }
Java
["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 17
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
8f12cba7866799cf6d060b39b6e8c17e
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.*; public class A1471{ public static void main (String [] args){ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); int k=0; while(k<t) { int a = scn.nextInt(); int b = scn.nextInt(); String str = scn.next(); int x=0; int y =0; for(int i=0;i<str.length();i++) { char c = str.charAt(i); if(c =='U') { if(y<b) { y++; } else { continue; } } else if(c=='R') { if(x<a) { x++; } else { continue; } } else if(c=='D') { if(y>b) { y--; } else { continue; } } else if(c=='L') { if(x>a) { x--; } else { continue; } } } if(x == a && y == b) { System.out.println("YES"); // System.out.println(x); // System.out.println(y); } else { System.out.println("NO"); // System.out.println(x); // System.out.println(y); } k++; } return; } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
eb92be2d4d9b9711186f745ea47251d9
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.*; import java.lang.Math; public class SpaceNavigation { public static void main(String[] args) { Scanner sk = new Scanner(System.in); int n = sk.nextInt(); int u = 0; int d = 0; int r = 0; int l = 0; for(int i = 0; i < n; i++){ //planet loc also the dist in x and y int pX = sk.nextInt(); int pY = sk.nextInt(); //dir input String dir = sk.next(); for(int j = 0; j < dir.length(); j++){ if(dir.charAt(j) == 'U') u++; if(dir.charAt(j) == 'D') d++; if(dir.charAt(j) == 'R') r++; if(dir.charAt(j) == 'L') l++; } //possibilites //planet x, y // x, -y // -x, y // -x, -y if (pX>=0 && pY>=0) { if (r>=Math.abs(pX) && u>=Math.abs(pY)) System.out.println("YES"); else { System.out.println("NO"); } } else if (pX>=0 && pY<=0) { if (r>=Math.abs(pX) && d>=Math.abs(pY)) System.out.println("YES"); else { System.out.println("NO"); } } else if (pX<=0 && pY>=0) { if (l>=Math.abs(pX) && u>=Math.abs(pY)) System.out.println("YES"); else { System.out.println("NO"); } } else if (pX<=0 && pY<=0) { if (l>=Math.abs(pX) && d>=Math.abs(pY)) System.out.println("YES"); else { System.out.println("NO"); } } u = 0; d = 0; r = 0; l = 0; } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
9216b4c06115c7ca4cc49b798a1f9177
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int testCasesNum = in.nextInt(); boolean px = false; boolean py = false; for (int i = 0; i < testCasesNum; i++) { int x = in.nextInt(); int y = in.nextInt(); String s = in.next(); HashMap<Character, Integer> charCountMap = countChar(s); if (x == 0) px = true; if (y == 0) py = true; if (x > 0) { if (charCountMap.containsKey('R') && charCountMap.get('R') >= x) px = true; } if (x < 0) { if (charCountMap.containsKey('L') && charCountMap.get('L') >= Math.abs(x)) px = true; } if (y > 0) { if (charCountMap.containsKey('U') && charCountMap.get('U') >= y) py = true; } if (y < 0) { if (charCountMap.containsKey('D') && charCountMap.get('D') >= Math.abs(y)) py = true; } if (px && py) System.out.println("YES"); else System.out.println("No"); px = false; py = false; } } public static HashMap<Character, Integer> countChar(String s) { HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>(); char[] strArray = s.toCharArray(); for (char c : strArray) { if (charCountMap.containsKey(c)) { charCountMap.put(c, charCountMap.get(c) + 1); } else { charCountMap.put(c, 1); } } return charCountMap; } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
d3f8282da746d490a8a15182221da59c
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A_Space_Navigation { static long x, y, U, D, R, L; static char lx, ly; static BufferedReader reader; static StringTokenizer tok; public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(reader.readLine()); for (int i = 0; i < tc; i++) { tok = new StringTokenizer(reader.readLine()); x = Long.parseLong(tok.nextToken()); y = Long.parseLong(tok.nextToken()); Opreations(); System.out.println((getPointN() && getPointM()) ? "YES" : "NO"); } } static void Opreations() throws IOException { lx = x >= 0 ? 'R' : 'L'; ly = y >= 0 ? 'U' : 'D'; String s = reader.readLine(); U = String.join("", s.split("[D,R,L]")).length(); D = String.join("", s.split("[U,R,L]")).length() * -1; R = String.join("", s.split("[U,D,L]")).length(); L = String.join("", s.split("[U,R,D]")).length() * -1; } static boolean getPointN() { switch (lx) { case 'R': if (x <= R) { return true; } break; case 'L': if (x >= L) { //هنا الاصغر هي الاكبر return true; } } return false; } static boolean getPointM() { switch (ly) { case 'U': if (y <= U) { return true; } break; case 'D': if (y >= D) { //هنا الاصغر هي الاكبر return true; } } return false; } } /* 15 1 0 L 1 1 L 0 1 L 1 -1 L 0 -1 L -1 -1 L -1 0 L NO NO YES NO YES YES YES NO NO NO NO NO NO YES */
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
fef2ad325695c8871e9abcba942def57
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A_Space_Navigation { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok; int x = Integer.parseInt(reader.readLine()); for (int i = 0; i < x; i++) { tok = new StringTokenizer(reader.readLine()); long n, m, U, D, R, L; n = Long.parseLong(tok.nextToken()); m = Long.parseLong(tok.nextToken()); char ln = n >= 0 ? 'R' : 'L'; char lm = m >= 0 ? 'U' : 'D'; pair plane = new pair(n, m, ln, lm); String s = reader.readLine(); U = String.join("", s.split("[D,R,L]")).length(); D = String.join("", s.split("[U,R,L]")).length() * -1; R = String.join("", s.split("[U,D,L]")).length(); L = String.join("", s.split("[U,R,D]")).length() * -1; boolean flagN = false, flagM = false; switch (plane.ln) { case 'R': if (plane.x <= R) { flagN = true; } break; case 'L': if (plane.x >= L) { //هنا الاصغر هي الاكبر flagN = true; } break; } switch (plane.lm) { case 'U': if (plane.y <= U) { flagM = true; } break; case 'D': if (plane.y >= D) { //هنا الاصغر هي الاكبر flagM = true; } break; } System.out.println((flagM && flagN) ? "YES" : "NO"); } } static class pair { long x, y; char ln, lm; public pair(long x, long y, char ln, char lm) { this.x = x; this.y = y; this.ln = ln; this.lm = lm; } } } /* 15 1 0 L 1 1 L 0 1 L 1 -1 L 0 -1 L -1 -1 L -1 0 L NO NO YES NO YES YES YES NO NO NO NO NO NO YES */
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
b9701e0b31bf23e2c03ac6ed0e963195
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.Scanner; public class A_Space_Navigation { // The first line contains a single integer t (1≤t≤1000) — the number of test cases. // // Each test case consists of two lines. The first line in each test case contains two integers px and py (−105≤px,py≤105; (px,py)≠(0,0)) — the coordinates of Planetforces (px,py). // // The second line contains the string s (1≤|s|≤105: |s| is the length of string s) — the list of orders. // // It is guaranteed that the sum of |s| over all test cases does not exceed 105. private static void planetcodeforces(int px, int py, String orders) { char x,y; if(px>=0 && py>=0) { x='R'; y='U'; // ++ } else if(px>=0 && py<=0) { x='R'; y='D'; // +- } else if(px<=0 && py>=0) { x='L'; y='U'; // -+ } else { // -- x='L'; y='D'; } int apx=Math.abs(px); int apy=Math.abs(py); // System.out.println("apx="+apx+"|apy"+apy); // System.out.println("x="+x+"|y"+y); int countx=0; int county=0; for(int i=0;i<orders.length();i++) { if(orders.charAt(i)==x) { countx+=1; } if(orders.charAt(i)==y) { county+=1; } } if((countx>=apx) && (county>=apy)) { System.out.println("YES"); } else { System.out.println("NO"); } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); for(int i=0;i<t;i++) { int px=scan.nextInt(); int py=scan.nextInt(); scan.nextLine(); String orders=scan.nextLine(); planetcodeforces(px,py,orders); } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
1d96e052e13aa732c07466d12acaec34
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
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 x = sc.nextInt(); int y = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char a[]=new char[4]; // 0 - left // 1 - right // 2 - up // 3 - down for(int i=0;i<s.length();i++){ if(s.charAt(i)=='L') a[0]++; if(s.charAt(i)=='R') a[1]++; if(s.charAt(i)=='U') a[2]++; if(s.charAt(i)=='D') a[3]++; } if(x<=0 && y<=0){ if(a[0]>=(-1*x) && a[3]>=(y*-1)) System.out.println("yes"); else System.out.println("no"); } else if(x<=0 && y>= 0){ if(a[0]>=(-1*x) && a[2]>=y) System.out.println("yes"); else System.out.println("no"); } else if(x>=0 && y<=0){ if(a[1]>=x && a[3]>=(-1*y)) System.out.println("yes"); else System.out.println("no"); } else if(x>=0 && y>=0){ if(a[1]>=x && a[2]>=y) System.out.println("yes"); else System.out.println("no"); } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
68748e7ca2b6eb3d80bec1afb0ef4b42
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
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 x = sc.nextInt(); int y = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char a[]=new char[4]; // 0 - left // 1 - right // 2 - up // 3 - down for(int i=0;i<s.length();i++){ if(s.charAt(i)=='L') a[0]++; if(s.charAt(i)=='R') a[1]++; if(s.charAt(i)=='U') a[2]++; if(s.charAt(i)=='D') a[3]++; } if(x<=0 && y<= 0){ if(a[0]>=(-1*x) && a[3]>=(y*-1)) System.out.println("yes"); else System.out.println("no"); } if(x<=0 && y> 0){ if(a[0]>=(-1*x) && a[2]>=y) System.out.println("yes"); else System.out.println("no"); } if(x>0 && y<= 0){ if(a[1]>=x && a[3]>=(-1*y)) System.out.println("yes"); else System.out.println("no"); } if(x>0 && y>0){ if(a[1]>=x && a[2]>=y) System.out.println("yes"); else System.out.println("no"); } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
55560db18f67132eb7a8c520dddb39e6
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class 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 a=0,b=0,c=0,d=0; int x=sc.nextInt(); int y=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); if(x>0 && y>0){ for(int i=0;i<s.length();i++){ char z=s.charAt(i); if(z=='R') { a+=1; } else if(s.charAt(i)=='U') b+=1; } if(a>=x && b>=y) System.out.println("Yes"); else System.out.println("No"); } if(x>0 && y<=0){ for(int i=0;i<s.length();i++){ if(s.charAt(i)=='R') { a+=1; } else if(s.charAt(i)=='D') c+=1; } if(a>=x && c>=Math.abs(y)) System.out.println("Yes"); else System.out.println("No"); } if(x<=0 && y>0){ for(int i=0;i<s.length();i++){ if(s.charAt(i)=='L') { d+=1; } else if(s.charAt(i)=='U') b+=1; } if(d>=Math.abs(x) && b>=y) System.out.println("Yes"); else System.out.println("No"); } if(x<=0 && y<=0){ for(int i=0;i<s.length();i++){ if(s.charAt(i)=='L') { d+=1; } else if(s.charAt(i)=='D') c+=1; } if(d>=Math.abs(x) && c>=Math.abs(y)) System.out.println("Yes"); else System.out.println("No"); } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
7a3f30ca48aa8279c3e2ec406d70e2ac
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Code4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int x = sc.nextInt(); int y = sc.nextInt(); boolean canx=false; boolean cany=false; StringBuilder sb = new StringBuilder(sc.next()); HashMap<Character, Integer> hm = new HashMap<>(); hm.put('U',0); hm.put('D',0); hm.put('L',0); hm.put('R',0); for (int i = 0; i < sb.length(); i++) { int count; count= hm.get(sb.charAt(i)); hm.put(sb.charAt(i), count + 1); } if(x>=0){ if (hm.get('R') != null ) { if (hm.get('R')>=x) { // System.out.println(hm.get('R')); canx = true; } } } else { if (hm.get('L') != null ) { if (hm.get('L')>= Math.abs(x)) canx=true; } } if(y>=0){ if (hm.get('U') !=null ) { // System.out.println(hm.get('U') +y); if (hm.get('U')>=y) { // System.out.println(hm.get('U')); cany = true; } } } else { if (hm.get('D') !=null) { if (hm.get('D')>= Math.abs(y)) cany=true; } } // System.out.println(hm); if(canx && cany) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
7a5139e6d8ea4169cf13d09331bb626a
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.*; public class Solution { public static void main(String []args) { int t,j; Scanner in=new Scanner(System.in); t=in.nextInt(); while(t-->0) { int px,py; px=in.nextInt(); py=in.nextInt(); String s; in.nextLine(); s=in.next(); int r,l,u,d,i; if(px>=0&&py>=0) { r=u=0; for(i=0;i<s.length();i++) { if(s.charAt(i)=='R') r++; if(s.charAt(i)=='U') u++; } if(r>=px && u>=py) { System.out.println("YES"); } else { System.out.println("NO"); } } else if(px>=0&&py<=0) { r=d=0; for(i=0;i<s.length();i++) { if(s.charAt(i)=='R') r++; if(s.charAt(i)=='D') d++; } if(r>=px && d>=-py) { System.out.println("YES"); } else { System.out.println("NO"); } } else if(px<=0&&py>=0) { l=u=0; for(i=0;i<s.length();i++) { if(s.charAt(i)=='L') l++; if(s.charAt(i)=='U') u++; } if(l>=-px && u>=py) { System.out.println("YES"); } else { System.out.println("NO"); } } else { l=d=0; for(i=0;i<s.length();i++) { if(s.charAt(i)=='L') l++; if(s.charAt(i)=='D') d++; } if(l>=-px && d>=-py) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
f32e6062dffbc64a33e9422ad6db0d4a
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
// 05-Feb-2021 import java.util.*; import java.io.*; public class B { static class FastReader { BufferedReader br; StringTokenizer st; private 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()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayOne(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; 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; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder str = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int x = s.nextInt(), y = s.nextInt(); int L = 0, R= 0 , D = 0, U = 0; String sub = s.nextLine(); for(int i = 0;i < sub.length(); i++) { char cur = sub.charAt(i); if(cur == 'L') { L++; } if(cur == 'R') { R++; } if(cur == 'D') { D++; } if(cur == 'U') { U++; } } if(x >= 0 && y >= 0 && R >= x && U >= y) { str.append("YES\n"); continue; } if(x <= 0 && y <= 0 && L >= -x && D >= -y) { str.append("YES\n"); continue; } if(x <= 0 && y >= 0 && L >= -x && U >= y) { str.append("YES\n"); continue; } if(x >= 0 && y <= 0 && R >= x && D >= -y) { str.append("YES\n"); continue; } str.append("NO\n"); } System.out.println(str); } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
4d6831bf2c59d6a83f5c0d8ccf9ee9f0
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.io.*; import java.util.*; public class spaceship { // instance variables - replace the example below with your own public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int z=1;z<=t;z++){ int p=sc.nextInt(); int q=sc.nextInt(); int a[]=new int[4]; String s=sc.next(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); if(ch=='U')a[0]++; else if(ch=='D')a[1]++; else if(ch=='R')a[2]++; else a[3]++; } Boolean b; if(p<0 && q<0)b=(a[3]>=(-1)*p && a[1]>=(-1)*q); else if(p<0 && q>=0)b=(a[3]>=(-1)*p && a[0]>=q); else if(p>=0 && q>=0)b=(a[2]>=p && a[0]>=q); else b=(a[2]>=p && a[1]>=(-1)*q); if(b)System.out.println("YES"); else System.out.println("NO"); } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
5d9ec19d21e72edb9d1cbec379773ef0
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Java_02 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int x = sc.nextInt(); int y = sc.nextInt(); String s = sc.next(); int U = 0, D = 0, L = 0, R = 0; for(int i = 0; i<s.length(); i++){ if(s.charAt(i)=='U') U++; if(s.charAt(i)=='D') D--; if(s.charAt(i)=='L') L--; if(s.charAt(i)=='R') R++; } if(x < 0 && y < 0){ if(L<=x && D<=y) System.out.println("YES"); else System.out.println("NO"); } if(x < 0 && y > 0){ if(L<=x && U>=y) System.out.println("YES"); else System.out.println("NO"); } if(x > 0 && y < 0){ if(R>=x && D<=y) System.out.println("YES"); else System.out.println("NO"); } if(x > 0 && y > 0){ if(R>=x && U>=y) System.out.println("YES"); else System.out.println("NO"); } if(x==0){ if(y>0){ if(U>=y) System.out.println("YES"); else System.out.println("NO"); } if(y<0){ if(D<=y) System.out.println("YES"); else System.out.println("NO"); } } if(y==0){ if(x>0){ if(R>=x) System.out.println("YES"); else System.out.println("NO"); } if(x<0){ if(L<=x) System.out.println("YES"); else System.out.println("NO"); } } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output
PASSED
c7357a8ebed7d080717dc542f1f047ff
train_107.jsonl
1612535700
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; /** * @link https://codeforces.com/contest/1481/problem/A */ public class SpaceNavigation_ { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] cd = br.readLine().split("\\s"); int x = Integer.parseInt(cd[0]); int y = Integer.parseInt(cd[1]); String directions = br.readLine(); int r = 0, u = 0, d = 0, l = 0; for (int i = 0; i < directions.length(); i++) { char di = directions.charAt(i); if (di == 'R') { r++; } else if (di == 'U') { u++; } else if (di == 'D') { d++; } else if (di == 'L') { l++; } } if (x > 0 && r >= Math.abs(x)) x = 0; if (x < 0 && l >= Math.abs(x)) x = 0; if (y > 0 && u >= Math.abs(y)) y = 0; if (y < 0 && d >= Math.abs(y)) y = 0; if (x == 0 && y == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"]
2 seconds
["YES\nYES\nYES\nNO\nYES\nNO"]
NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to "UR".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces.
Java 8
standard input
[ "greedy", "strings" ]
65a64ea63153fec4d660bad1287169d3
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \le p_x, p_y \le 10^5$$$; $$$(p_x, p_y) \neq (0, 0)$$$) — the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$) — the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case, print "YES" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
standard output