exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
sequencelengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
08d4480f3f22e066928dfde5e2612a79
train_000.jsonl
1398612600
Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x × y matrix c which has the following properties: the upper half of matrix c (rows with numbers from 1 to x) exactly matches b; the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Scanner; public class B { public static byte[][] a; public static int find(int n, int m) { boolean res = true; for (int i = 0; (i < n/2)&&(res == true) ; i++) for (int j = 0; j < m; j++) if(a[i][j] != a[n-i-1][j]) { res = false; break; } if(res == false || n == 1) return n; else n = n/2; if(n%2 == 1) return n; return find(n,m); } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); a = new byte[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = in.nextByte(); } } int p = n; if(n%2 != 1) n = find(n,m); System.out.println(n); } }
Java
["4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1", "3 3\n0 0 0\n0 0 0\n0 0 0", "8 1\n0\n1\n1\n0\n0\n1\n1\n0"]
1 second
["2", "3", "2"]
NoteIn the first test sample the answer is a 2 × 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001
Java 8
standard input
[ "implementation" ]
90125e9d42c6dcb0bf3b2b5e4d0f845e
The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.
1,300
In the single line, print the answer to the problem — the minimum number of rows of matrix b.
standard output
PASSED
4d82c86682b4c1452f2516d42d0e5e38
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.io.*; import java.util.*; public class D { static int [] w ; static ArrayList<Integer> primes; static int[] isComposite; static int [] phi ; static void sieve(int N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) if (isComposite[i] == 0) { primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) isComposite[j] = 1; } } static int tower(int mod , int l , int r ) { if(phi[mod] == 1 || l == r) return w[l] ; int x = tower(mod + 1, l + 1, r ) ; return modPow(w[l] , x , phi[mod]) ; // a ^ (phi(m) + x mod phi(m)) mod m ; } static int modPow(int a, int e, long mod) { long ans = 1; while (e > 0) { if ((e & 1) != 0) { if(ans >= mod / a) ans = (ans * a) % mod + mod ; else ans = ans * a ; } if(a >= mod / a) a = (int)((1l*a * a) % mod + mod) ; else a = a * a ; e >>= 1; } return (int)ans ; } static int phi(int N) { int ans = N; int idx = 0 ; long p = primes.get(0); while(p * p <= N) { if(N % p == 0) ans -= ans / p; while(N % p == 0) N /= p; p = primes.get(++idx); } if(N != 1) ans -= ans / N; return ans; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); sieve((int)1e7); int n = sc.nextInt() , m = sc.nextInt(); w = new int [n + 1] ; phi = new int [(int)1e6] ; phi[0] = m ; for(int i = 1 ; i < 1e6 ; i++)phi[i] = phi(phi[i - 1]) ; for(int i = 1 ; i <= n ;i++)w[i] = sc.nextInt(); int q = sc.nextInt(); while(q-->0) { int l = sc.nextInt() , r = sc.nextInt() ; if(l == r) out.println(w[l] % m); else out.println(tower( 0 , l , r ) % m); } out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } } static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
5d05cdbeed56b74e7cfa262cf04c1cf6
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.util.*; import java.io.*; public class Main { int [] phi , w; int phi(int n) { int ans = n ; for(int i = 2 ; 1L * i * i <= n ; i++) { if(n % i == 0 ) ans -= ans / i ; while(n % i == 0 ) n /= i ; } if(n != 1) ans -= ans / n ; return ans ; } int modPow(long a , int e , int mod) { long ans = 1 ; while(e > 0) { if((e & 1) != 0) { ans *= a ; if(ans >= mod) ans = ans % mod + mod ; } a *= a ; if(a >= mod) a = a % mod + mod ; e >>= 1 ; } return (int)ans ; } int tower(int l , int r , int mod) { if(phi[mod] == 1 || l == r)return w[r] ; return modPow(w[l] , tower(l + 1 , r , mod + 1) , phi[mod]) ; } void main() throws Exception { Scanner sc = new Scanner(System.in) ; PrintWriter out = new PrintWriter(System.out) ; int n = sc.nextInt() , m = sc.nextInt(); phi = new int[30] ; phi[0] = m ; for(int i = 1 ; i < 30 ; i++) phi[i] = phi(phi[i - 1]) ; w = new int [n] ; for(int i = 0 ; i < n ; i++) w[i] = sc.nextInt() ; int q = sc.nextInt(); while (q-->0) out.println(tower(sc.nextInt() - 1 , sc.nextInt() - 1, 0) % m); out.flush(); out.close(); } class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String path) throws Exception { br = new BufferedReader(new FileReader(path)) ; } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next());} } public static void main (String [] args) throws Exception {(new Main()).main();} }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
3b2a6fdf2b8cc7f221bd0ab8b284bef5
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.io.*; import java.util.*; public class D { static int [] w ; static ArrayList<Integer> primes; static int[] isComposite; static int [] phi ; static void sieve(int N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) if (isComposite[i] == 0) { primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) isComposite[j] = 1; } } static int tower(int mod , int l , int r ) { if(phi[mod] == 1 || l == r) return w[l] ; int x = tower(mod + 1, l + 1, r ) ; return modPow(w[l] , x , phi[mod]) ; // a ^ (phi(m) + x mod phi(m)) mod m ; } static int modPow(int a, int e, long mod) { long ans = 1; while (e > 0) { if ((e & 1) != 0) { if(ans >= mod / a) ans = (ans * a) % mod + mod ; else ans = ans * a ; } if(a >= mod / a) a = (int)((1l*a * a) % mod + mod) ; else a = a * a ; e >>= 1; } return (int)ans ; } static int phi(int N) { int ans = N; int idx = 0 ; long p = primes.get(0); while(p * p <= N) { if(N % p == 0) ans -= ans / p; while(N % p == 0) N /= p; p = primes.get(++idx); } if(N != 1) ans -= ans / N; return ans; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); sieve((int)1e7); int n = sc.nextInt() , m = sc.nextInt(); w = new int [n + 1] ; phi = new int [(int)1e6] ; phi[0] = m ; for(int i = 1 ; i < 1e6 ; i++)phi[i] = phi(phi[i - 1]) ; for(int i = 1 ; i <= n ;i++)w[i] = sc.nextInt(); int q = sc.nextInt(); while(q-->0) { int l = sc.nextInt() , r = sc.nextInt() ; if(l == r) out.println(w[l] % m); else out.println(tower( 0 , l , r ) % m); } out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } } static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
b55d1c1d5debea19191b7c449c33e6ac
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.util.*; import java.io.IOException; public class Test { int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } long m; final int N = 100_010; int[] power = new int[N]; long[] phi = new long[64]; long getPhi(long v) { long ans = v; for (int i = 2; i*i <= v; i++) { if (v % i == 0) { ans *= (i-1); ans /= i; while (v % i == 0) v /= i; } } if (v != 1) ans = ans*(v-1)/v; return ans; } long mod(long v, long m) { if (v < m) return v; return (v % m) + m; } long pm(long b, long e, long m) { long ans = 1, v = b; while (e != 0) { if ((e & 1) != 0) ans = mod(ans * v, m); v = mod(v*v, m); e >>>= 1; } return ans; } long solve(int l, int r, int d) { if (l > r || phi[d] == 1) return 1; long e = solve(l + 1, r, d + 1); return pm(power[l], e, phi[d]); } void start() { int n = readInt(), m = readInt(); for (int i = 1; i <= n; i++) power[i] = readInt(); phi[0] = m; for (int i = 0; phi[i] != 1; i++) phi[i+1] = getPhi(phi[i]); int q = readInt(); while (q-- > 0) { int l = readInt(), r = readInt(); System.out.println((l == r ? power[l] : solve(l, r, 0)) % m); } } public static void main(String[] args) { new Test().start(); } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
5e4a51edda4147a8cfa9131810a303d0
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.util.*; import java.io.IOException; public class Test { int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } long m; final int N = 100_010; int[] power = new int[N]; long[] phi = new long[64]; long getPhi(long v) { long ans = v; for (int i = 2; i*i <= v; i++) { if (v % i == 0) { ans *= (i-1); ans /= i; while (v % i == 0) v /= i; } } if (v != 1) ans = ans*(v-1)/v; return ans; } long mod(long v, long m) { if (v < m) return v; return (v % m) + m; } long solve(int l, int r, int d) { if (l > r || phi[d] == 1) return 1; long e = solve(l + 1, r, d + 1); long ans = 1, v = power[l]; while (e != 0) { if ((e & 1) != 0) ans = mod(ans * v, phi[d]); v = mod(v*v, phi[d]); e >>>= 1; } return ans; } void start() { int n = readInt(), m = readInt(); for (int i = 1; i <= n; i++) power[i] = readInt(); phi[0] = m; for (int i = 0; phi[i] != 1; i++) phi[i+1] = getPhi(phi[i]); int q = readInt(); while (q-- > 0) { int l = readInt(), r = readInt(); System.out.println((l == r ? power[l] : solve(l, r, 0)) % m); } } public static void main(String[] args) { new Test().start(); } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
4d42b2ebb6a5424c78a6d8e9b2c2e29b
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.util.*; import java.io.IOException; public class Test { int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } long m; final int N = 100_010; int[] power = new int[N]; long[] phi = new long[64]; long getPhi(long v) { long ans = v; for (int i = 2; i*i <= v; i++) { if (v % i == 0) { ans *= (i-1); ans /= i; while (v % i == 0) v /= i; } } if (v != 1) ans = ans*(v-1)/v; return ans; } long mod(long v, long m) { if (v < m) return v; return (v % m) + m; } long solve(int l, int r, int d) { if (l > r || phi[d] == 0) return 1; long e = solve(l + 1, r, d + 1); long ans = 1, v = power[l]; while (e != 0) { if ((e & 1) != 0) ans = mod(ans * v, phi[d]); v = mod(v*v, phi[d]); e >>>= 1; } return ans; } void start() { int n = readInt(), m = readInt(); for (int i = 1; i <= n; i++) power[i] = readInt(); phi[0] = m; for (int i = 0; phi[i] != 1; i++) phi[i+1] = getPhi(phi[i]); int q = readInt(); while (q-- > 0) { int l = readInt(), r = readInt(); System.out.println((l == r ? power[l] : solve(l, r, 0)) % m); } } public static void main(String[] args) { new Test().start(); } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
d28d9be1c728369e3cec84a84d1cd36b
train_000.jsonl
1514037900
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; public class F { private static int[] w; private static int fastModPow(int a, int b, int mod) { int modPow = 1; for (; b > 0; b >>= 1) { if ((b & 1) != 0) modPow = modMul(modPow, a, mod); a = modMul(a, a, mod); } return modPow; } private static int modMul(int a, int b, int mod) { return 1L * a * b >= mod ? (int) ((1L * a * b) % mod) + mod : a * b; } private static int phi(int n) { int phi = n; for (int i = 2; 1L * i*i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; phi -= phi / i; } } if (n > 1) phi -= phi / n; return phi; } public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String[] splits; splits = in.readLine().split(" "); int n = Integer.parseInt(splits[0]); int m = Integer.parseInt(splits[1]); w = new int[n]; splits = in.readLine().split(" "); for (int i = 0; i < n; i++) w[i] = Integer.parseInt(splits[i]); ArrayList<Integer> phis = new ArrayList<>(); for ( phis.add(m); phis.get(phis.size() - 1) > 1; phis.add(phi(phis.get(phis.size() - 1))) ); int q = Integer.parseInt(in.readLine()); while (q-- > 0) { splits = in.readLine().split(" "); int l = Integer.parseInt(splits[0]) - 1; int r = Integer.parseInt(splits[1]) - 1; int answer = 1; for (int i = Math.min(r - l, phis.size() - 1); i >= 0; i--) answer = fastModPow(w[l + i], answer, phis.get(i)); out.println(answer % m); } out.flush(); } }
Java
["6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6"]
4.5 seconds
["1\n1\n2\n4\n256\n3\n27\n597484987"]
Note327 = 7625597484987
Java 8
standard input
[ "number theory", "math" ]
a0038cd4698c649cf95759d744854cfc
First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109). Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have. Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you. kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n).
2,700
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
standard output
PASSED
de9a5fc0ca8a2f55348e28e047cb3bc3
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
//1020A //SISBuilding import java.io.*; import java.util.*; public class SISBuilding { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tow = sc.nextInt(); int flo = sc.nextInt(); int lo = sc.nextInt(); int hi = sc.nextInt(); int q = sc.nextInt(); while(q-->0) { int t1 = sc.nextInt(); int f1 = sc.nextInt(); int t2 = sc.nextInt(); int f2 = sc.nextInt(); long ans = Math.abs(t1 - t2); if(t1 == t2) ans = Math.abs(f1 - f2); else if(f1 < f2) { if(f1 > hi) ans += (f1 - hi) + (f2 - hi); else if(f1 < lo) ans += (lo - f1) + Math.abs(f2 - lo); else ans += f2 - f1; } else if(f1 > f2) { if(f1 < lo) ans += (lo - f1) + (lo - f2); else if(f1 > hi) ans += (f1 - hi) + Math.abs(f2 - hi); else ans += f1 - f2; } else { if(f1 < lo) ans += (lo - f1) + (lo - f2); else if(f1 > hi) ans += (f1 - hi) + (f2 - hi); } pw.println(ans); } pw.flush(); pw.close(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
ef9e665c8a9b426891d4a2619fbb16ef
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int h = scan.nextInt(); int a = scan.nextInt(); int b = scan.nextInt(); int k = scan.nextInt(); for(int q=0;q<k;q++){ int ta = scan.nextInt(); int fa = scan.nextInt(); int tb = scan.nextInt(); int fb = scan.nextInt(); int res = tb-ta; if(ta>tb) { res = ta-tb; } if(res>0 && fa<a){ res += a-fa; fa=a; } else if(res>0 && fa>b){ res += fa-b; fa=b; } if(fa>fb){ res += fa-fb; } else { res += fb-fa; } System.out.println(res); } scan.close(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
8b76c20285240e4f72d17f93b86e37f5
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.io.*; public class A3 { public static void main(String[] args) throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int h = Integer.parseInt(s[1]); int a = Integer.parseInt(s[2]); int b = Integer.parseInt(s[3]); int k = Integer.parseInt(s[4]); PrintWriter out = new PrintWriter(System.out); for(int i = 0; i < k; i++) { s = br.readLine().split(" "); int ans = 0; int ta = Integer.parseInt(s[0]); int fa = Integer.parseInt(s[1]); int tb = Integer.parseInt(s[2]); int fb = Integer.parseInt(s[3]); if(ta==tb) ans = Math.abs(fa-fb); else if(fa >= a && fa <= b) ans = Math.abs(tb-ta) + Math.abs(fb-fa); else if(fa < a) ans = (a-fa) + Math.abs(tb-ta) + Math.abs(a-fb); else ans = (fa-b) + Math.abs(tb-ta) + Math.abs(b-fb); out.println(ans); } out.flush(); out.close(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
bb283a2e8248772d72384bcd7c62985b
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
//package pack; import java.util.*; public class second { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int h=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int k=sc.nextInt(); for(int rep=0;rep<k;rep++) { int ta=sc.nextInt(); int fa=sc.nextInt(); int tb=sc.nextInt(); int fb=sc.nextInt(); if(ta==tb) { System.out.println(Math.abs(fa-fb)); continue; } int towercost=0; if(fa<a) { towercost=a-fa; fa=a; } else if(fa>b) { towercost=fa-b; fa=b; } int towerchangecost=Math.abs(ta-tb); int secondtowercost=Math.abs(fa-fb); System.out.println(towercost+towerchangecost+secondtowercost); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
41219da85ad4b133b7ea2d8b51e948fa
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class NewBuildingInSIS { private int n; private int h; private int a; private int b; public NewBuildingInSIS(int n,int h,int a, int b){ this.n=n; this.h=h; this.a=a; this.b=b; } public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int h=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int t=scan.nextInt(); NewBuildingInSIS town=new NewBuildingInSIS(n,h,a,b); while (t-->0){ int t1=scan.nextInt(); int f1=scan.nextInt(); int t2=scan.nextInt(); int f2=scan.nextInt(); System.out.println(town.timeTaken(t1,f1,t2,f2)); } } //3 10 3 5 1 private int timeTaken(int t1,int f1,int t2,int f2){ if(t1==t2){ return mod(f1,f2); } int totalTime=0; int linkingFloor=nearestLinkingFloor(f1); totalTime=totalTime+(mod(f1,linkingFloor)); totalTime=totalTime+(mod(t1,t2)); totalTime=totalTime+(mod(linkingFloor,f2)); return totalTime; } private int nearestLinkingFloor(int f1){ if(f1<=b && f1>=a){ return f1; } else if(mod(f1,a)>=mod(f1,b)){ return b; }else{ return a; } } private int min(int n1,int n2){ if(n1<n2){ return n1; }else { return n2; } } private int mod(int n1,int n2){ if(n1>n2){ return n1-n2; } return n2-n1; } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
e1cd629e576612a5b40d32e07d9c396e
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class A1020 { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(System.out); String[] line = bufferedReader.readLine().split(" "); int n = Integer.parseInt(line[0]); int h = Integer.parseInt(line[1]); int a = Integer.parseInt(line[2]); int b = Integer.parseInt(line[3]); int k = Integer.parseInt(line[4]); while (k-- > 0) { line = bufferedReader.readLine().split(" "); int x1 = Integer.parseInt(line[0]); int y1 = Integer.parseInt(line[1]); int x2 = Integer.parseInt(line[2]); int y2 = Integer.parseInt(line[3]); int time = Math.abs(x2 - x1); if (time != 0) { if (y1 < a && y2 < a) { time += 2 * a - y1 - y2; } else if (y1 > b && y2 > b) { time += y1 + y2 - 2 * b; } else if (y1 < a && y2 > b) { time += a - y1 + y2 - b + b - a; } else if (y2 < a && y1 > b) { time += a - y2 + y1 - b + b - a; } else { time += Math.abs(y2 - y1); } }else{ time += Math.abs(y1 - y2); } System.out.println(time); } printWriter.flush(); printWriter.close(); bufferedReader.close(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
89b2f44343888668c241f2eb64bea730
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; import java.io.*; public class programA { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); int a =Integer.parseInt(st.nextToken()); int b =Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); for(int i=1;i<=k;i++){ st = new StringTokenizer(br.readLine()); int ta = Integer.parseInt(st.nextToken()); int fa = Integer.parseInt(st.nextToken()); int tb = Integer.parseInt(st.nextToken()); int fb = Integer.parseInt(st.nextToken()); int ans = 0; if(ta-tb == 0)ans = (Math.abs(fa-fb)); else { int dis1; if(a<=fa && fa<=b) dis1 = fa; else if(fa>b) dis1 = b; else dis1 = a; ans = Math.abs(ta-tb) + Math.abs(fa-dis1)+ Math.abs(dis1-fb); } System.out.println(ans); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
7085421e4cabda988f69d139413f3bdc
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class ReceiveInput { public static int abs(int a) { return a>=0? a:-a; } public static int min(int a,int b) { return a>b?b:a; } public static void main(String[]args) { Scanner input=new Scanner(System.in); int n=input.nextInt(); int h=input.nextInt(); int a=input.nextInt(); int b=input.nextInt(); int k=input.nextInt(); for(int i=0;i<k;i++) { int data1=input.nextInt(); int data2=input.nextInt(); int data3=input.nextInt(); int data4=input.nextInt(); if(data1==data3) { System.out.println(abs(data2-data4)); } else { int ans=0; if(data2<a||data2>b) ans=min(abs(data2-a)+abs(data4-a),abs(data2-b)+abs(data4-b)); else { ans=abs(data2-data4); } System.out.println(ans+abs(data1-data3)); } } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
93bcd94c8de4bd808589d52c12f95a00
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
/* * Remember a 7.0 student can know more than a 10.0 student. * Grades don't determine intelligence, they test obedience. * I Never Give Up. */ import java.util.*; import java.util.Map.Entry; import java.io.*; import static java.lang.System.out; import static java.util.Arrays.*; import static java.lang.Math.*; public class ContestMain { private static Reader in=new Reader(); private static StringBuilder ans=new StringBuilder(); private static long MOD=1000000007;//10^9+7 private static final int N=100000; //10^5 // private static final int LIM=26; // private static final double PI=3.1415; // private static ArrayList<Integer> v[]=new ArrayList[N]; // private static int color[]=new int[N]; //For Graph Coloring // private static boolean mark[]=new boolean[N]; // private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // private static void dfs(int node){mark[node]=true;for(int x:v[node]){if(!mark[x]){dfs(x,v);}}} private static long powmod(long x,long n,long m){ if(n==0)return 1; else if(n%2==0)return(powmod((x*x)%m,n/2,m)); else return (x*(powmod((x*x)%m,(n-1)/2,m)))%m; } // private static void shuffle(long [] arr) { // for (int i = arr.length - 1; i >= 2; i--) { // int x = new Random().nextInt(i - 1); // long temp = arr[x]; // arr[x] = arr[i]; // arr[i] = temp; // } // } public static void main(String[] args) throws IOException{ int n=in.nextInt(); int h=in.nextInt(); int a=in.nextInt(); int b=in.nextInt(); int k=in.nextInt(); int ta,tb,fa,fb,x; long cnt; while(k-->0){ cnt=0; ta=in.nextInt(); fa=in.nextInt(); tb=in.nextInt(); fb=in.nextInt(); if(ta==tb){ cnt=abs(fa-fb); } else{ cnt=Long.MAX_VALUE; if((fa>=a&&fa<=b)&&(fb>=a&&fb<=b)){ cnt=abs(fa-fb); } else{ cnt=min(cnt,abs(fa-a)+abs(a-fb)); cnt=min(cnt,abs(fa-b)+abs(b-fb)); } cnt+=abs(ta-tb); } ans.append(cnt+"\n"); } out.println(ans); } static class Pair<T> implements Comparable<Pair>{ int l; int r; Pair(){ l=0; r=0; } Pair(int k,int v){ l=k; r=v; } @Override public int compareTo(Pair o) { if(l!=o.l)return (int) (l-o.l); else return (int) (r-o.r); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
69bda8a6a28dff3c02c56d8cceb8cb7d
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; import java.io.*; public class building { public void run() throws Exception { Scanner file = new Scanner(System.in); int n = file.nextInt(), h = file.nextInt(), low = file.nextInt(), high = file.nextInt(), k = file.nextInt(); for (int i= 0; i < k; i++) { long a = file.nextInt(), b = file.nextInt(), c = file.nextInt(), d = file.nextInt(); long total = 0; if ( a - c == 0) { System.out.println(Math.abs(b - d)); } else { if (b >= low && b <= high) { total += Math.abs(a - c) + Math.abs(b - d); } else { if (b > high) { total += (b - high) + Math.abs(a - c) + Math.abs(high - d); } else { total += (low - b) + Math.abs(a - c) + Math.abs(low - d); } } System.out.println(total); } } } public static void main(String[] args) throws Exception { new building().run(); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
ea088c0cfb33025d500122b20ace7982
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class NewBuild{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int h = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int k = sc.nextInt(); long arr[] = new long[k]; long sum = 0; int z = 0; for(int j = 0;j < k;j++){ int ta = sc.nextInt(); int fa = sc.nextInt(); int tb = sc.nextInt(); int fb = sc.nextInt(); if(ta < tb) sum = tb - ta; else sum = ta - tb; int i = 0; if(sum == 0){ if(fa > fb) sum = sum + fa - fb; else sum = sum + fb - fa; } else{ if(fa >= b && fb >= b){ i = b; } else if(fa < a && fb < a) i = a; else{ if(fa < fb) i = (fb + fa) / 2; else i = (fb + fa) / 2; } if(i < fa) sum = sum + fa - i; else sum = sum + i - fa; if(i > fb) sum = sum + i - fb; else sum = sum + fb - i; } arr[z] = sum; z++; } for(long s: arr) System.out.println(s); } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
1d21b4019b6a227a85cd8ebec384ccf3
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
/** * Author: Ridam Nagar * Date: 27 February 2019 * Time: 01:17:36 **/ /* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; /* 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 n=sc.nextInt(); int h=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int k=sc.nextInt(); for(int j=0;j<k;j++){ int t1 =sc.nextInt(); int f1 =sc.nextInt(); int t2 =sc.nextInt(); int f2 =sc.nextInt(); if(t1==t2)System.out.println((int)Math.abs(f1-f2)); else if (f1>=a&&f1<=b) System.out.println(Math.abs(f1-f2)+Math.abs(t1-t2)); else { System.out.println((int)(Math.abs(f1<a?f1-a:f1-b)+Math.abs(t1-t2)+Math.abs((f1<a?a:b)-f2))); } } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
48fb618bc2449112f30d978f73359aa3
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author nirav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scan in = new Scan(inputStream); PrintWriter out = new PrintWriter(outputStream); ANewBuildingForSIS solver = new ANewBuildingForSIS(); solver.solve(1, in, out); out.close(); } static class ANewBuildingForSIS { public void solve(int testNumber, Scan in, PrintWriter out) { long n = in.scanLong(); long h = in.scanLong(); long a = in.scanLong(); long b = in.scanLong(); long k = in.scanLong(); while (k-- > 0) { long ans = 0; long ta = in.scanLong(); long fa = in.scanLong(); long tb = in.scanLong(); long fb = in.scanLong(); if(ta==tb) ans=Math.abs(fa-fb); else if(fa>b) ans=Math.abs(fa-b)+Math.abs(fb-b); else if(fa<a) ans=Math.abs(fa-a)+Math.abs(fb-a); else ans=Math.abs(fa-fb); ans=ans+(Math.abs(ta-tb)); System.out.println(ans); } } } static class Scan { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public Scan(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } public long scanLong() { long I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
1fa09b7a62ae08e0c103200b2f771404
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author nirav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scan in = new Scan(inputStream); PrintWriter out = new PrintWriter(outputStream); ANewBuildingForSIS solver = new ANewBuildingForSIS(); solver.solve(1, in, out); out.close(); } static class ANewBuildingForSIS { public void solve(int testNumber, Scan in, PrintWriter out) { long n = in.scanLong(); long h = in.scanLong(); long a = in.scanLong(); long b = in.scanLong(); long k = in.scanLong(); while (k-- > 0) { long ans = 0; long ta = in.scanLong(); long fa = in.scanLong(); long tb = in.scanLong(); long fb = in.scanLong(); if(ta==tb) ans=Math.abs(fa-fb); else if(fa>b) ans=Math.abs(fa-b)+Math.abs(fb-b); else if(fa<a) ans=Math.abs(fa-a)+Math.abs(fb-a); else ans=Math.abs(fa-fb); ans=ans+(Math.abs(ta-tb)); out.println(ans); } } } static class Scan { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public Scan(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } public long scanLong() { long I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
cfc0927e57cc3e75b91e591e6251979c
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; public class vashya { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int h=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int k=sc.nextInt(); for(int i=0;i<k;i++) { long ans=0; int ta=sc.nextInt(); int fa=sc.nextInt(); int tb=sc.nextInt(); int fb=sc.nextInt(); if(ta==tb) ans=Math.abs(fa-fb); else if(fa>b) ans=Math.abs(fa-b)+Math.abs(fb-b); else if(fa<a) ans=Math.abs(fa-a)+Math.abs(fb-a); else { ans=Math.abs(fa-fb); } ans=ans+(Math.abs(ta-tb)); System.out.println(ans); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
e75338cbfd9514b3dba930c9816c0872
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; public class Travel{ public static void main(String[] args) { MyScanner sc=new MyScanner(); int towers=sc.ni(); int floors=sc.ni(); int a=sc.ni(); int b=sc.ni(); int q=sc.ni(); while(q-->0) { int ans=0; int initBuild=sc.ni(), initFloor=sc.ni(), finalBuild=sc.ni(), finalFloor=sc.ni(); int buildGap=Math.abs(initBuild-finalBuild); int elevation=-1; if(initBuild==finalBuild && initFloor==finalFloor) { System.out.println(0); continue; } else if(initBuild==finalBuild) { System.out.println(Math.abs(initFloor-finalFloor)); continue; } if(initFloor<a) {ans+=a-initFloor;elevation=a;} else if(initFloor>b) {ans+=initFloor-b;elevation=b;} else {ans=0;elevation=initFloor;} ans+=buildGap; ans+=Math.abs(elevation-finalFloor); System.out.println(ans); } } private 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 ni() { return Integer.parseInt(next()); } float nf() { return Float.parseFloat(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
5662e494dd0854b8feac9ed1757c866c
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; public class SISBuilding { public static void main(String... args) { Scanner sc = new Scanner(System.in); int towers = sc.nextInt(); int floors = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int queries = sc.nextInt(); for (int i = 0; i < queries; i++) { int t1 = sc.nextInt(); int f1 = sc.nextInt(); int t2 = sc.nextInt(); int f2 = sc.nextInt(); int result = 0; if (f1 < a && f2 < a && t1 != t2) result = Math.abs(t1 - t2) + Math.abs(f1 - a) + Math.abs(f2 - a); else if (t1 != t2 && f1 > b && f2 > b) result = Math.abs(t1 - t2) + Math.abs(f1 - b) + Math.abs(f2 - b); else result = Math.abs(t1 - t2) + Math.abs(f1 - f2); System.out.println(result); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
392374ae34cb579264072f9c82747245
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; import java.lang.Math.*; public class NewBuilding{ public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); //no. of towers int h = s.nextInt(); //no. of flowers int a = s.nextInt(); //lowest floor int b = s.nextInt(); //highest floor int k = s.nextInt(); //total queries int q1, q2, q3, q4; int result = 0; int temp1h = 0; int temp2h = 0; int dist = 0; int ar[] = new int[k]; for(int i=0; i<k ;i++){ // taking 4 queries at a time which is building1 floor_of_1 building_2 floor_of_2 q1 = s.nextInt(); q2 = s.nextInt(); q3 = s.nextInt(); q4 = s.nextInt(); //check if building are same if(q1 == q3){ result = Math.abs(q2-q4); ar[i] = result; } //if not same then do following else{ // if floor_of_1 is greater than upperlimit than just subtract them and look for floor_of_2 if(q2 > b){ temp1h = Math.abs(q2 - b); temp2h = Math.abs(q4 - b); } else if (q2 < a){ temp1h = Math.abs(q2 - a); temp2h = Math.abs(q4 - a); } else if ((q2 <= b) && (q2 >= a)){ temp1h = 0; temp2h = Math.abs(q2-q4); } dist = Math.abs(q3 - q1); result = temp1h + dist + temp2h; ar[i] = result; } } for(int j=0; j<k; j++){ System.out.println(ar[j]); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
247bbaf579fecbb4fdbad8d8025005c7
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.Scanner; /** * * @author Suraya */ public class Main { public static void main(String[] args) { Scanner leia = new Scanner(System.in); int n = leia.nextInt(); int h = leia.nextInt(); int a = leia.nextInt(); int b = leia.nextInt(); int k = leia.nextInt(); int ta = 0; int fa = 0; int tb = 0; int fb = 0; long P1 = 0, P2 = 0, P3 = 0, P4 = 0, aux = 0; for (int i = 0; i < k; i++) { ta = leia.nextInt(); fa = leia.nextInt(); tb = leia.nextInt(); fb = leia.nextInt(); if (ta==tb&&fa==fb) { System.out.println("0"); } else if (ta == tb && fa != fb) { if (fa > fb) { P1 = fa - fb; } else if (fa < fb) { P1 = fb - fa; } System.out.println(P1); } else { if (fa>=a&&fa<=b) { P1 = 0; aux = fa; } else if (fa>b) { P1 = fa-b; aux = fa-P1; } else if (fa<a) { P1 = a-fa; aux = fa+P1; } if (tb>ta) { P2 = tb-ta; } else { P2 = ta-tb; } if (fb>aux) { P4 = fb-aux; } else { P4 = aux-fb; } System.out.println(P1+P2+P4); aux = 0; } } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
14d94ff3c606ee21631ac0c8721d1fef
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.util.*; import java.io.*; public class cf503a{ public static void main(String [] args) throws IOException{ InputReader in = new InputReader("cf503a.in"); StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); for(int i = 0; i < k; i++){ st = new StringTokenizer(in.readLine()); int t1 = Integer.parseInt(st.nextToken()); int h1 = Integer.parseInt(st.nextToken()); int t2 = Integer.parseInt(st.nextToken()); int h2 = Integer.parseInt(st.nextToken()); long total = 0; total += Math.abs(t2 - t1); total += Math.abs(h2 - h1); if(h1 < a && h2 < a && t2 != t1) total += Math.min((a - h1), (a - h2)) * 2; if(h2 > b && h1 > b && t2 != t1) total += Math.min((h1 - b), (h2 - b)) * 2; System.out.println(total); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(String s) { try{ reader = new BufferedReader(new FileReader(s), 32768); } catch (Exception e){ reader = new BufferedReader(new InputStreamReader(System.in), 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 String readLine() throws IOException{ return reader.readLine(); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
36cb70fcb99ea94247cd37e43bc0ffc0
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
//package cf.pkgnew.building.pkgfor.sis; // @author samyar import java.util.Scanner; public class CFNewBuildingForSIS { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n =in.nextInt(),h=in.nextInt(),a=in.nextInt(),b=in.nextInt(),k=in.nextInt(); while(k-- != 0) { int ans=0; int ta = in.nextInt(),fa=in.nextInt(); int tb = in.nextInt(),fb=in.nextInt(); ans += Math.abs(tb - ta); if (ans != 0) { if (fa > b) { ans += fa - b; ans += Math.abs(b - fb); } else if (fa < a) { ans += a - fa; ans += Math.abs(a - fb); } else ans += Math.abs(fb - fa); } else ans += Math.abs(fa - fb); System.out.println(ans); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
873e36f60fcc1d29327ffa3690a55ecd
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.nio.file.StandardWatchEventKinds; import java.util.*; import java.lang.*; import java.util.concurrent.LinkedBlockingDeque; import static java.lang.Math.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "Check2", 1 << 28).start();// to increse stack size in java } long mod=(long)(1e9+7); public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); long n=in.nextLong(); long h=in.nextLong(); long a=in.nextLong(); long b=in.nextLong(); int k=in.nextInt(); while (k-->0){ long ta=in.nextLong(); long fa=in.nextLong(); long tb=in.nextLong(); long fb=in.nextLong(); long ans=0; if(ta==tb){ w.println(abs(fa-fb)); continue; } if(fa>=a&&fa<=b){ ans=abs(ta-tb); ans+=abs(fb-fa); } else{ ans=abs(tb-ta); long val1=abs(fa-a)+abs(fb-a); long val2=abs(fa-b)+abs(fb-b); ans+=min(val1,val2); } w.println(ans); } w.close(); } void debug(Object...args) { System.out.println(Arrays.deepToString(args)); } class pair { int a; long b; pair(int a,long b){ this.a=a; this.b=b; } public boolean equals(Object obj) { // override equals method for object to remove tem from arraylist and sets etc....... if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; pair other = (pair) obj; if (b!= other.b||a!=other.a) return false; return true; } } long modinv(long a,long b) { long p=power(b,mod-2,mod); p=a%mod*p%mod; p%=mod; return p; } long power(long x,long y,long mod){ if(y==0)return 1%mod; if(y==1)return x%mod; long res=1; x=x%mod; while(y>0) { if((y%2)!=0){ res=(res*x)%mod; } y=y/2; x=(x*x)%mod; } return res; } long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } void sev(int a[],int n){ for(int i=2;i<=n;i++)a[i]=i; for(int i=2;i<=n;i++){ if(a[i]!=0){ for(int j=2*i;j<=n;){ a[j]=0; j=j+i; } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars){ curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String stock = ""; try { stock = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return stock; } 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); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
36a070d88cf2300c0a81dbf448f34934
train_000.jsonl
1533994500
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class d { public static void main(String[] args) throws Exception { Scanner s=new Scanner(System.in); int n=s.nextInt(); int h=s.nextInt(); int a=s.nextInt(); int b=s.nextInt(); int q=s.nextInt(); // int[] arr=new int[n]; for(int e=0;e<q;e++) { int x1=s.nextInt(); int y1=s.nextInt(); int x2=s.nextInt(); int y2=s.nextInt(); long ans=0; ans=ans+Math.abs(x2-x1); if(x1-x2!=0) { if(y1<a||y1>b) { if(Math.abs(y1-a)<Math.abs(y1-b)) { ans=ans+Math.abs(y1-a); ans=ans+Math.abs(a-y2); }else if(Math.abs(y1-a)>Math.abs(y1-b)){ ans=ans+Math.abs(y1-b); ans=ans+Math.abs(b-y2); }else { ans=ans+Math.abs(y1-b); ans=ans+Math.min(Math.abs(b-y2),Math.abs(a-y2)); } }else { ans=ans+Math.abs(y1-y2); } }else { ans=ans+Math.abs(y1-y2); } System.out.println(ans); } } }
Java
["3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3"]
1 second
["1\n4\n2"]
null
Java 8
standard input
[ "math" ]
bdce4761496c88a3de00aa863ba7308d
The first line of the input contains following integers: n: the number of towers in the building (1 ≤ n ≤ 108), h: the number of floors in each tower (1 ≤ h ≤ 108), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
1,000
For each query print a single integer: the minimum walking time between the locations in minutes.
standard output
PASSED
22dd3bb4120c1aff42ed62fa7aa6338f
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Comparator; import java.util.HashSet; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { ContestScanner in = new ContestScanner(); long a = in.nextLong(); long b = in.nextLong(); int n = in.nextInt(); for(int q=0; q<n; q++){ int l = in.nextInt(); int t = in.nextInt(); int m = in.nextInt(); int left = l; int right = l + 1000000; int med = (left + right)/2; while(true){ long count = 0; // for(int i=l; i<=med; i++){ // count += a+(i-1)*b; // } long last = a+(med-1)*b; long first = a+(l-1)*b; count = (med-l+1)*(first+last)/2; count = (count + (m-1))/m; if(last > count) count = last; if(count > t){ if(med == l){ med = -1; break; } right = med; med = (left + right)/2; }else{ if(count == t){ break; }else if(left+1 == right){ // for(int i=l; i<=right; i++){ // count += a+(i-1)*b; // } last = a+(right-1)*b; first = a+(l-1)*b; count = (right-l+1)*(first+last)/2; count = (count + (m-1))/m; if(last > count) count = last; if(count > t){ med = left; }else med = right; break; } left = med; med = (left + right + 1)/2; } } System.out.println(med); } } } class Node{ int id; HashSet<Node> edge = new HashSet<Node>(); public Node(int id) { this.id = id; } public void createEdge(Node node) { edge.add(node); } } class MyComp implements Comparator<int[]> { final int idx; public MyComp(int idx){ this.idx = idx; } public int compare(int[] a, int[] b) { return a[idx] - b[idx]; } } class Reverse implements Comparator<Integer> { public int compare(Integer arg0, Integer arg1) { return arg1 - arg0; } } class ContestWriter { private PrintWriter out; public ContestWriter(String filename) throws IOException { out = new PrintWriter(new BufferedWriter(new FileWriter(filename))); } public ContestWriter() throws IOException { out = new PrintWriter(System.out); } public void println(String str) { out.println(str); } public void print(String str) { out.print(str); } public void close() { out.close(); } } class ContestScanner { private BufferedReader reader; private String[] line; private int idx; public ContestScanner() throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(System.in)); } public ContestScanner(String filename) throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(new FileInputStream( filename))); } public String nextToken() throws IOException { if (line == null || line.length <= idx) { line = reader.readLine().trim().split(" "); idx = 0; } return line[idx++]; } public long nextLong() throws IOException, NumberFormatException { return Long.parseLong(nextToken()); } public int nextInt() throws NumberFormatException, IOException { return (int) nextLong(); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
b814b6354eb25bd6a796304b1062fb6c
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
/* package whatever; // 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 Ideone { //int r2 = (r-l+1)*(a+((l+r)/2-1)*b); static long a; static long b; static long l; static long s[]; public static long f(long x){ return ((x-l+1)*(2*a+(l+x-2)*b))/2; } public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); a = Long.parseLong(sc.next()); b= Long.parseLong(sc.next()); int n = Integer.parseInt(sc.next()); s = new long[1000003]; for(int i = 1;i<=1000002;i++){ s[i]=a+((long)i-1)*b; } for(int i = 0;i<n;i++){ l = Long.parseLong(sc.next()); /* for(int j=(int)l;j<=n;j++){ System.out.print(f(j) + " " ); }*/ // System.out.println(); long t = Long.parseLong(sc.next()); long m= Long.parseLong(sc.next()); int r1 = 1+(int)((t-a)/b); // int r2 = (r-l+1)*(a+((l+r)/2-1)*b); if(a+(l-1)*b>t){ System.out.println(-1); } else{ r1++; /* System.out.println(l + " ------- " + r1); for(int j=(int)l;j<(int)r1;j++){ System.out.print(f((long)j) + " " ); } System.out.println();*/ /// System.out.println("BEGIN"); int r = rsearch((int)l,r1,m*t); // System.out.println("END"); System.out.println(r); } } } public static int rsearch(int first, int last, long key){//returns same thing, noting that first is inclusive, last is exclusive, and a[0]<=key<a[last], taking a[a.length]=infinity // System.out.println(first + " " + last); if(last-first==1){ /* if(a[first]<=key){ return first; } else{ return first-1; }*/ return first; } else{ // System.out.println("compare " + a[(first+last)/2] + " to " + f(key)); if(f((first+last)/2)<=key){ return rsearch((first+last)/2,last,key); } else{ return rsearch(first, (first+last)/2,key); } } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
3f17a516be6dd7a3de0463863fc469b4
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import static java.lang.Math.*; public class A { public A () { long A = sc.nextLong(), B = sc.nextLong(); int N = sc.nextInt(); for (@SuppressWarnings("unused") int i : rep(N)) { long L = sc.nextLong(), T = sc.nextLong(), M = sc.nextLong(); long res = LINF; if (sum(A, B, L, L) > T) res = -1; { long P = L, Q = INF, m = -1; while (Q - P > 1) { m = (P + Q) / 2; if (sum(A, B, m, m) <= T) P = m; else Q = m; } res = min(res, P); } { long Z = T*M; long P = L, Q = INF, m = -1; while (Q - P > 1) { m = (P + Q) / 2; if (sum(A, B, L, m) <= Z) P = m; else Q = m; } res = min(res, P); print(res); } } } long sum(long A, long B, long P, long Q) { long res = (Q - P + 1) * A + B * (Q * (Q-1) - (P-1) * (P-2)) / 2; return res; } private static final int INF = (int) 1e6 + 10; private static final long LINF = (long) 1e18; private static int [] rep(int N) { return rep(0, N); } private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } //////////////////////////////////////////////////////////////////////////////////// private final static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static void print (Object o, Object ... A) { IOUtils.print(o, A); } private static class IOUtils { public static class MyScanner { public String next() { newLine(); return line[index++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = split(readLine()); index = 0; } } private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim(String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static void start() { if (t == 0) t = millis(); } private static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>) o) append(b, p, delim); else { if (o instanceof Double) o = new java.text.DecimalFormat("#.############").format(o); b.append(delim).append(o); } } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print(Object o, Object ... A) { pw.println(build(o, A)); } private static void err(Object o, Object ... A) { System.err.println(build(o, A)); } private static void exit() { IOUtils.pw.close(); System.out.flush(); err("------------------"); err(IOUtils.time()); System.exit(0); } private static long t; private static long millis() { return System.currentTimeMillis(); } private static String time() { return "Time: " + (millis() - t) / 1000.0; } } public static void main (String[] args) { new A(); IOUtils.exit(); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
4163f4dcb77136bc8e0d4ced24e5dde5
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.BufferedReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alejandro Lopez */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public long sum(long l, long r, long A, long B) { return A * (r - l + 1) + B * (r * (r - 1) - (l - 1) * (l - 2)) / 2; } public void solve(int testNumber, InputReader in, PrintWriter out) { int A = in.nextInt(); int B = in.nextInt(); int n = in.nextInt(); for (int i = 0; i < n; ++i) { int l = in.nextInt(); int t = in.nextInt(); int m = in.nextInt(); long lo = 0; long hi = 2l * t * m; while (hi - lo > 1) { long mid = (hi + lo) / 2; long r = l + mid; long last = A + (r - 1l) * B; if (last <= t && sum(l, r, A, B) <= m * 1l * t) { lo = mid; } else { hi = mid; } } long r = l + lo; long last = A + (r - 1l) * B; if (last <= t && sum(l, r, A, B) <= m * 1l * t) { out.println(l + lo); } else { out.println(-1); } } } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
867044a95bfd6117f1a74340ff67ffdd
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.util.InputMismatchException; import java.io.InputStream; import java.math.BigInteger; import java.io.PrintStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * @author karan173 */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { long a, d; long findVal(long i) { return a + (i - 1) * d; } int q; long l, t, m; public void solve(int testNumber, FastReader in, PrintWriter out) { a = in.nl (); d = in.nl (); q = in.ni (); while (q-- > 0) { l = in.ni (); t = in.ni (); m = in.ni (); if (findVal (l) > t) { out.println (-1); continue; } out.println (findMax (l, l + t * m)); } } private long findMax(long lo, long hi) { if (lo > hi) { return -1; } long mid = (lo + hi) / 2; if (ok (mid)) { return Math.max (mid, findMax (mid + 1, hi)); } return findMax (lo, mid - 1); } private boolean ok(long mid) { long val1 = findVal (mid); long val2 = findVal (l); long sum = val1 + val2; if (val1 > t) { return false; } long n = mid - l + 1; double ss = sum; ss *= n; if (ss > Long.MAX_VALUE / 4) { BigInteger big = BigInteger.valueOf (sum); big = big.multiply (BigInteger.valueOf (n)); return big.compareTo (BigInteger.valueOf (2 * t * m)) <= 0; } sum *= (mid - l + 1); return sum <= 2 * t * m; } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 ni() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public long nl() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c) { return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
ba5795b6107f04e627ef0806e866274d
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int A = in.nextInt(); int B = in.nextInt(); int n = in.nextInt(); for (int i = 0; i < n; i++) { int l = in.nextInt(); int t = in.nextInt(); int m = in.nextInt(); long a = 0; long max = 2 * Math.max(m, t) + 1; //Bin search while (max - a > 1) { long q = (max + a) / 2; if (A + (l + q - 2) * 1l * B <= t && ((A + (l - 1) * 1l * B) * 1l * q + (q - 1) * 1l * B * 1l * q / 2 <= t * 1l * m)) { a = q; } else { max = q; } } if (a == 0) out.println("-1"); else out.println(l + a - 1); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
7520f3aa084956bd20612747c1543122
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long A = nextInt(); long B = nextInt(); int n = nextInt(); while (n-->0) { long L = nextInt(); long t = nextInt(); long m = nextInt(); long sL = A + (L-1) * B; if (t < sL) pw.println(-1); else { long left = L; long right = L + t + 1; while (right-left > 1) { long middle = (left+right) >> 1; long sum = (2*A + B * (L+middle-2)) * (middle-L+1) / 2; long need = sum / m; if (sum % m > 0) need++; need = Math.max(need, A+(middle-1)*B); if (t >= need) left = middle; else right = middle; } pw.println(left); } } pw.close(); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
163f57e6a7f154b798bb04a3fd15aab2
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Random; import java.util.StringTokenizer; public class TavasandKarafs { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { long A = nextLong(); long B = nextLong(); long n = nextLong(); while (n-- > 0) { long l = nextLong(); long t = nextLong(); long m = nextLong(); long r = (t - A) / B + 1; long copyL = l; long valueL = A + (l - 1) * B; if (valueL > t) { out.println(-1); continue; } long ans = l; while (l <= r) { long mid = (l + r) >>> 1; long valueMid = A + (mid - 1) * B; long sum = (valueL + valueMid) * (mid - copyL + 1) / 2; if (sum <= t * m) { ans = mid; l = mid + 1; } else r = mid - 1; } out.println(ans); } } public static void main(String args[]) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } static int nextInt() throws IOException { return Integer.parseInt(next()); } static int[] nextIntArray(int len, int start) throws IOException { int[] a = new int[len]; for (int i = start; i < len; i++) a[i] = nextInt(); return a; } static long nextLong() throws IOException { return Long.parseLong(next()); } static long[] nextLongArray(int len, int start) throws IOException { long[] a = new long[len]; for (int i = start; i < len; i++) a[i] = nextLong(); return a; } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static double[] nextDoubleArray(int len, int start) throws IOException { double[] a = new double[len]; for (int i = start; i < len; i++) a[i] = nextDouble(); return a; } static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String nextLine() throws IOException { tok = new StringTokenizer(""); return in.readLine(); } static void shuffleArray(long[] array) { Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { int index = random.nextInt(i + 1); long temp = array[index]; array[index] = array[i]; array[i] = temp; } } static boolean hasNext() throws IOException { while (tok == null || !tok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return false; } tok = new StringTokenizer(s); } return true; } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
890285ee70cfa07e9209d1a4dc4fe11b
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.util.*; import java.io.*; public class TavasAndKarafs { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(in.readLine()); long A = Long.parseLong(st.nextToken()); long B = Long.parseLong(st.nextToken()); long n = Long.parseLong(st.nextToken()); for (int i = 0; i < n; i++) { st = new StringTokenizer(in.readLine()); long l = Long.parseLong(st.nextToken()); long t = Long.parseLong(st.nextToken()); long m = Long.parseLong(st.nextToken()); long lo = l, hi = 1; while (possible(A, B, l, hi, t, m)) hi *= 2; while (lo < hi) { long mid = lo + (hi - lo + 1)/2; if (possible(A, B, l, mid, t, m)) { lo = mid; } else { hi = mid - 1; } } if (!possible(A, B, l, l, t, m)) { out.println(-1); } else { out.println(lo); } } in.close(); out.close(); } public static boolean possible(long A, long B, long l, long r, long t, long m) { return calc(A, B, r) <= t && sum(A, B, l, r) <= t*m; } public static long calc(long A, long B, long i) { return A + B*(i-1); } public static long sum(long A, long B, long l, long r) { return (r-l+1)*A + B*((r-l+1)*(l-1+r-1)/2); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
d59ccc7ad8a7be52e6b4a5984bbb025a
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.*; import java.util.*; public class R299TaskA { static long a,b; public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter w = new PrintWriter(System.out); StringTokenizer st1 = new StringTokenizer(br.readLine()); a = ip(st1.nextToken()); b = ip(st1.nextToken()); int n = ip(st1.nextToken()); for(int i=0;i<n;i++){ StringTokenizer st2 = new StringTokenizer(br.readLine()); int l = ip(st2.nextToken()) - 1; long t = ip(st2.nextToken()); long m = ip(st2.nextToken()); if(t < (a + l*b)){ w.println("-1"); continue; } else w.println(bSLast(l,l+1104010,l,m,t) + 1); } w.close(); } public static int bSLast(int start,int end,int l,long m,long t){ int ret = -1; while(true){ if(start==end) return ret; int mid = (start+end)/2; if(func(mid,l,m,t)) ret = mid; if(end-start==1) return ret; if(!func(mid,l,m,t)) end = mid; else start = mid+1; } } public static boolean func(long r,long l,long m,long t){ long sum = a * (r - l + 1) + b * (1L * r * (r + 1) - 1L * l * (l-1))/2; return sum <= m*t && (a + r*b <= t); } public static int ip(String s){ return Integer.parseInt(s); } public static long lp(String s){ return Long.parseLong(s); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
4bd920484b6d1ba3fe6945d59dc69eae
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.IOException; import java.util.InputMismatchException; public class TavasAndKarafs { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); long A = sc.nextLong(); long B = sc.nextLong(); long N = sc.nextLong(); StringBuilder sb = new StringBuilder(); for (int q = 0; q < N; q++) { long L = sc.nextLong(); long T = sc.nextLong(); long M = sc.nextLong(); long left = L; long rite = (T + B - A) / B; long good = -1; while (left <= rite) { long mid = (left + rite) / 2; long actM = Math.min(M, mid - L + 1); long blocks = actM * T; long sL = A + (L - 1) * B; long sR = A + (mid - 1) * B; long sum = (sL + sR) * (mid - L + 1) / 2; if (sum <= blocks) { good = mid; left = mid + 1; } else { rite = mid - 1; } } sb.append(good + "\n"); } System.out.print(sb.toString()); } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
dcf01d6a54ad6b964b476a192cad1637
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Scanner; public class CF3 { public static void main(String args[]) throws FileNotFoundException{ Scanner sc=new Scanner(System.in); // PrintStream out = new PrintStream(new FileOutputStream("C:/Users/z00191f/Downloads/output.txt")); // System.setOut(out); long A=sc.nextLong(); long B=sc.nextLong(); int n=sc.nextInt(); for(int i=0;i<n;i++){ long l=sc.nextLong(); long t=sc.nextLong(); long m=sc.nextLong(); if(A+(l-1)*B>t){ System.out.println(-1); continue; }else if(A+(l+m-2)*B>=t){ long lo=l; long hi=l+m-1; long mid=hi; while(lo!=mid){ if(A+B*(mid-1)<=t){ lo=mid; }else{ hi=mid; } mid=(lo+hi)/2; //System.out.println(lo+" "+hi+" "+mid+" "+(A+B*(mid-1))); } System.out.println(mid); }else{ long lo=l+m-1; long hi=(long) (1e6+5); long mid=hi; while(lo!=mid){ if(A+(mid-1)*B>t){ hi=mid; }else if(sum(A+(l-1)*B,B,mid-l+1)<=m*t){ lo=mid; }else{ hi=mid; } mid=(lo+hi)/2; //System.out.println(lo+" "+hi); } System.out.println(mid); } } } public static long sum(long A,long B,long m){ return (2*A+(m-1)*B)*m/2; } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
b30663390c5f2b487d1e4d727c34813b
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nguyen Trung Hieu - [email protected] */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { long A = in.readInt(); long B = in.readInt(); int queryCount = in.readInt(); for (int query = 0; query < queryCount; query++) { long idx = in.readInt() - 1; long begin = A + idx * B; long time = in.readInt(); long k = in.readInt(); long total = time * k; if (begin > time) { out.printLine(-1); continue; } long left = 0; long right = 100000000000L; while (right - left > 1) { long mid = (right + left) >> 1; long val = mid + idx; long sum = (val * (val + 1) / 2) - (idx * (idx + 1) / 2); if (begin + A * (val - idx) + sum * B > total || (A + val * B) > time) { right = mid; } else { left = mid; } } long val = right + idx; long sum = (val * (val + 1) / 2) - (idx * (idx + 1) / 2); if (begin + A * (val - idx) + sum * B <= total && (A + val * B) <= time) { out.printLine(idx + right + 1); } else { out.printLine(idx + left + 1); } } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
c7c7db7a6ad79b32f14aac3f405c5d21
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.util.*; import java.io.*; public class A { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); long a = in.nextInt(); long b = in.nextInt(); int n = in.nextInt(); for(int x = 0; x < n; x++) { long l = in.nextInt() - 1; long t = in.nextInt(); long m = in.nextInt(); long low = l; long high = t + 2; while(low < high) { long mid = (low + high + 1) / 2; if(check(a, b, l, mid, t, m)) { low = mid; } else { high = mid - 1; } } if(!check(a, b, l, low, t, m)) { System.out.println(-1); } else { System.out.println(low + 1); } } } public static boolean check(long a, long b, long l, long r, long t, long m) { if(a + b * r > t) { return false; } long sum = a * (r - l + 1) + ((b * (r - l + 1) * (r + l)) / 2); return sum <= t * m; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream input) { br = new BufferedReader(new InputStreamReader(input)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) { return st.nextToken(); } else { st = new StringTokenizer(br.readLine()); return next(); } } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
2d324b97de4d18652049496e78da1927
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class solver implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new solver(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } int a; int b; long getValue(int x) { return a + (long)(x - 1) * b; } long calcArythm(long from, long to, int cnt) { return (from + to) * cnt / 2; } boolean check(int l, int r, int t, int m) { long x = getValue(l); long y = getValue(r); return y <= t && calcArythm(x, y, r-l+1) <= (long)m * t; } void solve() throws IOException { a = readInt(); b = readInt(); int n = readInt(); for (int i=0;i<n;i++) { int l = readInt(); int t = readInt(); int m = readInt(); int left = 0; int right = (int)1e6; right *= 4; int answer = -1; while (left <= right) { int mid = (left + right) / 2; if (check(l, l + mid, t, m)) { answer = l + mid; left = mid + 1; } else { right = mid - 1; } } out.println(answer); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
97a583262e80083688ec43267bfde2f3
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.*; import java.util.*; public class AMain { String noResultMessage = "NoResult"; Parser in = new Parser(); PrintWriter out; long A = in.nextInteger(); long B = in.nextInteger(); int n = in.nextInteger(); public void solve() { for(int i = 0; i < n; ++i){ int l = in.nextInteger(); int t = in.nextInteger(); int m = in.nextInteger(); out.println(solve(l, t, m)); } } private int solve(int l, long t, long m) { long start = A + (l-1)*B; if(start > t) return -1; int r = l; for(int i = 22; i >= 0; --i){ int d = 1 << i; int rr = r + d; long finish = A + (rr-1)*B; if(finish > t)continue; long count = (start + finish) * (rr - l + 1) / 2; if(count <= m * t) r = rr; } return r; } static public class Parser{ Scanner scanner; public Parser() { scanner = new Scanner(System.in).useLocale(Locale.ENGLISH); } public Parser(String str) { scanner = new Scanner(str).useLocale(Locale.ENGLISH); } long nextLong(){ return scanner.nextLong(); } int nextInteger(){ return scanner.nextInt(); } double nextDouble(){ return scanner.nextDouble(); } String nextLine(){ return scanner.nextLine(); } String next(){ return scanner.next(); } int[] nextIntegers(int count){ int[] result = new int[count]; for(int i = 0; i < count; ++i){ result[i] = nextInteger(); } return result; } long[] nextLongs(int count){ long[] result = new long[count]; for(int i = 0; i < count; ++i){ result[i] = nextLong(); } return result; } int[][] nextIntegers(int fields, int count){ int[][] result = new int[fields][count]; for(int c = 0; c < count; ++c){ for(int i = 0; i < fields; ++i){ result[i][c] = nextInteger(); } } return result; } } void noResult(){ throw new NoResultException(); } void noResult(String str){ throw new NoResultException(str); } void run(){ try{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); out = new PrintWriter(outStream); solve(); out.close(); System.out.print(outStream.toString()); } catch (NoResultException exc){ System.out.print(null == exc.response ? noResultMessage : exc.response); } } public static void main(String[] args) { new AMain().run(); } public static class NoResultException extends RuntimeException{ private String response; public NoResultException(String response) { this.response = response; } public NoResultException() { } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
0144dc5164cf4c9a6a97983bcf670653
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.util.*; import java.io.*; public class A { public static void main(String[] args) { new A(new FastScanner()); } int A, B; long sum(long n) { if (n <= 0) return 0; return n*(n+1L)/2; } long countBites(long lo, long hi, int m) { long height = A+B*hi; long numCells = A*(hi-lo+1); numCells += B*(sum(hi)-sum(lo-1)); long numOps = (numCells+m-1)/m; return Math.max(height, numOps); } public A(FastScanner in) { A = in.nextInt(); B = in.nextInt(); int Q = in.nextInt(); PrintWriter out = new PrintWriter(System.out); while (Q-->0) { int start = in.nextInt()-1; int t = in.nextInt(); int m = in.nextInt(); if (countBites(start, start, m) > t) { out.println(-1); continue; } long lo = start, hi = 2*start+1; while (countBites(start, hi, m) <= t) { lo = hi; hi *= 2; } while (lo < hi) { long mid = (lo+hi+1)/2; if (countBites(start, mid, m) <= t) lo = mid; else hi = mid-1; } out.println(lo+1); } out.close(); } } class FastScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner() { stream = System.in; } 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++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } 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(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
6472a3de7f7b391ae1fcaf8d0a7c7323
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
//package round299; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class A { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long a = nl(); long b = nl(); for(int Q = ni();Q >= 1;Q--){ long L = nl(), t = nl(), m = nl(); long left = a + (L-1)*b; if(left > t){ out.println(-1); }else{ long which = (t - a)/b+1; long low = L, high = which + 1; while(high - low > 1){ long h = high + low>>>1; long mm = Math.min(m, h-L+1); long sum = 0; if(h >= which){ sum = (a*which + (which)*(which-1)/2*b) - (a*(L-1) + (L-2)*(L-1)/2*b) + t * (h-which); }else{ sum = (a*h + h*(h-1)/2*b) - (a*(L-1) + (L-2)*(L-1)/2*b); } if(sum <= mm*t){ low = h; }else{ high = h; } } out.println(low); } } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
db66bd458fe7eb488cd6306f5b665ff2
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF299A { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strtok; strtok = new StringTokenizer(in.readLine()); long A = Integer.parseInt(strtok.nextToken()); long B = Integer.parseInt(strtok.nextToken()); int n = Integer.parseInt(strtok.nextToken()); for (int i = 0; i < n; i++) { strtok = new StringTokenizer(in.readLine()); int l = Integer.parseInt(strtok.nextToken()); int t = Integer.parseInt(strtok.nextToken()); int m = Integer.parseInt(strtok.nextToken()); if (A + B * (l - 1) > t) { System.out.println(-1); continue; } int lo = l; int hi = t + 1; while (hi > lo) { int mid = lo + hi >> 1; long s = A * mid + B * mid * (mid - 1) / 2 - A * (l - 1) - B * (l - 1) * (l - 2) / 2; if (A + B * (mid - 1) > t || s > t * 1L * m) hi = mid; else lo = mid + 1; } System.out.println(hi - 1); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
b4c7481ee5e857553c8f5b6e1c2ad546
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { long A, B; int n; Scanner in = new Scanner(System.in); A = in.nextLong(); B = in.nextLong(); n = in.nextInt(); while ((n--) != 0) { long L; long t, m; L = in.nextLong(); t = in.nextLong(); m = in.nextLong(); if (A + (L - 1) * B > t) { System.out.println(-1); continue; } long tl = L, tr = (t - A) / B + 2; while (tr - tl > 1) { long mid = (tl + tr) / 2; if ((L - 1 + mid - 1) * (mid - L + 1) / 2 * B + (mid - L + 1) * A <= t * m) tl = mid; else tr = mid; } System.out.println(tl); } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
b1208f0ee2a0765fe07945157d9ef6b4
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { /*RE*/ long sum(long L, long R, long A, Long B) { return (R - L + 1) * (A + (L - 1) * B + A + (R - 1) * B) / 2; } public void solve(int testNumber, InputReader in, PrintWriter out) { long A = in.nextLong(); long B = in.nextLong(); int n = in.nextInt(); while ((n--) != 0) { long L = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (A + (L - 1) * B > t) { out.println(-1); continue; } /* Long tl = L, tr = (t - A) / B + 1; while (tl <= tr) { Long mid = (tl + tr) / 2; if (sum(L, mid, A, B) < t * m) tl = mid + 1; else if (sum(L, mid, A, B) > t * m) tr = mid - 1; } out.println(tr); */ long tl = L, tr = (t - A) / B + 2; while (tr - tl > 1) { long mid = (tl + tr) / 2; if (sum(L, mid, A, B) <= t * m) tl = mid; else tr = mid; } out.println(tl); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
f806e869e29d12683b1a4eeba948f981
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { /*RE*/ long sum(long L, long R, long A, Long B) { return (R - L + 1) * (A + (L - 1) * B + A + (R - 1) * B) / 2; } public void solve(int testNumber, InputReader in, PrintWriter out) { long A = in.nextLong(); long B = in.nextLong(); int n = in.nextInt(); while ((n--) != 0) { long L = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (A + (L - 1) * B > t) { System.out.println(-1); continue; } long tl = L, tr = (t - A) / B + 2; while (tr - tl > 1) { long mid = (tl + tr) / 2; if ((L - 1 + mid - 1) * (mid - L + 1) / 2 * B + (mid - L + 1) * A <= t * m) tl = mid; else tr = mid; } System.out.println(tl); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
2b1a76342b58f2a685055b7405dfc446
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long A = in.nextLong(); long B = in.nextLong(); int n = in.nextInt(); for (int kase = 1; kase <= n; ++kase) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (t < A + (l - 1) * B) { out.println(-1); } else { long right = (t - A) / B + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if (t * m >= (l - 1 + middle - 1) * (middle - l + 1) / 2 * B + (middle - l + 1) * A) { left = middle; } else { right = middle; } } out.println(left); } } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
0c49241ce559c75238fbe7d348c0e5fe
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long a = in.nextLong(); long b = in.nextLong(); int n = in.nextInt(); for (int i = 0; i < n; ++i) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (t < a + (l - 1) * b) { out.println(-1); } else { long right = (t - a) / b + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if (t * m >= (l - 1 + middle - 1) * (middle - l + 1) / 2 * b + (middle - l + 1) * a) { left = middle; } else { right = middle; } } out.println(left); } } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
892788879543014f884fdf2704544530
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); long a = in.nextLong(); long b = in.nextLong(); int n = in.nextInt(); for (int i = 0; i < n; ++i) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (t < a + (l - 1) * b) { System.out.println(-1); } else { long right = (t - a) / b + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if (t * m >= (l - 1 + middle - 1) * (middle - l + 1) / 2 * b + (middle - l + 1) * a) { left = middle; } else { right = middle; } } System.out.println(left); } } } } class TaskA { public void solve(int testNumber, Scanner in) { } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
c11a77dd4c9f46d63be40e39cae165e6
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long a = in.nextLong(); long b = in.nextLong(); int n = in.nextInt(); for (int i = 0; i < n; ++i) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (t < a + (l - 1) * b) { out.println(-1); } else { long right = (t - a) / b + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if (t * m >= (l - 1 + middle - 1) * (middle - l + 1) / 2 * b + (middle - l + 1) * a) { left = middle; } else { right = middle; } } out.println(left); } } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
29f6180c9a6b03518fefb3eb81cb656b
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long A = in.nextLong(); long B = in.nextLong(); int n = in.nextInt(); for (int kase = 1; kase <= n; ++kase) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (A + (l - 1) * B > t) { out.println(-1); } else { long right = (t - A) / B + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if ((l - 1 + middle - 1) * (middle - l + 1) / 2 * B + (middle - l + 1) * A <= t * m) { left = middle; } else { right = middle; } } out.println(left); } } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
032a6f83857dca63962e5c7903bb7c7c
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long A = in.nextLong(); long B = in.nextLong(); int n = in.nextInt(); for (int kase = 1; kase <= n; ++kase) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (A + (l - 1) * B > t) { out.println(-1); } else { long tr = (t - A) / B + 2; long tl = l; while (tr - tl > 1) { long mid = (tl + tr) / 2; if ((l - 1 + mid - 1) * (mid - l + 1) / 2 * B + (mid - l + 1) * A <= t * m) { tl = mid; } else { tr = mid; } } out.println(tl); } } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
d9970b2702b4958c220fe36c5a771c01
train_000.jsonl
1429029300
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); long a = in.nextLong(); long b = in.nextLong(); int n = in.nextInt(); for (int i = 0; i < n; ++i) { long l = in.nextLong(); long t = in.nextLong(); long m = in.nextLong(); if (t < a + (l - 1) * b) { System.out.println(-1); } else { long right = (t - a) / b + 2; long left = l; while (right - left > 1) { long middle = (left + right) / 2; if (t * m >= (l - 1 + middle - 1) * (middle - l + 1) / 2 * b + (middle - l + 1) * a) { left = middle; } else { right = middle; } } System.out.println(left); } } } }
Java
["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"]
2 seconds
["4\n-1\n8\n-1", "1\n2"]
null
Java 7
standard input
[ "binary search", "greedy", "math" ]
89c97b6c302bbb51e9d5328c680a7ea7
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
1,900
For each query, print its answer in a single line.
standard output
PASSED
ea0d64c68756e40e526a5fd7d31a19bb
train_000.jsonl
1300809600
A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class YoungPhysicist { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int x = 0, y = 0, z = 0; int n = Integer.parseInt(input.readLine()); for(int i = 0 ; i < n; i++) { String s = input.readLine(); StringTokenizer st = new StringTokenizer(s); x += Integer.parseInt(st.nextToken()); y += Integer.parseInt(st.nextToken()); z += Integer.parseInt(st.nextToken()); } if(x == 0 && y == 0 && z == 0) System.out.println("YES"); else System.out.println("NO"); } }
Java
["3\n4 1 7\n-2 4 -1\n1 -5 -3", "3\n3 -1 7\n-5 2 -4\n2 -1 -3"]
2 seconds
["NO", "YES"]
null
Java 6
standard input
[ "implementation", "math" ]
8ea24f3339b2ec67a769243dc68a47b2
The first line contains a positive integer n (1 ≤ n ≤ 100), then follow n lines containing three integers each: the xi coordinate, the yi coordinate and the zi coordinate of the force vector, applied to the body ( - 100 ≤ xi, yi, zi ≤ 100).
1,000
Print the word "YES" if the body is in equilibrium, or the word "NO" if it is not.
standard output
PASSED
b50bfef5e5984a04b7a0eebaebbb39ad
train_000.jsonl
1300809600
A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class YoungPhysicist { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int x = 0, y = 0, z = 0; int n = Integer.parseInt(input.readLine()); for(int i = 0 ; i < n; i++) { String s = input.readLine(); StringTokenizer st = new StringTokenizer(s); x += Integer.parseInt(st.nextToken()); y += Integer.parseInt(st.nextToken()); z += Integer.parseInt(st.nextToken()); } if(x == 0 && y == 0 && z == 0) System.out.println("YES"); else System.out.println("NO"); } }
Java
["3\n4 1 7\n-2 4 -1\n1 -5 -3", "3\n3 -1 7\n-5 2 -4\n2 -1 -3"]
2 seconds
["NO", "YES"]
null
Java 6
standard input
[ "implementation", "math" ]
8ea24f3339b2ec67a769243dc68a47b2
The first line contains a positive integer n (1 ≤ n ≤ 100), then follow n lines containing three integers each: the xi coordinate, the yi coordinate and the zi coordinate of the force vector, applied to the body ( - 100 ≤ xi, yi, zi ≤ 100).
1,000
Print the word "YES" if the body is in equilibrium, or the word "NO" if it is not.
standard output
PASSED
9e85c3ddc9d6083c8f402cd91a020b51
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args)throws IOException { BufferedReader ob=new BufferedReader(new InputStreamReader(System.in)); StringBuffer sb=new StringBuffer(); int t=Integer.parseInt(ob.readLine()); while(t-->0) { String s[]=ob.readLine().split(" "); int n=Integer.parseInt(s[0]); int k=Integer.parseInt(s[1]); char ch[]=ob.readLine().toCharArray(); int pre[]=new int[n]; int suff[]=new int[n]; // pre[0]=0; // suff[n-1]=0; int a=0;int b=0; for(int i=0,j=n-1;i<=Math.min(k,n-1);i++,j--) { pre[i]=a; if(ch[i]=='1') a++; suff[j]=b; if(ch[j]=='1') b++; } int st=0; for(int i=k+1;i<n;i++) { if(ch[i-1]=='1') pre[i]=pre[i-1]+1; else pre[i]=pre[i-1]; if(ch[st]=='1') pre[i]=Math.max(0,pre[i]-1); st++; } st=n-1; for(int i=n-(k+2);i>=0;i--) { // System.out.println("i = "+i); // System.out.println("ch[i+1] = "+ch[i+1]); if(ch[i+1]=='1') suff[i]=suff[i+1]+1; else suff[i]=suff[i+1]; // System.out.println("ch[st] = "+ch[st]); if(ch[st]=='1') suff[i]=Math.max(suff[i]-1,0); // System.out.println("pre["+i+"] = "+pre[i]); st--; } // print(pre); // print(suff); int ans=0; for(int i=0;i<n;i++) { if(ch[i]=='0' && pre[i]==0 && suff[i]==0) { ans++; i+=k; } } sb.append(ans+"\n"); } System.out.println(sb); } static void print(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
ffaf4134a551017ba03497a8636be2bf
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class c1 { public static void main(String[] args) { MyScanner scan = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t=scan.nextInt(); while(t-->0) { int n=scan.nextInt(); int k=scan.nextInt(); boolean[] b=new boolean[n]; String[] s=scan.nextLine().split(""); for(int c=0;c<n;c++) { if(s[c].equals("1")) { for(int i=c;i<=c+k;i++) { if(i<b.length&&b[i]!=true) { b[i]=true; } } for(int i=c;i>=c-k;i--) { if(i>=0&&b[i]!=true) { b[i]=true; } } } } int seats=0; for(int c=0;c<n;c++) { if(b[c]!=true) { seats++; for(int i=c;i<=c+k;i++) { if(i<b.length&&b[i]!=true) { b[i]=true; } } for(int i=c;i>=c-k;i--) { if(i>=0&&b[i]!=true) { b[i]=true; } } } } out.println(seats); } out.close(); } //scanner public static PrintWriter out; 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; } } //methods public static int gcd(int a, int b) { if (b == 0) return a; if (a == 0) return b; return (a > b) ? gcd(a % b, b) : gcd(a, b % a); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static boolean isprime(long a) { if (a == 0 || a == 1) { return false; } if (a == 2) { return true; } for (int i = 2; i < Math.sqrt(a) + 1; i++) { if (a % i == 0) { return false; } } return true; } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
bf78ac1097707fd6e822afcb446c5d85
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.*; public class Main{ public static void main(String [] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- >0) { int n = scan.nextInt(); int k = scan.nextInt(); char s[] = scan.next().toCharArray(); int count =0; for(int i=0;i<n ;i++) { int pos =i; if(s[i]=='0') { for(int j=i+1 ;j<n && j<=i+k ; j++) { if(s[j]=='1') { pos=j; //break; } } if(pos==i) { count++; // i+=k; } } i = pos +k; } System.out.println(count); } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
d1fa4619b79efd04b77e263770ae5cc2
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int test = 0; test < t; test++) { int n = in.nextInt(); int k = in.nextInt(); char[] seq = in.next().toCharArray(); int res = 0; int left = 0, right = 0; for (int i = 0; i < n;) { int next = i + 1; for (; next < n; next++) { if (seq[next] == '1') break; } left = seq[i] == '1' ? k : 0; right = next < n && seq[next] == '1' ? k : 0; int count = next - i; if (left == k) count--; count -= (left + right); if (count > 0) res += (count + k) / (k + 1); i = next; } System.out.println(res); } } } /* for (int i = 0; i < n;) { int j = i + 1; for (; j < n; j++) { if (seq[j] == '1') break; } left = seq[i] == '1' ? k : 0; right = j < n && seq[j] == '1' ? k : 0; int len = j - i; if (left == k) len--; len -= left + right; if (len > 0) { res += (len + k) / (k + 1); } i = j; } */
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
e37d044567f2028c7b128a2754e72a21
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int test = 0; test < t; test++) { int n = in.nextInt(); int k = in.nextInt(); char[] seq = in.next().toCharArray(); int res = 0; int left = 0, right = 0; for (int i = 0; i < n;) { int next = i + 1; for (; next < n; next++) { if (seq[next] == '1') break; } if (seq[i] == '1') left = k; right = next < n && seq[next] == '1' ? k : 0; int count = next - i; if (left == k) count--; count -= (left + right); if (count > 0) res += (count + k) / (k + 1); i = next; } System.out.println(res); } } } /* for (int i = 0; i < n;) { int j = i + 1; for (; j < n; j++) { if (seq[j] == '1') break; } left = seq[i] == '1' ? k : 0; right = j < n && seq[j] == '1' ? k : 0; int len = j - i; if (left == k) len--; len -= left + right; if (len > 0) { res += (len + k) / (k + 1); } i = j; } */
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
c6b2a732723afe55125d19d8de8dbec6
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int test = 0; test < t; test++) { int n = in.nextInt(); int k = in.nextInt(); char[] seq = in.next().toCharArray(); int res = 0; int left = 0, right = 0; int count = 0; for (int i = 0; i < n;) { int j = i + 1; for (; j < n; j++) { if (seq[j] == '1') break; } left = seq[i] == '1' ? k : 0; right = j < n && seq[j] == '1' ? k : 0; int len = j - i; if (left == k) len--; len -= left + right; if (len > 0) { res += (len + k) / (k + 1); } i = j; } System.out.println(res); } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
5793cff2fc62f1af9ed48038a6988559
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int test = 0; test < t; test++) { int n = in.nextInt(); int k = in.nextInt(); char[] seq = in.next().toCharArray(); int res = 0; int left = 0, right = 0; for (int i = 0; i < n;) { int next = i + 1; for (; next < n; next++) { if (seq[next] == '1') break; } if (seq[i] == '1') left = k; if (next <n && seq[next]=='1') right = k; else right = 0; int count = next - i; if (left == k) count--; count -= (left + right); if (count > 0) res += (count + k) / (k + 1); i = next; } System.out.println(res); } } } /* for (int i = 0; i < n;) { int j = i + 1; for (; j < n; j++) { if (seq[j] == '1') break; } left = seq[i] == '1' ? k : 0; right = j < n && seq[j] == '1' ? k : 0; int len = j - i; if (left == k) len--; len -= left + right; if (len > 0) { res += (len + k) / (k + 1); } i = j; } */
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
ce173a4d9beaae0b2f12e7af03168e6a
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
//package codeforces.problem.nov; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; /* https://codeforces.com/problemset/problem/?/? 1 1 1 1 */ public class Task1367_C { static void solve() { int n = FS.nextInt(); int k = FS.nextInt(); char[] s = FS.next().toCharArray(); int res = 0; for (int i = 0; i < n;) { int j = i + 1; for (; j < n && s[j] != '1'; j++); int left = s[i] == '1' ? k : 0; int right = j < n && s[j] == '1' ? k : 0; int len = j - i; if (left == k) { len--; } len -= left + right; if (len > 0) { res += (len + k) / (k + 1); } i = j; } FS.pt.println(res); } public static void main(String[] args) { int T = FS.nextInt(); for (int tt = 0; tt < T; tt++) { solve(); } FS.pt.close(); } static class FS { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer st = new StringTokenizer(""); static PrintWriter pt = new PrintWriter(System.out); static String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } static int[][] read2Array(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } static long[][] read2ArrayL(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } void printArr(long[] arr) { for (long value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static long[] readArrayL(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } static void printArr(int[] arr) { for (int value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static void printArrL(int[] arr) { for (int value : arr) { pt.print(value); pt.print(" "); } pt.println(); } static void print2Arr(int[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pt.print(arr[i][j]); pt.print(" "); } pt.println(); } pt.println(); } static void print2Arr(long[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pt.print(arr[i][j]); pt.print(" "); } pt.println(); } pt.println(); } static List<Long> readListLong(int n) { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } static List<Integer> readListInt(int n) { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } static <T> void printList(List<T> list) { for (T value : list) { pt.print(value); pt.print(" "); } pt.println(); } static void close() { pt.close(); } static long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
c81711de834ebde1c5373f0a655fa83c
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
//package CodeForces; import java.util.Scanner; public class SocialDistance { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); int k=s.nextInt(); String str=s.next(); int i=0;int j=str.length()-1; int count1=0;int count2=0; int ans=0; while(i<n&&str.charAt(i)=='0') { i++; count1++; } while(j>i&&str.charAt(j)=='0') { j--; count2++; } if(count1+count2==str.length()) { if(str.length()<=k+1) { System.out.println(1); t--; continue; } ans=str.length()/(k+1); if(str.length()%(k+1)!=0) { ans++; } System.out.println(ans); t--; continue; } ans=ans+count1/(k+1)+count2/(k+1); while(i<j) { int p=i+1;int prev=i; while(p<j&&str.charAt(p)=='0') { p++; } int m=i+1; while(m<p) { if(m-prev>k&&p-m>k) { ans++; prev=m; } m++; } i=p; } System.out.println(ans); t--; } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
5ceb335f0d0ff62972335db51396685e
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UncheckedIOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; public class TEST { static int LOCAL = 1; public static void main(String[] args) throws IOException { int t = in.nextInt(); while (t-- > 0) { int N = in.nextInt(); int K = in.nextInt(); List<Integer> dec = decomp(in.readLine()); if (dec.size() == 1) { out.println((N + K) / (K + 1)); } else { int count = 0; for (int i = 0; i < dec.size(); i++) { int space = 0; if (i == 0 || i == dec.size() - 1) space = dec.get(i) - K; else space = dec.get(i) - 2 * K; count += (space + K) / (K + 1); } out.println(count); } } out.close(); } public static List<Integer> decomp(String s) { List<Integer> os = new ArrayList<>(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { os.add(count); count = 0; } else { count++; } } os.add(count); return os; } public static <F, S> Pair<F, S> mp(F f, S s) { return new Pair<F, S>(f, s); } public static FastInput in = new FastInput(); public static FastOutput out = new FastOutput(); public static void setIO(String file) { try { in = new FastInput(file + ".in"); out = new FastOutput(file + ".out"); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } static class Pair<F, S> { F f; S s; Pair(F f, S s) { this.f = f; this.s = s; } @Override public int hashCode() { int a = f == null ? 0 : f.hashCode(); int b = s == null ? 0 : s.hashCode(); return (a << 5) - a + b; } @Override public String toString() { return "(" + f + ", " + s + ")"; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (o instanceof Pair<?, ?>) { Pair<?, ?> p = (Pair<?, ?>) o; return ((f == p.f) || p.f.equals(f)) && (s == p.s || p.s.equals(s)); } return false; } } static void print(Object o) { if (LOCAL != 0) System.out.print(o); } static void println(Object o) { if (LOCAL != 0) System.out.println(o); } static class FastInput { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastInput() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastInput(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws IOException { StringBuilder buf = new StringBuilder(64); byte c = read(); while (c <= ' ') c = read(); do { buf.append((char) c); } while ((c = read()) <= ' '); return buf.toString().trim(); } public String readLine() throws IOException { StringBuilder buf = new StringBuilder(64); byte c = read(); while (c <= ' ') c = read(); do { buf.append((char) c); } while ((c = read()) != '\n'); return buf.toString().trim(); } 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 class FastOutput { StringBuilder cache; Writer os; int BufferLimit = 1 << 16; FastOutput() { cache = new StringBuilder(); os = new OutputStreamWriter(System.out); } FastOutput(String file) throws IOException { cache = new StringBuilder(); os = new FileWriter(file + ".out"); } void print(Object o) { cache.append(o.toString()); if (cache.length() > BufferLimit) flush(); } void println(Object o) { cache.append(o.toString() + '\n'); if (cache.length() > BufferLimit) flush(); } void flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
76b0c5441275195d06b981c36293c0e9
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UncheckedIOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; public class TEST2 { static FastInput in = new FastInput(); static FastOutput out = new FastOutput(); public static void main(String[] args) throws IOException { int t = in.nextInt(); while (t-- > 0) { int N = in.nextInt(); int K = in.nextInt(); List<Integer> dec = decomp(in.nextString()); if (dec.size() == 1) { out.println((N + K) / (K + 1)); } else { int count = 0; for (int i = 0; i < dec.size(); i++) { int space = 0; if (i == 0 || i == dec.size() - 1) space = dec.get(i) - K; else space = dec.get(i) - 2 * K; count += (space + K) / (K + 1); } out.println(count); } } out.close(); } public static List<Integer> decomp(String s) { List<Integer> os = new ArrayList<>(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { os.add(count); count = 0; } else { count++; } } os.add(count); return os; } static class FastInput { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastInput() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastInput(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws IOException { StringBuilder buf = new StringBuilder(64); byte c = read(); while (c == ' ' || c == '\n') c = read(); do { buf.append((char) c); } while ((c = read()) != ' ' && c != '\n'); return buf.toString().trim(); } 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 class FastOutput { StringBuilder cache; Writer os; int BufferLimit = 1 << 16; FastOutput() { cache = new StringBuilder(); os = new OutputStreamWriter(System.out); } FastOutput(String file) throws IOException { cache = new StringBuilder(); os = new FileWriter(file + ".out"); } void print(Object o) { cache.append(o.toString()); if (cache.length() > BufferLimit) flush(); } void println(Object o) { cache.append(o.toString() + '\n'); if (cache.length() > BufferLimit) flush(); } void flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
c77c2a939db25c8bfe9eb9d2e13810b6
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
//package BaseCodeForCoding; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class TEST2 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] tokens = br.readLine().split(" "); int N = Integer.parseInt(tokens[0]); int K = Integer.parseInt(tokens[1]); List<Integer> dec = decomp(br.readLine()); if (dec.size() == 1) { sb.append((N + K) / (K + 1)); } else { int count = 0; for (int i = 0; i < dec.size(); i++) { int space = 0; if (i == 0 || i == dec.size() - 1) space = dec.get(i) - K; else space = dec.get(i) - 2 * K; count += (space + K) / (K + 1); } sb.append(count); } sb.append("\n"); } System.out.println(sb); } public static List<Integer> decomp(String s) { List<Integer> os = new ArrayList<>(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { os.add(count); count = 0; } else { count++; } } os.add(count); return os; } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
fc917b13f7f96aa6fbc5d846a2d51a31
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UncheckedIOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; public class TEST2 { static FastInput in = new FastInput(); static FastOutput out = new FastOutput(); public static void main(String[] args) throws IOException { int t = in.nextInt(); while (t-- > 0) { int N = in.nextInt(); int K = in.nextInt(); List<Integer> dec = decomp(in.readLine()); if (dec.size() == 1) { out.println((N + K) / (K + 1)); } else { int count = 0; for (int i = 0; i < dec.size(); i++) { int space = 0; if (i == 0 || i == dec.size() - 1) space = dec.get(i) - K; else space = dec.get(i) - 2 * K; count += (space + K) / (K + 1); } out.println(count); } } out.close(); } public static List<Integer> decomp(String s) { List<Integer> os = new ArrayList<>(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { os.add(count); count = 0; } else { count++; } } os.add(count); return os; } static class FastInput { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastInput() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastInput(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws IOException { StringBuilder buf = new StringBuilder(64); byte c = read(); while (c == ' ' || c == '\n') c = read(); do { buf.append((char) c); } while ((c = read()) != ' ' && c != '\n'); return buf.toString().trim(); } public String readLine() throws IOException { StringBuilder buf = new StringBuilder(64); byte c = read(); while (c == ' ' || c == '\n') c = read(); do { buf.append((char) c); } while ((c = read()) != '\n'); return buf.toString().trim(); } 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 class FastOutput { StringBuilder cache; Writer os; int BufferLimit = 1 << 16; FastOutput() { cache = new StringBuilder(); os = new OutputStreamWriter(System.out); } FastOutput(String file) throws IOException { cache = new StringBuilder(); os = new FileWriter(file + ".out"); } void print(Object o) { cache.append(o.toString()); if (cache.length() > BufferLimit) flush(); } void println(Object o) { cache.append(o.toString() + '\n'); if (cache.length() > BufferLimit) flush(); } void flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
e35f5a89ef09a6b3c29d5dfef90a5e62
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.*; public class SocialDistance { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); System.out.println(solve(s, n, k)); } } private static int solve(String s, int n, int k) { int lastTableSaw = 0; int additionalTables = 0; for (int i = 0; i < n; i++) { if (tableAt(s,i)) { lastTableSaw = k; tablesSeen.remove(i); } else if (lastTableSaw == 0 && !tableInRange(s, n, k, i)) { additionalTables++; lastTableSaw = k; } else if (lastTableSaw > 0) { lastTableSaw--; } } return additionalTables; } final static Set<Integer> tablesSeen = new TreeSet<>(); private static boolean tableInRange(String s, int n, int k, int from) { int max = Math.min(n, from + k + 1); for (Integer i : tablesSeen) { if(i>=from && i<max) return true; } for (int i = from; i < max; i++) { if (tableAt(s,i)) { tablesSeen.add(i); return true; } } return false; } static boolean tableAt(String s, int i){ return s.charAt(i) == '1'; } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
5e984a7080e6e3cab295d196472ecd66
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
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.Comparator; import java.util.List; import java.util.StringTokenizer; public class SocialDistance { // Template for CF public static class ListComparator implements Comparator<List<Integer>> { @Override public int compare(List<Integer> l1, List<Integer> l2) { for (int i = 0; i < l1.size(); ++i) { if (l1.get(i).compareTo(l2.get(i)) != 0) { return l1.get(i).compareTo(l2.get(i)); } } return 0; } } public static void main(String[] args) throws IOException { // Check for int overflow!!!! // Should you use a long to store the sum or smthn? BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(f.readLine()); for (int i = 0; i < T; i++) { StringTokenizer st = new StringTokenizer(f.readLine()); int N = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String str = f.readLine(); int sum = 0; int len = 0; List<Integer> list = new ArrayList<>(); for (int j = 0; j < str.length(); j++) { if (j == str.length() - 1) { if (str.charAt(j) == '0') { len++; } list.add(len); if (str.charAt(j) == '1') { list.add(0); } } else if (j == 0) { if (str.charAt(j) == '0') { len++; } else { list.add(0); } } else { if (str.charAt(j) == '0') { len++; } else { list.add(len); len = 0; list.add(0); } } } // System.out.println(list); for (int j = 0; j < list.size(); j++) { if (j == 0 && j == list.size() - 1) { int a = list.get(j); if (a > 0) { sum += (a + k) / (k + 1); } } else if (j == 0) { int a = list.get(j) - k; if (a > 0) { sum += (a + k) / (k + 1); } } else if (j == list.size() - 1) { int a = list.get(j) - k; if (a > 0) { sum += (a + k) / (k + 1); } } else { int a = list.get(j) - 2 * k; if (a > 0) { sum += (a + k) / (k + 1); } } } out.println(sum); } out.close(); } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
7e2c6690b81aec74a39b5c7ca4e7b357
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Scanner; /** * * @author HP */ public class Question3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int testCases = s.nextInt(); while(testCases-->0) { int size =s.nextInt(); int k = s.nextInt(); s.nextLine(); char positions[] = s.nextLine().toCharArray(); int count=0, j, res=0; int l; for(l=0;l<=k&&l<size;l++) { if(positions[l]=='1') break; } if(l>k||l==size) { positions[0]= '1'; res++; } for(int i= 0 ; i < size; i++) { if(positions[i]=='0') { count++; } if(positions[i]=='1') { count = 0; } if(count>k) { j = 1; while(j<=k) { if(i+j<size&&positions[i+j]=='1') break; j++; } if(j>k) { positions[i] = '1'; res++; } count = 0; } } System.out.println(res); // System.out.println(String.valueOf(positions)); } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
45f4bd1609e122c1c8466c8b8e9f7136
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); String s = in.nextLine(); out.println(ans(n, k, s)); } } static int ans(int n, int k, String s) { int count = 1; int tmpZ = -1; if (k == n) { if (s.contains("1")) { return 0; } else { return 1; } } // String firstKStr = s.substring(0,k+2); // if(!firstKStr.contains("1")){ // count++; // s = "1"+s.substring(1); // } boolean postZ = true; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '0') { tmpZ++; if (tmpZ == k + 1) { count++; tmpZ = 0; postZ = true; } } else { if (postZ && tmpZ < k) { count--; postZ = false; } tmpZ = 0; } } return count; } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
d0667444f9b6ab2d14781a68c9312427
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.util.Scanner; public class SocialDistances { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = Integer.parseInt(sc.nextLine()); if (count == 0) System.out.println(0); while (count > 0) { String row = sc.nextLine(); String[] temp = row.split(" "); int n = Integer.parseInt(temp[0]); int k = Integer.parseInt(temp[1]); row = sc.nextLine(); int[] tables = new int[row.length()]; for (int i = 0; i < tables.length; i++) { tables[i] = Character.getNumericValue(row.charAt(i)); } int ans = 0; int j = 0; for (int i = - k; i <= tables.length - k-1; ) { if (j > tables.length + k-1) break; if (j > tables.length - 1 || tables[j] == 0) { //System.out.printf("CCCCC: %d %d\n", i, j); if (j - i == 2 * k) { tables[(i + j) / 2] = 1; ans++; i = ((i + j) >> 1) + 1; } else j++; //System.out.printf("DDDDD: %d %d\n", i, j); } else { i = j + 1; j = i; } } /*for(int i=0;i<tables.length;){ while(tables[j]==1){ i=j+1; j=i; } if(tables[j]==0&&j-i==2*k-2){ tables[(i+j)/2]=1; ans++; i=j+1; j=i; } if(i==0&&tables[i]==0){ while(tables[j]==0&&j-i<k){ j++; } if(tables[j]==0&&j-i==k){ tables[i]=1; ans++; i=j+1; j=i; } if(tables[j]==1&&j+1<tables.length)i=j+1; } else if(j==tables.length-1&&j-i==2*k-2){ tables[j]=1; ans++; } else{ while(tables[j]==0&&j-i<2*k-2)j++; } }*/ count--; System.out.println(ans); } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
4de68e969a138ac78aeb16ae9803648e
train_000.jsonl
1592318100
Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ "100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.
256 megabytes
import java.io.*; import java.util.*; public class codeforce { public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(bf.readLine().trim()); while(t!=0) { String[] in=bf.readLine().trim().split(" "); int n=Integer.parseInt(in[0]); int k=Integer.parseInt(in[1]); String s=bf.readLine().trim(); char[] ch=s.toCharArray(); if(n==k) { k--; } int i=0,flag=0,count=0; for(i=0;i<k+1;i++) { if(ch[i]=='1') { flag=1; break; } } if(flag==0) { count++; i=1; } else { i=i+1; } if(n==k) { k++; } int p=(2*k)+1; int c=0; while(i<ch.length) { if(ch[i]=='0') { c++; } if(ch[i]=='1') { c=0; } if(c==p) { count++; c=k; } i++; } if(c>=k+1) { count++; } System.out.println(count); t--; } } }
Java
["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"]
2 seconds
["1\n2\n0\n1\n1\n1"]
NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant.
Java 8
standard input
[ "constructive algorithms", "greedy", "math" ]
fd85ebe1dc975a71c72fac7eeb944a4a
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2\cdot 10^5$$$) — the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\cdot 10^5$$$.
1,300
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.
standard output
PASSED
030d17c6fa00b6d657099ffd75629f2b
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Codeforces implements Runnable { private BufferedReader br = null; private PrintWriter pw = null; private StringTokenizer stk = new StringTokenizer(""); public static void main(String[] args) { new Thread(new Codeforces()).run(); } public void run() { /* * try { br = new BufferedReader(new FileReader("/home/user/freshdb.sql")); pw = new * PrintWriter("/home/user/freshdb_fix.sql"); } catch (FileNotFoundException e) { * e.printStackTrace(); } */ br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); solver(); pw.close(); } private void nline() { try { if (!stk.hasMoreTokens()) stk = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException("KaVaBUnGO!!!", e); } } private String nstr() { while (!stk.hasMoreTokens()) nline(); return stk.nextToken(); } private int ni() { return Integer.valueOf(nstr()); } private long nl() { return Long.valueOf(nstr()); } private double nd() { return Double.valueOf(nstr()); } private BigInteger nbi() { return new BigInteger(nstr()); } String nextLine() { try { return br.readLine(); } catch (IOException e) { } return null; } class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } } private void bfs(int t, boolean[] using, int[][] g) { Stack<Integer> st = new Stack<Integer>(); int n = using.length; st.add(t); while (!st.empty()) { int p = st.pop(); for (int i = 0; i < n; i++) { if (g[p][i] == 1 && !using[i]) { st.add(i); using[i] = true; } } } } public int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private boolean checkPrime(int a) { for (int i = 2; i <= Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } class KeySet implements Comparable<KeySet> { int key; int value; public KeySet(int key, int value) { this.key = key; this.value = value; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } @Override public int compareTo(KeySet keySet) { if (this.value > keySet.value) { return 1; } else if (this.value < keySet.value) { return -1; } else { return 0; } } public int getValue() { return value; } public void setValue(int value) { this.value = value; } @Override public String toString() { return "{" + key + "; " + value + "}"; } } BigInteger toPositive(BigInteger bi) { return bi.signum() < 0 ? bi.negate() : bi; } int fact(int x) { int ans = 1; for (int i = 1; i <= x; i++) { ans *= i; } return ans; } private long getZeroIfNegative(long n) { return n > 0 ? n : 0; } private boolean checkPalindrome(String s) { for (int i = 0; i < s.length() / 2 + 1; i++) { if (s.charAt(i) != s.charAt(s.length() - i - 1)) { return false; } } return true; } private void solver() { int n = ni(), k = ni(); List<Integer> a = new ArrayList<>(); int ans = 0; for (int i = 0; i < n; i++) { int t = ni(); ans += t / 10; if (t % 10 != 0) { a.add(t % 10); } } Collections.sort(a, Collections.reverseOrder()); for (int i = 0; i < a.size(); i++) { if (k <= 0) { break; } int d = 10 - a.get(i); if (d <= k) { ans++; k -= d; } else { break; } } if (k > 0) { int d = n * 10 - ans; if (k <= d * 10) { ans += k / 10; } else { ans = n * 10; } } System.out.println(ans); } void exit() { pw.close(); System.exit(0); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
34a0a38d5fc58665659e70fd45f1cedd
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int l = n*100; int a[] = new int[n]; int m = 0 ; for(int i = 0 ; i < n ; i++){ a[i] = in.nextInt(); l -=a[i]; m+=a[i]/10; a[i]%=10; a[i]=10-a[i]; } Arrays.sort(a); for(int i = 0 ; i <a.length ; i++){ //System.out.println("k = "+k +" a[i]="+a[i]); k-=a[i]; l-=a[i]; if(k>=0&&l>=0){ m++; }else break; if(i==a.length-1) if(k>=l){ m=n*10; }else{ m+=k/10; } } System.out.println(m); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
27082417b1b536f6bc779947294fee38
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.*; import java.util.*; public class C { FastScanner in; PrintWriter out; int i = 0, j = 0; void solve() { /**************START**************/ int n = in.nextInt(); int k = in.nextInt(); int totalPoints = 0; int[] needed = new int[n]; int cur; for (i = 0; i < n; i++) { cur = in.nextInt(); totalPoints += cur/10; if (cur % 10 == 0) { needed[i] = 10; } else { needed[i] = 10 - (cur % 10); } } Arrays.sort(needed); boolean done = false; for (i = 0; i < n && (!done); i++) { if (k >= needed[i]) { totalPoints++; k -= needed[i]; } else { done = true; } } totalPoints += k/10; if (totalPoints > (n*10)) { totalPoints = n*10; } out.println(totalPoints); /***************END***************/ } public static void main(String[] args) { new C().runIO(); } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
e420b33287e73b68eeaf2f08176ca7e8
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; import static java.lang.Math.min; public class C { static Scanner in = new Scanner(System.in); static PrintStream out = System.out; public static void main(String[] args) { int n = in.nextInt(); int k = in.nextInt(); int[] skills = new int[n]; int[] required = new int[n]; int result = 0; for (int i = 0; i < n; ++i) { int s = in.nextInt(); skills[i] = s; result += s / 10; required[i] = 10 - s % 10; } Arrays.sort(required); for (int i = 0; i < n; ++i) { if (k >= required[i]) { result += 1; k -= required[i]; } } result += k / 10; result = min(result, n * 10); out.print(result); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
fe6ef1af3c7607643c2b932c02d4a92f
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.util.*; public class Main { private static Scanner in; static class Rate implements Comparable<Rate> { int rate, rateMod; public Rate() { } public Rate(int R) { rate = R; rateMod = R % 10; } public int compareTo(Rate arg0) { if (rateMod != arg0.rateMod) return rateMod - arg0.rateMod; return rate - arg0.rate; } } public static void main(String[] arg) { in = new Scanner(System.in); int n = in.nextInt(), k = in.nextInt(); Rate character[] = new Rate[n]; for (int i = 0; i < n; i++) { character[i] = new Rate(in.nextInt()); } Arrays.sort(character); int ans = 0; for (int i = n - 1; i >= 0; i--) { if ((k >= (10 - character[i].rateMod)) && character[i].rate < 100) { k -= (10 - character[i].rateMod); character[i].rate += (10 - character[i].rateMod); } } if (k > 0) { int i; for (i = 0; i < n; i++) { int rat = character[i].rate; character[i].rate += k; if (character[i].rate > 100) { k = character[i].rate - 100; character[i].rate = 100; } else k = 0; if (k == 0) break; } } for (int i = 0; i < n; i++) ans += (character[i].rate / 10); System.out.println(ans); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
c8f26b79b0e68536409b6fa3890e2aa3
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.util.Random; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); PriorityQueue<Skill> pq = new PriorityQueue<Skill>(); int ans = 0; for (int i = 0; i < n; i++) { int a = in.nextInt(); if (a == 100) { ans += 10; } else { pq.add(new Skill(a)); } } while (!pq.isEmpty() && k > 0) { Skill c = pq.poll(); int a = Math.min(k, 10 - (c.s % 10)); c.s += a; k -= a; if (c.s == 100) { ans += 10; } else { pq.add(c); } } while (!pq.isEmpty()) { ans += pq.poll().s / 10; } System.out.println(ans); } } static class Skill implements Comparable<Skill> { int s; public Skill(int ss) { this.s = ss; } public String toString() { return Integer.toString(s); } public int compareTo(Skill other) { if (this.s % 10 == 0) { return 1; } if (other.s % 10 == 0) { return -1; } return (other.s % 10 - this.s % 10); } } static class InputReader { public BufferedReader reader; public StringTokenizer st; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
dc13dbd3f27f7e68c0a834fc0604837d
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class ACMSolution { public static void main(String[] args) throws IOException { //Scanner in = new Scanner(Paths.get("input.txt")); Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] skill = new int[n]; int rem = 0; int total = 0; for(int i=0;i<n;i++){ int temp = in.nextInt(); rem += (100 - temp) / 10; skill[i] = 10 - temp % 10; total += temp / 10; } Arrays.sort(skill); for(int i=0;i<n;i++){ if(k>=skill[i] && skill[i]!=10){ k -= skill[i]; total++; } } if(k > 0){ total += Math.min(k / 10, rem); } System.out.println(total); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
dfc7b40eb0d79c68197bc02c5aeadcbf
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Round_323_Div2 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int k = in.nextInt(); long total = 0; int[]data = new int[n]; Point[]p = new Point[n]; for (int i = 0; i < n; i++) { int v = in.nextInt(); data[i] = v; p[i] = new Point((10 - (data[i] % 10)) % 10 , data[i]); } Arrays.sort(p); for(int i = 0; i < n && k > 0; i++){ if(p[i].x <= k){ k -= p[i].x; p[i].y += p[i].x; } } for(int i = 0; i < n && k > 0; i++){ int need = 100 - p[i].y; if(need <= k){ k -= need; p[i].y += need; }else{ p[i].y += k; k = 0; } } for(Point a : p){ total += a.y/10; } out.println(total); out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return x - o.x; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new // BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new // FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
cd3dfa52601dbf27745a23608be14909
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class P581C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] n_k_str = br.readLine().split(" "); int n = Integer.parseInt(n_k_str[0]); int k = Integer.parseInt(n_k_str[1]); String[] skills_str = br.readLine().split(" "); int[] skills = new int[n]; int[] hist = new int[11]; int total_level = 0; for (int i = 0; i < n; i++) { skills[i] = Integer.parseInt(skills_str[i]); int skill_level = skills[i] / 10; total_level += skill_level; if (skills[i] == 100) continue; int mod = skills[i] % 10; hist[10 - mod]++; hist[10] += (9 - skill_level); } for (int i = 1; i < hist.length; i++) { if (hist[i] == 0) continue; int max_times = Math.min(k / i, hist[i]); k -= max_times * i; total_level += max_times; } System.out.println(total_level); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
efee5c54ca4b199fb247f23574f307d5
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.util.*; import java.awt.Point; import java.io.*; import java.math.BigInteger; public class CodeForces { FastScanner in; PrintWriter out; class skil implements Comparable { int skil; int x; skil (int a, int b) { skil = a; x = b; } public int compareTo(Object obg) { skil y = (skil) obg; return x - y.x; } } public void solve() throws IOException { int n = in.nextInt(); int k = in.nextInt(); int level = 0; skil mas[] = new skil [n]; for (int i = 0; i < n; i++) { int cur = in.nextInt(); mas[i] = new skil(cur, cur%10); level += (cur / 10); } Arrays.sort(mas); for (int i = n-1; i >= 0; i--) { if (mas[i].x == 0) break; int needSkil = 10 - mas[i].x; if (k >= needSkil) { mas[i].skil += needSkil; k -= needSkil; level++; } } k /= 10; for (int i = 0; i < n && k > 0; i++) { if (mas[i].skil < 100) { int min = Math.min(10-(mas[i].skil / 10), k); level += min; k -= min; } } System.out.print(level); } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); //in = new FastScanner(new File("knapsack.in")); //out = new PrintWriter(new File("knapsack.out")); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String nextLine() { String ret = null; try { ret = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return ret; } 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()); } int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long[] nextLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } BigInteger nextBigInteger() { return new BigInteger(next()); } Point nextIntPoint() { int x = nextInt(); int y = nextInt(); return new Point(x, y); } Point[] nextIntPointArray(int size) { Point[] array = new Point[size]; for (int index = 0; index < size; ++index) { array[index] = nextIntPoint(); } return array; } List<Integer>[] readGraph(int vertexNumber, int edgeNumber, boolean undirected) { List<Integer>[] graph = new List[vertexNumber]; for (int index = 0; index < vertexNumber; ++index) { graph[index] = new ArrayList<Integer>(); } while (edgeNumber-- > 0) { int from = nextInt() - 1; int to = nextInt() - 1; graph[from].add(to); if(undirected) graph[to].add(from); } return graph; } } public static void main(String[] arg) { new CodeForces().run(); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
dcf17459809f9dd7230f55a965e857ed
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class P581C { private void run() { int n = nextInt(); int k = nextInt(); List<Integer> skills = new ArrayList<Integer>(n); for (int i = 0; i < n; i++) { int skill = nextInt(); skills.add(skill); } Collections.sort(skills, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { Integer i1 = o1 % 10; Integer i2 = o2 % 10; int i = (i1.compareTo(i2)); if (i == 0) { return -1 * (o1.compareTo(o2)); } else { return -1 * (i1.compareTo(i2)); } } }); int improvements = k; int current = 0; boolean startScratch = true; while (improvements > 0) { int missing = 10 - (skills.get(current) % 10); if (improvements >= missing) { if (!skills.get(current).equals(100)) { improvements = improvements - missing; int nextNumber = skills.get(current) + missing; skills.set(current, nextNumber); startScratch = false; } } else { improvements = 0; } current++; if (current >= n) { if (startScratch) { break; } current = 0; startScratch = true; } } int rating = 0; for (Integer skill : skills) { rating = rating + (int)Math.floor(skill / 10d); } System.out.println(rating); } private int getMaxSatisfaction( Map<Integer, Map<Integer, Long>> increaseSatisfaction, Map<Integer, Long> satisfaction, Set<Integer> visited, Long currentSatisfaction) { return 0; } private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount, HashSet<Integer> visited) { if (visited.contains(currentVertex)) { return 0; } visited.add(currentVertex); Set<Integer> vertexEdges = new HashSet<Integer>(edges.get(currentVertex)); vertexEdges.removeAll(visited); int newCatsCount = vertexes.get(currentVertex).booleanValue() ? catsCount + 1 : 0; if (vertexEdges == null || vertexEdges.size() == 0) { //leaf if (newCatsCount > maxCatsCount) { return 0; } else { if (currentVertex.intValue() != 1) { return 1; } else { return 0; } } } if (newCatsCount > maxCatsCount) { return 0; } int count = 0; for (Integer nextVertex : vertexEdges) { count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount, visited); } visited.remove(currentVertex); return count; } public boolean isTriangle(int a, int b, int c) { return (a + b > c) && (a + c > b) && (b + c > a); } private char nextChar() { try { return (char) System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } } private char[] incrementWord(char[] input, char maxLetter) { int currentIndex = input.length - 1; while (currentIndex >= 0 && input[currentIndex] == maxLetter) { currentIndex--; } if (currentIndex < 0) { return input; } input[currentIndex] = (char) (input[currentIndex] + 1); for (int i = currentIndex + 1; i < input.length; i++) { input[i] = 'a'; } return input; } private int getFree(Integer currentFree, Map<Integer, Integer> count) { while (count.containsKey(currentFree)) { currentFree = currentFree + 1; } return currentFree; } private double computeArea(double side1, double side2, double side3) { double p = (side1 + side2 + side3) / 2d; return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } private int greaterThan(List<Integer> indexesP2, Integer j) { for (int i = 0; i < indexesP2.size(); i++) { Integer number = indexesP2.get(i); if (number > j) { return indexesP2.size() - i; } } return 0; } public static void main(String[] args) { P581C notes = new P581C(); notes.run(); notes.close(); } private Scanner sc; private P581C() { this.sc = new Scanner(System.in); } private int[] asInteger(String[] values) { int[] ret = new int[values.length]; for (int i = 0; i < values.length; i++) { String val = values[i]; ret[i] = Integer.valueOf(val).intValue(); } return ret; } private String nextLine() { return sc.nextLine(); } private long nextLong() { return sc.nextLong(); } private int nextInt() { return sc.nextInt(); } private String readLine() { if (sc.hasNextLine()) { return (sc.nextLine()); } else { return null; } } private void close() { try { sc.close(); } catch (Exception e) { //log } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
98ae622f98f67870e5661477849516ff
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int k = Reader.nextInt(); NN mod[] = new NN[n]; for(int i = 0; i < n; i++){ int x = Reader.nextInt(); int m = x % 10; mod[i] = new NN(x, 10 - m); if(m == 0) mod[i].mod = 0; } Arrays.sort(mod); for (int i = 0; i < n; i++) { if(k < mod[i].mod){ break; } k -= mod[i].mod; mod[i].x += mod[i].mod; } for (int i = 0; i < n; i++) { if(k == 0) break; int y = Math.min(k , 100 - mod[i].x); if(y < 1) continue; k -= y; mod[i].x += y; } long sum = 0; for (int i = 0; i < n; i++) { sum += mod[i].x / 10; } System.out.println(sum); } } class Edge implements Comparable<Edge>{ int to; int from; int cost; Edge(int a, int b, int c){ from = a; to = b; cost = c; } @Override public int compareTo(Edge t) { return t.cost - cost; } } class NN implements Comparable<NN>{ int x; int mod; NN(int a, int b){ x = a; mod = b; } @Override public int compareTo(NN t) { return mod - t.mod; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
5d3c4b53060c0246d6f8da12fdbcd4bd
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.IOException; import java.io.InputStream; public class C581 { public static void main(String[] args) throws IOException { InputReader reader = new InputReader(System.in); int N = reader.readInt(); int K = reader.readInt(); int rating = 0; int[] upgrades = new int[11]; for (int n=0; n<N; n++) { int skill = reader.readInt(); rating += skill/10; int over = skill%10; if (over > 0) { upgrades[10-over]++; } upgrades[10] += 10-(skill+9)/10; } for (int price=1; price<=10; price++) { int max = Math.min(K/price, upgrades[price]); rating += max; K -= max*price; } System.out.println(rating); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final int readInt() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } public final long readLong() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
587567756277e6cbbb2a76d8d9c983dc
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.util.Collections; import java.util.Comparator; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } } class Task { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); int result = 0; for (int i = 0; i < n; ++i) { arr.add(in.nextInt()); } Collections.sort(arr, new SortedByName()); boolean b = true; long sum = 0; for (int i = 0; i < n; ++i) { int a = (10 - arr.get(i) % 10) % 10; if (k >= a) { k -= a; arr.set(i, arr.get(i) + a); } else { b = false; break; } } if (!b) { for (int i = 0; i < n; ++i) { result += arr.get(i) / 10; } } else { for (int i = 0; i < n; ++i) { result += arr.get(i) / 10; } int d = n * 10 - result; if (k / 10 > d) { result = n * 10; } else { result += k / 10; } } // for (int i = 0; i < n; ++i) { // out.print(arr.get(i) + " "); // } // out.println(); out.println(result); } } class SortedByName implements Comparator<Integer> { public int compare(Integer a, Integer b) { return (b % 10 - a % 10); } } class Scanner { BufferedReader in; StringTokenizer tok; public Scanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); tok = new StringTokenizer(""); } private String tryReadNextLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } public String nextToken() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(next()); } return tok.nextToken(); } public String next() { String newLine = tryReadNextLine(); if (newLine == null) throw new InputMismatchException(); return newLine; } public int nextInt() { return Integer.parseInt(nextToken()); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
42612c58394786836549e9e2fa605518
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { private static class Elem implements Comparable<Elem> { int number; int cong; public Elem(int number, int cong) { this.number = number; this.cong = cong; } @Override public int compareTo(Elem o) { if (cong > o.cong) return -1; return cong < o.cong ? 1 : 0; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] split = br.readLine().split(" "); int n = Integer.parseInt(split[0]); int k = Integer.parseInt(split[1]); Elem[] numbers = new Elem[n]; String[] split2 = br.readLine().split(" "); for (int i = 0; i < n; i++) { int parseInt = Integer.parseInt(split2[i]); numbers[i] = new Elem(parseInt, parseInt % 10); } Arrays.sort(numbers); int idx = 0; while (k > 0 && idx < n) { if (numbers[idx].number >= 100) { idx++; continue; } int toSum = Math.min(k, 10 - numbers[idx].cong); k -= toSum; numbers[idx++].number += toSum; } idx = 0; while (k > 0 && idx < n) { if (numbers[idx].number >= 100) { idx++; continue; } int toSum = Math.min(k, 10); k -= toSum; numbers[idx].number += toSum; } int sum = 0; for (Elem num : numbers) { sum += (num.number / 10); } System.out.println(sum); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
0260270abdc937e400b9ebb8ea37abd7
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Collections; import java.util.LinkedList; import java.util.StringTokenizer; public class R322C { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); LinkedList<MyNum> l = new LinkedList<>(); while(n-- > 0) { int x = in.nextInt(); l.addLast(new MyNum(x)); } Collections.sort(l); int over100 = 0; int over100Sum = 0; while (k > 0 && !l.isEmpty()) { // System.out.println(k); // System.out.println(l); MyNum last = l.removeLast(); if (last.val >= 100) { over100Sum += 10; continue; } if (k >= 10 - (last.val % 10)) { k -= (10 - (last.val % 10)); last.val += (10 - (last.val % 10)); } else { l.addFirst(last); break; } l.addFirst(last); } long sum = 0; for (MyNum i : l) { sum += (i.val / 10); } out.println(sum + over100Sum); out.flush(); } static class MyNum implements Comparable { int val; @Override public int compareTo(Object o) { MyNum other = (MyNum) o; return (this.val % 10) - (other.val % 10); } public MyNum(int v) { val = v; } public String toString() { return String.valueOf(val); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
c9a684c4bbe82f5208e539e12f671054
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.*; import java.util.*; public final class developing_skills { static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(System.out); @SuppressWarnings("unchecked") public static void main(String args[]) throws Exception { int n=sc.nextInt(),k=sc.nextInt(),c=0; int[] a=new int[n],cnt=new int[10]; ArrayDeque<Node>[] al=new ArrayDeque[10]; long ans=0; for(int i=0;i<=9;i++) { al[i]=new ArrayDeque<Node>(); } for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]>=100) { ans+=10; c++; } else { int last=a[i]%10; al[last].add(new Node(a[i],last)); } } outer: while(k>0 && c<n) { for(int i=9;i>=0;i--) { ArrayDeque<Node> curr_list=al[i]; while(curr_list.size()>0) { Node curr=curr_list.pollFirst(); if(curr.val<100) { int add=10-curr.last; if(k-add>=0) { k-=add; curr.val+=add; curr.last=curr.val%10; al[curr.last].add(new Node(curr.val,curr.last)); } else { al[curr.last].add(new Node(curr.val,curr.last)); break outer; } } else { c++; ans+=10; } } } } for(int i=9;i>=0;i--) { ArrayDeque<Node> ad=al[i]; for(Node x:ad) { ans+=(x.val/10); } } out.println(ans); out.close(); } } class Node { int val,last; public Node(int val,int last) { this.val=val; this.last=last; } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
6b05bf51d8e1e1eb25ed2f65cf69d41c
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.util.*; public class fence { //static {System.out.println("hello"); } public static void main(String args[]) { Scanner scn=new Scanner(System.in); int n=scn.nextInt(); int k=scn.nextInt(); num ar[]=new num[n]; int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=scn.nextInt(); ar[i]=new num(10- a[i]%10, a[i]); } Arrays.sort(ar, new num() ); int rat=0; for(int i=0;i<n;i++) { if (k >= ar[i].ff) { ar[i].ss+=(ar[i].ff); k-= ar[i].ff ; } } int add=k/10; for(int i=0;i<n;i++) { if (add*10>=100-ar[i].ss) { add-=(100-ar[i].ss)/10; ar[i].ss=100; } else { ar[i].ss+=add*10; add=0; } } int ans=0; for(int i=0;i<n;i++) ans+=(ar[i].ss/10); System.out.println(ans); } } class num implements Comparator<num> { public int ff; public int ss; num() { } num(int x,int y) { this.ff = x; this.ss =y; } public int getff() { return ff; } public int getss() { return ss; } public boolean equals(Object n1) {num n=(num)n1; if (this.ff==n.ff && this.ss==n.ss) return true; else return false; } public int compare(num n1,num n2) { if (n1.ff!=n2.ff) return n1.ff-n2.ff; else return n2.ss-n1.ss; } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output
PASSED
04d1f7436cda9ace760bf571eff1285b
train_000.jsonl
1443430800
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class Solution { Comparator<Integer> myComparator = new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { if (a % 10 > b % 10) return -1; if (a % 10 < b % 10) return 1; return 0; } }; public void solve() { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int k = in.nextInt(); List<Integer> list = new ArrayList<Integer>(); int total = 0; for (int i = 0; i < n; i++) list.add(in.nextInt()); Collections.sort(list, myComparator); int ind = 0; while (ind < n) { if (k <= 0) // no more bonus skills break; if (list.get(ind) % 10 == 0) { // no need to upgrade this skill ind++; continue; } int cur = list.get(ind); int left = 10 - cur % 10; if (k - left >= 0) { list.set(ind, cur + left); k -= left; } else break; ind++; } if (k > 0) { for (int i = 0; i < list.size(); i++) { if (k <= 0) break; int cur = list.get(i); if (cur < 100) { int dist = 100 - cur; int fin; if (k < dist) { fin = cur + k; list.set(i, fin); k = 0; } else { list.set(i, 100); k -= dist; } } } } for (int i = 0; i < n; i++) total += list.get(i) / 10; out.print(total); out.close(); } public static void main(String[] args) { new Solution().solve(); } }
Java
["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"]
1 second
["2", "5", "20"]
NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
Java 7
standard input
[ "implementation", "sortings", "math" ]
b4341e1b0ec0b7341fdbe6edfe81a0d4
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
1,400
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
standard output